add_library.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. Object Libraries
  65. ^^^^^^^^^^^^^^^^
  66. .. signature::
  67. add_library(<name> OBJECT <sources>...)
  68. :target: OBJECT
  69. Add an :ref:`Object Library <Object Libraries>` to compile source files
  70. without archiving or linking their object files into a library.
  71. Other targets created by ``add_library`` or :command:`add_executable`
  72. may reference the objects using an expression of the
  73. form :genex:`$\<TARGET_OBJECTS:objlib\> <TARGET_OBJECTS>` as a source, where
  74. ``objlib`` is the object library name. For example:
  75. .. code-block:: cmake
  76. add_library(... $<TARGET_OBJECTS:objlib> ...)
  77. add_executable(... $<TARGET_OBJECTS:objlib> ...)
  78. will include objlib's object files in a library and an executable
  79. along with those compiled from their own sources. Object libraries
  80. may contain only sources that compile, header files, and other files
  81. that would not affect linking of a normal library (e.g. ``.txt``).
  82. They may contain custom commands generating such sources, but not
  83. ``PRE_BUILD``, ``PRE_LINK``, or ``POST_BUILD`` commands. Some native build
  84. systems (such as Xcode) may not like targets that have only object files, so
  85. consider adding at least one real source file to any target that references
  86. :genex:`$\<TARGET_OBJECTS:objlib\> <TARGET_OBJECTS>`.
  87. .. versionadded:: 3.12
  88. Object libraries can be linked to with :command:`target_link_libraries`.
  89. Interface Libraries
  90. ^^^^^^^^^^^^^^^^^^^
  91. .. signature::
  92. add_library(<name> INTERFACE)
  93. :target: INTERFACE
  94. Add an :ref:`Interface Library <Interface Libraries>` target that may
  95. specify usage requirements for dependents but does not compile sources
  96. and does not produce a library artifact on disk.
  97. An interface library with no source files is not included as a target
  98. in the generated buildsystem. However, it may have
  99. properties set on it and it may be installed and exported.
  100. Typically, ``INTERFACE_*`` properties are populated on an interface
  101. target using the commands:
  102. * :command:`set_property`,
  103. * :command:`target_link_libraries(INTERFACE)`,
  104. * :command:`target_link_options(INTERFACE)`,
  105. * :command:`target_include_directories(INTERFACE)`,
  106. * :command:`target_compile_options(INTERFACE)`,
  107. * :command:`target_compile_definitions(INTERFACE)`, and
  108. * :command:`target_sources(INTERFACE)`,
  109. and then it is used as an argument to :command:`target_link_libraries`
  110. like any other target.
  111. .. versionadded:: 3.15
  112. An interface library can have :prop_tgt:`PUBLIC_HEADER` and
  113. :prop_tgt:`PRIVATE_HEADER` properties. The headers specified by those
  114. properties can be installed using the :command:`install(TARGETS)` command.
  115. .. signature::
  116. add_library(<name> INTERFACE [EXCLUDE_FROM_ALL] <sources>...)
  117. :target: INTERFACE-with-sources
  118. .. versionadded:: 3.19
  119. Add an :ref:`Interface Library <Interface Libraries>` target with
  120. source files (in addition to usage requirements and properties as
  121. documented by the :command:`above signature <add_library(INTERFACE)>`).
  122. Source files may be listed directly in the ``add_library`` call
  123. or added later by calls to :command:`target_sources` with the
  124. ``PRIVATE`` or ``PUBLIC`` keywords.
  125. If an interface library has source files (i.e. the :prop_tgt:`SOURCES`
  126. target property is set), or header sets (i.e. the :prop_tgt:`HEADER_SETS`
  127. target property is set), it will appear in the generated buildsystem
  128. as a build target much like a target defined by the
  129. :command:`add_custom_target` command. It does not compile any sources,
  130. but does contain build rules for custom commands created by the
  131. :command:`add_custom_command` command.
  132. The options are:
  133. ``EXCLUDE_FROM_ALL``
  134. Set the :prop_tgt:`EXCLUDE_FROM_ALL` target property automatically.
  135. See documentation of that target property for details.
  136. .. note::
  137. In most command signatures where the ``INTERFACE`` keyword appears,
  138. the items listed after it only become part of that target's usage
  139. requirements and are not part of the target's own settings. However,
  140. in this signature of ``add_library``, the ``INTERFACE`` keyword refers
  141. to the library type only. Sources listed after it in the ``add_library``
  142. call are ``PRIVATE`` to the interface library and do not appear in its
  143. :prop_tgt:`INTERFACE_SOURCES` target property.
  144. .. _`add_library imported libraries`:
  145. Imported Libraries
  146. ^^^^^^^^^^^^^^^^^^
  147. .. signature::
  148. add_library(<name> <type> IMPORTED [GLOBAL])
  149. :target: IMPORTED
  150. Add an :ref:`IMPORTED library target <Imported Targets>` called ``<name>``.
  151. The target name may be referenced like any target built within the project,
  152. except that by default it is visible only in the directory in which it is
  153. created, and below.
  154. The ``<type>`` must be one of:
  155. ``STATIC``, ``SHARED``, ``MODULE``, ``UNKNOWN``
  156. References a library file located outside the project. The
  157. :prop_tgt:`IMPORTED_LOCATION` target property (or its per-configuration
  158. variant :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) specifies the
  159. location of the main library file on disk:
  160. * For a ``SHARED`` library on most non-Windows platforms, the main library
  161. file is the ``.so`` or ``.dylib`` file used by both linkers and dynamic
  162. loaders. If the referenced library file has a ``SONAME`` (or on macOS,
  163. has a ``LC_ID_DYLIB`` starting in ``@rpath/``), the value of that field
  164. should be set in the :prop_tgt:`IMPORTED_SONAME` target property.
  165. If the referenced library file does not have a ``SONAME``, but the
  166. platform supports it, then the :prop_tgt:`IMPORTED_NO_SONAME` target
  167. property should be set.
  168. * For a ``SHARED`` library on Windows, the :prop_tgt:`IMPORTED_IMPLIB`
  169. target property (or its per-configuration variant
  170. :prop_tgt:`IMPORTED_IMPLIB_<CONFIG>`) specifies the location of the
  171. DLL import library file (``.lib`` or ``.dll.a``) on disk, and the
  172. ``IMPORTED_LOCATION`` is the location of the ``.dll`` runtime
  173. library (and is optional, but needed by the :genex:`TARGET_RUNTIME_DLLS`
  174. generator expression).
  175. Additional usage requirements may be specified in ``INTERFACE_*``
  176. properties.
  177. An ``UNKNOWN`` library type is typically only used in the implementation
  178. of :ref:`Find Modules`. It allows the path to an imported library
  179. (often found using the :command:`find_library` command) to be used
  180. without having to know what type of library it is. This is especially
  181. useful on Windows where a static library and a DLL's import library
  182. both have the same file extension.
  183. ``OBJECT``
  184. References a set of object files located outside the project.
  185. The :prop_tgt:`IMPORTED_OBJECTS` target property (or its per-configuration
  186. variant :prop_tgt:`IMPORTED_OBJECTS_<CONFIG>`) specifies the locations of
  187. object files on disk.
  188. Additional usage requirements may be specified in ``INTERFACE_*``
  189. properties.
  190. ``INTERFACE``
  191. Does not reference any library or object files on disk, but may
  192. specify usage requirements in ``INTERFACE_*`` properties.
  193. The options are:
  194. ``GLOBAL``
  195. Make the target name globally visible.
  196. No rules are generated to build imported targets, and the :prop_tgt:`IMPORTED`
  197. target property is ``True``. Imported libraries are useful for convenient
  198. reference from commands like :command:`target_link_libraries`.
  199. Details about the imported library are specified by setting properties whose
  200. names begin in ``IMPORTED_`` and ``INTERFACE_``. See documentation of
  201. such properties for more information.
  202. Alias Libraries
  203. ^^^^^^^^^^^^^^^
  204. .. signature::
  205. add_library(<name> ALIAS <target>)
  206. :target: ALIAS
  207. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
  208. used to refer to ``<target>`` in subsequent commands. The ``<name>`` does
  209. not appear in the generated buildsystem as a make target. The ``<target>``
  210. may not be an ``ALIAS``.
  211. .. versionadded:: 3.11
  212. An ``ALIAS`` can target a ``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  213. .. versionadded:: 3.18
  214. An ``ALIAS`` can target a non-``GLOBAL`` Imported Target. Such alias is
  215. scoped to the directory in which it is created and below.
  216. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  217. alias is global or not.
  218. ``ALIAS`` targets can be used as linkable targets and as targets to
  219. read properties from. They can also be tested for existence with the
  220. regular :command:`if(TARGET)` subcommand. The ``<name>`` may not be used
  221. to modify properties of ``<target>``, that is, it may not be used as the
  222. operand of :command:`set_property`, :command:`set_target_properties`,
  223. :command:`target_link_libraries` etc. An ``ALIAS`` target may not be
  224. installed or exported.
  225. See Also
  226. ^^^^^^^^
  227. * :command:`add_executable`