add_library.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. add_library
  2. -----------
  3. .. only:: html
  4. .. contents::
  5. Add a library to the project using the specified source files.
  6. Normal Libraries
  7. ^^^^^^^^^^^^^^^^
  8. .. code-block:: cmake
  9. add_library(<name> [STATIC | SHARED | MODULE]
  10. [EXCLUDE_FROM_ALL]
  11. [<source>...])
  12. Adds a library target called ``<name>`` to be built from the source files
  13. listed in the command invocation. The ``<name>``
  14. corresponds to the logical target name and must be globally unique within
  15. a project. The actual file name of the library built is constructed based
  16. on conventions of the native platform (such as ``lib<name>.a`` or
  17. ``<name>.lib``).
  18. .. versionadded:: 3.1
  19. Source arguments to ``add_library`` may use "generator expressions" with
  20. the syntax ``$<...>``. See the :manual:`cmake-generator-expressions(7)`
  21. manual for available expressions.
  22. .. versionadded:: 3.11
  23. The source files can be omitted if they are added later using
  24. :command:`target_sources`.
  25. ``STATIC``, ``SHARED``, or ``MODULE`` may be given to specify the type of
  26. library to be created. ``STATIC`` libraries are archives of object files
  27. for use when linking other targets. ``SHARED`` libraries are linked
  28. dynamically and loaded at runtime. ``MODULE`` libraries are plugins that
  29. are not linked into other targets but may be loaded dynamically at runtime
  30. using dlopen-like functionality. If no type is given explicitly the
  31. type is ``STATIC`` or ``SHARED`` based on whether the current value of the
  32. variable :variable:`BUILD_SHARED_LIBS` is ``ON``. For ``SHARED`` and
  33. ``MODULE`` libraries the :prop_tgt:`POSITION_INDEPENDENT_CODE` target
  34. property is set to ``ON`` automatically.
  35. A ``SHARED`` library may be marked with the :prop_tgt:`FRAMEWORK`
  36. target property to create an macOS Framework.
  37. .. versionadded:: 3.8
  38. A ``STATIC`` library may be marked with the :prop_tgt:`FRAMEWORK`
  39. target property to create a static Framework.
  40. If a library does not export any symbols, it must not be declared as a
  41. ``SHARED`` library. For example, a Windows resource DLL or a managed C++/CLI
  42. DLL that exports no unmanaged symbols would need to be a ``MODULE`` library.
  43. This is because CMake expects a ``SHARED`` library to always have an
  44. associated import library on Windows.
  45. By default the library file will be created in the build tree directory
  46. corresponding to the source tree directory in which the command was
  47. invoked. See documentation of the :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY`,
  48. :prop_tgt:`LIBRARY_OUTPUT_DIRECTORY`, and
  49. :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` target properties to change this
  50. location. See documentation of the :prop_tgt:`OUTPUT_NAME` target
  51. property to change the ``<name>`` part of the final file name.
  52. If ``EXCLUDE_FROM_ALL`` is given the corresponding property will be set on
  53. the created target. See documentation of the :prop_tgt:`EXCLUDE_FROM_ALL`
  54. target property for details.
  55. See the :manual:`cmake-buildsystem(7)` manual for more on defining
  56. buildsystem properties.
  57. See also :prop_sf:`HEADER_FILE_ONLY` on what to do if some sources are
  58. pre-processed, and you want to have the original sources reachable from
  59. within IDE.
  60. Object Libraries
  61. ^^^^^^^^^^^^^^^^
  62. .. code-block:: cmake
  63. add_library(<name> OBJECT [<source>...])
  64. Creates an :ref:`Object Library <Object Libraries>`. An object library
  65. compiles source files but does not archive or link their object files into a
  66. library. Instead other targets created by :command:`add_library` or
  67. :command:`add_executable` may reference the objects using an expression of the
  68. form :genex:`$\<TARGET_OBJECTS:objlib\> <TARGET_OBJECTS>` as a source, where
  69. ``objlib`` is the object library name. For example:
  70. .. code-block:: cmake
  71. add_library(... $<TARGET_OBJECTS:objlib> ...)
  72. add_executable(... $<TARGET_OBJECTS:objlib> ...)
  73. will include objlib's object files in a library and an executable
  74. along with those compiled from their own sources. Object libraries
  75. may contain only sources that compile, header files, and other files
  76. that would not affect linking of a normal library (e.g. ``.txt``).
  77. They may contain custom commands generating such sources, but not
  78. ``PRE_BUILD``, ``PRE_LINK``, or ``POST_BUILD`` commands. Some native build
  79. systems (such as Xcode) may not like targets that have only object files, so
  80. consider adding at least one real source file to any target that references
  81. :genex:`$\<TARGET_OBJECTS:objlib\> <TARGET_OBJECTS>`.
  82. .. versionadded:: 3.12
  83. Object libraries can be linked to with :command:`target_link_libraries`.
  84. Interface Libraries
  85. ^^^^^^^^^^^^^^^^^^^
  86. .. code-block:: cmake
  87. add_library(<name> INTERFACE)
  88. Creates an :ref:`Interface Library <Interface Libraries>`.
  89. An ``INTERFACE`` library target does not compile sources and does
  90. not produce a library artifact on disk. However, it may have
  91. properties set on it and it may be installed and exported.
  92. Typically, ``INTERFACE_*`` properties are populated on an interface
  93. target using the commands:
  94. * :command:`set_property`,
  95. * :command:`target_link_libraries(INTERFACE)`,
  96. * :command:`target_link_options(INTERFACE)`,
  97. * :command:`target_include_directories(INTERFACE)`,
  98. * :command:`target_compile_options(INTERFACE)`,
  99. * :command:`target_compile_definitions(INTERFACE)`, and
  100. * :command:`target_sources(INTERFACE)`,
  101. and then it is used as an argument to :command:`target_link_libraries`
  102. like any other target.
  103. An interface library created with the above signature has no source files
  104. itself and is not included as a target in the generated buildsystem.
  105. .. versionadded:: 3.15
  106. An interface library can have :prop_tgt:`PUBLIC_HEADER` and
  107. :prop_tgt:`PRIVATE_HEADER` properties. The headers specified by those
  108. properties can be installed using the :command:`install(TARGETS)` command.
  109. .. versionadded:: 3.19
  110. An interface library target may be created with source files:
  111. .. code-block:: cmake
  112. add_library(<name> INTERFACE [<source>...] [EXCLUDE_FROM_ALL])
  113. Source files may be listed directly in the ``add_library`` call or added
  114. later by calls to :command:`target_sources` with the ``PRIVATE`` or
  115. ``PUBLIC`` keywords.
  116. If an interface library has source files (i.e. the :prop_tgt:`SOURCES`
  117. target property is set), or header sets (i.e. the :prop_tgt:`HEADER_SETS`
  118. target property is set), it will appear in the generated buildsystem
  119. as a build target much like a target defined by the
  120. :command:`add_custom_target` command. It does not compile any sources,
  121. but does contain build rules for custom commands created by the
  122. :command:`add_custom_command` command.
  123. .. note::
  124. In most command signatures where the ``INTERFACE`` keyword appears,
  125. the items listed after it only become part of that target's usage
  126. requirements and are not part of the target's own settings. However,
  127. in this signature of ``add_library``, the ``INTERFACE`` keyword refers
  128. to the library type only. Sources listed after it in the ``add_library``
  129. call are ``PRIVATE`` to the interface library and do not appear in its
  130. :prop_tgt:`INTERFACE_SOURCES` target property.
  131. .. _`add_library imported libraries`:
  132. Imported Libraries
  133. ^^^^^^^^^^^^^^^^^^
  134. .. code-block:: cmake
  135. add_library(<name> <type> IMPORTED [GLOBAL])
  136. Creates an :ref:`IMPORTED library target <Imported Targets>` called ``<name>``.
  137. No rules are generated to build it, and the :prop_tgt:`IMPORTED` target
  138. property is ``True``. The target name has scope in the directory in which
  139. it is created and below, but the ``GLOBAL`` option extends visibility.
  140. It may be referenced like any target built within the project.
  141. ``IMPORTED`` libraries are useful for convenient reference from commands
  142. like :command:`target_link_libraries`. Details about the imported library
  143. are specified by setting properties whose names begin in ``IMPORTED_`` and
  144. ``INTERFACE_``.
  145. The ``<type>`` must be one of:
  146. ``STATIC``, ``SHARED``, ``MODULE``, ``UNKNOWN``
  147. References a library file located outside the project. The
  148. :prop_tgt:`IMPORTED_LOCATION` target property (or its per-configuration
  149. variant :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) specifies the
  150. location of the main library file on disk:
  151. * For a ``SHARED`` library on most non-Windows platforms, the main library
  152. file is the ``.so`` or ``.dylib`` file used by both linkers and dynamic
  153. loaders. If the referenced library file has a ``SONAME`` (or on macOS,
  154. has a ``LC_ID_DYLIB`` starting in ``@rpath/``), the value of that field
  155. should be set in the :prop_tgt:`IMPORTED_SONAME` target property.
  156. If the referenced library file does not have a ``SONAME``, but the
  157. platform supports it, then the :prop_tgt:`IMPORTED_NO_SONAME` target
  158. property should be set.
  159. * For a ``SHARED`` library on Windows, the :prop_tgt:`IMPORTED_IMPLIB`
  160. target property (or its per-configuration variant
  161. :prop_tgt:`IMPORTED_IMPLIB_<CONFIG>`) specifies the location of the
  162. DLL import library file (``.lib`` or ``.dll.a``) on disk, and the
  163. ``IMPORTED_LOCATION`` is the location of the ``.dll`` runtime
  164. library (and is optional, but needed by the :genex:`TARGET_RUNTIME_DLLS`
  165. generator expression).
  166. Additional usage requirements may be specified in ``INTERFACE_*`` properties.
  167. An ``UNKNOWN`` library type is typically only used in the implementation of
  168. :ref:`Find Modules`. It allows the path to an imported library (often found
  169. using the :command:`find_library` command) to be used without having to know
  170. what type of library it is. This is especially useful on Windows where a
  171. static library and a DLL's import library both have the same file extension.
  172. ``OBJECT``
  173. References a set of object files located outside the project.
  174. The :prop_tgt:`IMPORTED_OBJECTS` target property (or its per-configuration
  175. variant :prop_tgt:`IMPORTED_OBJECTS_<CONFIG>`) specifies the locations of
  176. object files on disk.
  177. Additional usage requirements may be specified in ``INTERFACE_*`` properties.
  178. ``INTERFACE``
  179. Does not reference any library or object files on disk, but may
  180. specify usage requirements in ``INTERFACE_*`` properties.
  181. See documentation of the ``IMPORTED_*`` and ``INTERFACE_*`` properties
  182. for more information.
  183. Alias Libraries
  184. ^^^^^^^^^^^^^^^
  185. .. code-block:: cmake
  186. add_library(<name> ALIAS <target>)
  187. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
  188. used to refer to ``<target>`` in subsequent commands. The ``<name>`` does
  189. not appear in the generated buildsystem as a make target. The ``<target>``
  190. may not be an ``ALIAS``.
  191. .. versionadded:: 3.11
  192. An ``ALIAS`` can target a ``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  193. .. versionadded:: 3.18
  194. An ``ALIAS`` can target a non-``GLOBAL`` Imported Target. Such alias is
  195. scoped to the directory in which it is created and below.
  196. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  197. alias is global or not.
  198. ``ALIAS`` targets can be used as linkable targets and as targets to
  199. read properties from. They can also be tested for existence with the
  200. regular :command:`if(TARGET)` subcommand. The ``<name>`` may not be used
  201. to modify properties of ``<target>``, that is, it may not be used as the
  202. operand of :command:`set_property`, :command:`set_target_properties`,
  203. :command:`target_link_libraries` etc. An ``ALIAS`` target may not be
  204. installed or exported.