add_library.rst 13 KB

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