add_library.rst 9.6 KB

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