add_library.rst 12 KB

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