add_library.rst 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. [source1] [source2 ...])
  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. Imported Libraries
  54. ^^^^^^^^^^^^^^^^^^
  55. .. code-block:: cmake
  56. add_library(<name> <SHARED|STATIC|MODULE|OBJECT|UNKNOWN> IMPORTED
  57. [GLOBAL])
  58. An :ref:`IMPORTED library target <Imported Targets>` references a library
  59. file located outside the project. No rules are generated to build it, and
  60. the :prop_tgt:`IMPORTED` target property is ``True``. The target name has
  61. scope in the directory in which it is created and below, but the ``GLOBAL``
  62. option extends visibility. It may be referenced like any target built
  63. within the project. ``IMPORTED`` libraries are useful for convenient
  64. reference from commands like :command:`target_link_libraries`. Details
  65. about the imported library are specified by setting properties whose names
  66. begin in ``IMPORTED_`` and ``INTERFACE_``.
  67. The most important properties are:
  68. * :prop_tgt:`IMPORTED_LOCATION` (and its per-configuration
  69. variant :prop_tgt:`IMPORTED_LOCATION_<CONFIG>`) which specifies the
  70. location of the main library file on disk.
  71. * :prop_tgt:`IMPORTED_OBJECTS` (and :prop_tgt:`IMPORTED_OBJECTS_<CONFIG>`)
  72. for object libraries, specifies the locations of object files on disk.
  73. * :prop_tgt:`PUBLIC_HEADER` files to be installed during :command:`install` invocation
  74. See documentation of the ``IMPORTED_*`` and ``INTERFACE_*`` properties
  75. for more information.
  76. An ``UNKNOWN`` library type is typically only used in the implementation of
  77. :ref:`Find Modules`. It allows the path to an imported library (often found
  78. using the :command:`find_library` command) to be used without having to know
  79. what type of library it is. This is especially useful on Windows where a
  80. static library and a DLL's import library both have the same file extension.
  81. Object Libraries
  82. ^^^^^^^^^^^^^^^^
  83. .. code-block:: cmake
  84. add_library(<name> OBJECT <src>...)
  85. Creates an :ref:`Object Library <Object Libraries>`. An object library
  86. compiles source files but does not archive or link their object files into a
  87. library. Instead other targets created by :command:`add_library` or
  88. :command:`add_executable` may reference the objects using an expression of the
  89. form ``$<TARGET_OBJECTS:objlib>`` as a source, where ``objlib`` is the
  90. object library name. For example:
  91. .. code-block:: cmake
  92. add_library(... $<TARGET_OBJECTS:objlib> ...)
  93. add_executable(... $<TARGET_OBJECTS:objlib> ...)
  94. will include objlib's object files in a library and an executable
  95. along with those compiled from their own sources. Object libraries
  96. may contain only sources that compile, header files, and other files
  97. that would not affect linking of a normal library (e.g. ``.txt``).
  98. They may contain custom commands generating such sources, but not
  99. ``PRE_BUILD``, ``PRE_LINK``, or ``POST_BUILD`` commands. Some native build
  100. systems (such as Xcode) may not like targets that have only object files, so
  101. consider adding at least one real source file to any target that references
  102. ``$<TARGET_OBJECTS:objlib>``.
  103. Alias Libraries
  104. ^^^^^^^^^^^^^^^
  105. .. code-block:: cmake
  106. add_library(<name> ALIAS <target>)
  107. Creates an :ref:`Alias Target <Alias Targets>`, such that ``<name>`` can be
  108. used to refer to ``<target>`` in subsequent commands. The ``<name>`` does
  109. not appear in the generated buildsystem as a make target. The ``<target>``
  110. may not be an ``ALIAS``.
  111. An ``ALIAS`` to a non-``GLOBAL`` :ref:`Imported Target <Imported Targets>`
  112. has scope in the directory in which the alias is created and below.
  113. The :prop_tgt:`ALIAS_GLOBAL` target property can be used to check if the
  114. alias is global or not.
  115. ``ALIAS`` targets can be used as linkable targets and as targets to
  116. read properties from. They can also be tested for existence with the
  117. regular :command:`if(TARGET)` subcommand. The ``<name>`` may not be used
  118. to modify properties of ``<target>``, that is, it may not be used as the
  119. operand of :command:`set_property`, :command:`set_target_properties`,
  120. :command:`target_link_libraries` etc. An ``ALIAS`` target may not be
  121. installed or exported.
  122. Interface Libraries
  123. ^^^^^^^^^^^^^^^^^^^
  124. .. code-block:: cmake
  125. add_library(<name> INTERFACE [IMPORTED [GLOBAL]])
  126. Creates an :ref:`Interface Library <Interface Libraries>`. An ``INTERFACE``
  127. library target does not directly create build output, though it may
  128. have properties set on it and it may be installed, exported and
  129. imported. Typically the ``INTERFACE_*`` properties are populated on
  130. the interface target using the commands:
  131. * :command:`set_property`,
  132. * :command:`target_link_libraries(INTERFACE)`,
  133. * :command:`target_link_options(INTERFACE)`,
  134. * :command:`target_include_directories(INTERFACE)`,
  135. * :command:`target_compile_options(INTERFACE)`,
  136. * :command:`target_compile_definitions(INTERFACE)`, and
  137. * :command:`target_sources(INTERFACE)`,
  138. and then it is used as an argument to :command:`target_link_libraries`
  139. like any other target.
  140. An ``INTERFACE`` :ref:`Imported Target <Imported Targets>` may also be
  141. created with this signature. An ``IMPORTED`` library target references a
  142. library defined outside the project. The target name has scope in the
  143. directory in which it is created and below, but the ``GLOBAL`` option
  144. extends visibility. It may be referenced like any target built within
  145. the project. ``IMPORTED`` libraries are useful for convenient reference
  146. from commands like :command:`target_link_libraries`.