add_library
-----------
.. only:: html
.. contents::
Add a library to the project using the specified source files.
Normal Libraries
^^^^^^^^^^^^^^^^
.. signature::
add_library( [] [EXCLUDE_FROM_ALL] ...)
:target: normal
Add a library target called ```` to be built from the source files
listed in the command invocation.
The optional ```` specifies the type of library to be created:
``STATIC``
An archive of object files for use when linking other targets.
``SHARED``
A dynamic library that may be linked by other targets and loaded
at runtime.
``MODULE``
A plugin that may not be linked by other targets, but may be
dynamically loaded at runtime using dlopen-like functionality.
If no ```` is given the default is ``STATIC`` or ``SHARED``
based on the value of the :variable:`BUILD_SHARED_LIBS` variable.
The options are:
``EXCLUDE_FROM_ALL``
Set the :prop_tgt:`EXCLUDE_FROM_ALL` target property automatically.
See documentation of that target property for details.
The ```` corresponds to the logical target name and must be globally
unique within a project. The actual file name of the library built is
constructed based on conventions of the native platform (such as
``lib.a`` or ``.lib``).
.. versionadded:: 3.1
Source arguments to ``add_library`` may use "generator expressions" with
the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
manual for available expressions.
.. versionadded:: 3.11
The source files can be omitted if they are added later using
:command:`target_sources`.
For ``SHARED`` and ``MODULE`` libraries the
:prop_tgt:`POSITION_INDEPENDENT_CODE` target
property is set to ``ON`` automatically.
A ``SHARED`` library may be marked with the :prop_tgt:`FRAMEWORK`
target property to create an macOS Framework.
.. versionadded:: 3.8
A ``STATIC`` library may be marked with the :prop_tgt:`FRAMEWORK`
target property to create a static Framework.
If a library does not export any symbols, it must not be declared as a
``SHARED`` library. For example, a Windows resource DLL or a managed C++/CLI
DLL that exports no unmanaged symbols would need to be a ``MODULE`` library.
This is because CMake expects a ``SHARED`` library to always have an
associated import library on Windows.
By default the library file will be created in the build tree directory
corresponding to the source tree directory in which the command was
invoked. See documentation of the :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY`,
:prop_tgt:`LIBRARY_OUTPUT_DIRECTORY`, and
:prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target properties to change this
location. See documentation of the :prop_tgt:`OUTPUT_NAME` target
property to change the ```` part of the final file name.
See the :manual:`cmake-buildsystem(7)` manual for more on defining
buildsystem properties.
See also :prop_sf:`HEADER_FILE_ONLY` on what to do if some sources are
pre-processed, and you want to have the original sources reachable from
within IDE.
Object Libraries
^^^^^^^^^^^^^^^^
.. signature::
add_library( OBJECT ...)
:target: OBJECT
Add an :ref:`Object Library