add_library.rst 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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 ``$<TARGET_OBJECTS:objlib>`` as a source, where ``objlib`` is the
  69. 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. ``$<TARGET_OBJECTS:objlib>``.
  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), it will appear in the generated buildsystem
  118. as a build target much like a target defined by the
  119. :command:`add_custom_target` command. It does not compile any sources,
  120. but does contain build rules for custom commands created by the
  121. :command:`add_custom_command` command.
  122. .. note::
  123. In most command signatures where the ``INTERFACE`` keyword appears,
  124. the items listed after it only become part of that target's usage
  125. requirements and are not part of the target's own settings. However,
  126. in this signature of ``add_library``, the ``INTERFACE`` keyword refers
  127. to the library type only. Sources listed after it in the ``add_library``
  128. call are ``PRIVATE`` to the interface library and do not appear in its
  129. :prop_tgt:`INTERFACE_SOURCES` target property.
  130. Imported Libraries
  131. ^^^^^^^^^^^^^^^^^^
  132. .. code-block:: cmake
  133. add_library(<name> <type> IMPORTED [GLOBAL])
  134. Creates an :ref:`IMPORTED library target <Imported Targets>` called ``<name>``.
  135. No rules are generated to build it, and the :prop_tgt:`IMPORTED` target
  136. property is ``True``. The target name has scope in the directory in which
  137. it is created and below, but the ``GLOBAL`` option extends visibility.
  138. It may be referenced like any target built within the project.
  139. ``IMPORTED`` libraries are useful for convenient reference from commands
  140. like :command:`target_link_libraries`. Details about the imported library
  141. are specified by setting properties whose names begin in ``IMPORTED_`` and
  142. ``INTERFACE_``.
  143. The ``<type>`` must be one of:
  144. ``STATIC``, ``SHARED``, ``MODULE``, ``UNKNOWN``
  145. References a library file located outside the project. The
  146. :prop_tgt:`IMPORTED_LOCATION` target property (or its per-configuration
  147. variant :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) specifies the
  148. location of the main library file on disk:
  149. * For a ``SHARED`` library on most non-Windows platforms, the main library
  150. file is the ``.so`` or ``.dylib`` file used by both linkers and dynamic
  151. loaders. If the referenced library file has a ``SONAME`` (or on macOS,
  152. has a ``LC_ID_DYLIB`` starting in ``@rpath/``), the value of that field
  153. should be set in the :prop_tgt:`IMPORTED_SONAME` target property.
  154. If the referenced library file does not have a ``SONAME``, but the
  155. platform supports it, then the :prop_tgt:`IMPORTED_NO_SONAME` target
  156. property should be set.
  157. * For a ``SHARED`` library on Windows, the :prop_tgt:`IMPORTED_IMPLIB`
  158. target property (or its per-configuration variant
  159. :prop_tgt:`IMPORTED_IMPLIB_<CONFIG>`) specifies the location of the
  160. DLL import library file (``.lib`` or ``.dll.a``) on disk, and the
  161. ``IMPORTED_LOCATION`` is the location of the ``.dll`` runtime
  162. library (and is optional).
  163. Additional usage requirements may be specified in ``INTERFACE_*`` properties.
  164. An ``UNKNOWN`` library type is typically only used in the implementation of
  165. :ref:`Find Modules`. It allows the path to an imported library (often found
  166. using the :command:`find_library` command) to be used without having to know
  167. what type of library it is. This is especially useful on Windows where a
  168. static library and a DLL's import library both have the same file extension.
  169. ``OBJECT``
  170. References a set of object files located outside the project.
  171. The :prop_tgt:`IMPORTED_OBJECTS` target property (or its per-configuration
  172. variant :prop_tgt:`IMPORTED_OBJECTS_<CONFIG>`) specifies the locations of
  173. object files on disk.
  174. Additional usage requirements may be specified in ``INTERFACE_*`` properties.
  175. ``INTERFACE``
  176. Does not reference any library or object files on disk, but may
  177. specify usage requirements in ``INTERFACE_*`` properties.
  178. See documentation of the ``IMPORTED_*`` and ``INTERFACE_*`` properties
  179. for more information.
  180. Alias Libraries
  181. ^^^^^^^^^^^^^^^
  182. .. code-block:: cmake
  183. add_library(<name> ALIAS <target>)
  184. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
  185. used to refer to ``<target>`` in subsequent commands. The ``<name>`` does
  186. not appear in the generated buildsystem as a make target. The ``<target>``
  187. may not be an ``ALIAS``.
  188. .. versionadded:: 3.11
  189. An ``ALIAS`` can target a ``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  190. .. versionadded:: 3.18
  191. An ``ALIAS`` can target a non-``GLOBAL`` Imported Target. Such alias is
  192. scoped to the directory in which it is created and below.
  193. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  194. alias is global or not.
  195. ``ALIAS`` targets can be used as linkable targets and as targets to
  196. read properties from. They can also be tested for existence with the
  197. regular :command:`if(TARGET)` subcommand. The ``<name>`` may not be used
  198. to modify properties of ``<target>``, that is, it may not be used as the
  199. operand of :command:`set_property`, :command:`set_target_properties`,
  200. :command:`target_link_libraries` etc. An ``ALIAS`` target may not be
  201. installed or exported.