target_link_libraries.rst 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. target_link_libraries
  2. ---------------------
  3. .. only:: html
  4. .. contents::
  5. Specify libraries or flags to use when linking a given target and/or
  6. its dependents. :ref:`Usage requirements <Target Usage Requirements>`
  7. from linked library targets will be propagated. Usage requirements
  8. of a target's dependencies affect compilation of its own sources.
  9. Overview
  10. ^^^^^^^^
  11. This command has several signatures as detailed in subsections below.
  12. All of them have the general form
  13. .. code-block:: cmake
  14. target_link_libraries(<target> ... <item>... ...)
  15. The named ``<target>`` must have been created by a command such as
  16. :command:`add_executable` or :command:`add_library` and must not be an
  17. :ref:`ALIAS target <Alias Targets>`. If policy :policy:`CMP0079` is not
  18. set to ``NEW`` then the target must have been created in the current
  19. directory. Repeated calls for the same ``<target>`` append items in
  20. the order called.
  21. .. versionadded:: 3.13
  22. The ``<target>`` doesn't have to be defined in the same directory as the
  23. ``target_link_libraries`` call.
  24. Each ``<item>`` may be:
  25. * **A library target name**: The generated link line will have the
  26. full path to the linkable library file associated with the target.
  27. The buildsystem will have a dependency to re-link ``<target>`` if
  28. the library file changes.
  29. The named target must be created by :command:`add_library` within
  30. the project or as an :ref:`IMPORTED library <Imported Targets>`.
  31. If it is created within the project an ordering dependency will
  32. automatically be added in the build system to make sure the named
  33. library target is up-to-date before the ``<target>`` links.
  34. If an imported library has the :prop_tgt:`IMPORTED_NO_SONAME`
  35. target property set, CMake may ask the linker to search for
  36. the library instead of using the full path
  37. (e.g. ``/usr/lib/libfoo.so`` becomes ``-lfoo``).
  38. The full path to the target's artifact will be quoted/escaped for
  39. the shell automatically.
  40. * **A full path to a library file**: The generated link line will
  41. normally preserve the full path to the file. The buildsystem will
  42. have a dependency to re-link ``<target>`` if the library file changes.
  43. There are some cases where CMake may ask the linker to search for
  44. the library (e.g. ``/usr/lib/libfoo.so`` becomes ``-lfoo``), such
  45. as when a shared library is detected to have no ``SONAME`` field.
  46. See policy :policy:`CMP0060` for discussion of another case.
  47. If the library file is in a macOS framework, the ``Headers`` directory
  48. of the framework will also be processed as a
  49. :ref:`usage requirement <Target Usage Requirements>`. This has the same
  50. effect as passing the framework directory as an include directory.
  51. .. versionadded:: 3.8
  52. On :ref:`Visual Studio Generators` for VS 2010 and above, library files
  53. ending in ``.targets`` will be treated as MSBuild targets files and
  54. imported into generated project files. This is not supported by other
  55. generators.
  56. The full path to the library file will be quoted/escaped for
  57. the shell automatically.
  58. * **A plain library name**: The generated link line will ask the linker
  59. to search for the library (e.g. ``foo`` becomes ``-lfoo`` or ``foo.lib``).
  60. The library name/flag is treated as a command-line string fragment and
  61. will be used with no extra quoting or escaping.
  62. * **A link flag**: Item names starting with ``-``, but not ``-l`` or
  63. ``-framework``, are treated as linker flags. Note that such flags will
  64. be treated like any other library link item for purposes of transitive
  65. dependencies, so they are generally safe to specify only as private link
  66. items that will not propagate to dependents.
  67. Link flags specified here are inserted into the link command in the same
  68. place as the link libraries. This might not be correct, depending on
  69. the linker. Use the :prop_tgt:`LINK_OPTIONS` target property or
  70. :command:`target_link_options` command to add link
  71. flags explicitly. The flags will then be placed at the toolchain-defined
  72. flag position in the link command.
  73. .. versionadded:: 3.13
  74. :prop_tgt:`LINK_OPTIONS` target property and :command:`target_link_options`
  75. command. For earlier versions of CMake, use :prop_tgt:`LINK_FLAGS`
  76. property instead.
  77. The link flag is treated as a command-line string fragment and
  78. will be used with no extra quoting or escaping.
  79. * **A generator expression**: A ``$<...>`` :manual:`generator expression
  80. <cmake-generator-expressions(7)>` may evaluate to any of the above
  81. items or to a :ref:`semicolon-separated list <CMake Language Lists>` of them.
  82. If the ``...`` contains any ``;`` characters, e.g. after evaluation
  83. of a ``${list}`` variable, be sure to use an explicitly quoted
  84. argument ``"$<...>"`` so that this command receives it as a
  85. single ``<item>``.
  86. Additionally, a generator expression may be used as a fragment of
  87. any of the above items, e.g. ``foo$<1:_d>``.
  88. Note that generator expressions will not be used in OLD handling of
  89. policy :policy:`CMP0003` or policy :policy:`CMP0004`.
  90. * A ``debug``, ``optimized``, or ``general`` keyword immediately followed
  91. by another ``<item>``. The item following such a keyword will be used
  92. only for the corresponding build configuration. The ``debug`` keyword
  93. corresponds to the ``Debug`` configuration (or to configurations named
  94. in the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  95. The ``optimized`` keyword corresponds to all other configurations. The
  96. ``general`` keyword corresponds to all configurations, and is purely
  97. optional. Higher granularity may be achieved for per-configuration
  98. rules by creating and linking to
  99. :ref:`IMPORTED library targets <Imported Targets>`.
  100. These keywords are interpreted immediately by this command and therefore
  101. have no special meaning when produced by a generator expression.
  102. Items containing ``::``, such as ``Foo::Bar``, are assumed to be
  103. :ref:`IMPORTED <Imported Targets>` or :ref:`ALIAS <Alias Targets>` library
  104. target names and will cause an error if no such target exists.
  105. See policy :policy:`CMP0028`.
  106. See the :manual:`cmake-buildsystem(7)` manual for more on defining
  107. buildsystem properties.
  108. Libraries for a Target and/or its Dependents
  109. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  110. .. code-block:: cmake
  111. target_link_libraries(<target>
  112. <PRIVATE|PUBLIC|INTERFACE> <item>...
  113. [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
  114. The ``PUBLIC``, ``PRIVATE`` and ``INTERFACE`` keywords can be used to
  115. specify both the link dependencies and the link interface in one command.
  116. Libraries and targets following ``PUBLIC`` are linked to, and are made
  117. part of the link interface. Libraries and targets following ``PRIVATE``
  118. are linked to, but are not made part of the link interface. Libraries
  119. following ``INTERFACE`` are appended to the link interface and are not
  120. used for linking ``<target>``.
  121. Libraries for both a Target and its Dependents
  122. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  123. .. code-block:: cmake
  124. target_link_libraries(<target> <item>...)
  125. Library dependencies are transitive by default with this signature.
  126. When this target is linked into another target then the libraries
  127. linked to this target will appear on the link line for the other
  128. target too. This transitive "link interface" is stored in the
  129. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property and may be overridden
  130. by setting the property directly. When :policy:`CMP0022` is not set to
  131. ``NEW``, transitive linking is built in but may be overridden by the
  132. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Calls to other signatures
  133. of this command may set the property making any libraries linked
  134. exclusively by this signature private.
  135. Libraries for a Target and/or its Dependents (Legacy)
  136. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  137. .. code-block:: cmake
  138. target_link_libraries(<target>
  139. <LINK_PRIVATE|LINK_PUBLIC> <lib>...
  140. [<LINK_PRIVATE|LINK_PUBLIC> <lib>...]...)
  141. The ``LINK_PUBLIC`` and ``LINK_PRIVATE`` modes can be used to specify both
  142. the link dependencies and the link interface in one command.
  143. This signature is for compatibility only. Prefer the ``PUBLIC`` or
  144. ``PRIVATE`` keywords instead.
  145. Libraries and targets following ``LINK_PUBLIC`` are linked to, and are
  146. made part of the :prop_tgt:`INTERFACE_LINK_LIBRARIES`. If policy
  147. :policy:`CMP0022` is not ``NEW``, they are also made part of the
  148. :prop_tgt:`LINK_INTERFACE_LIBRARIES`. Libraries and targets following
  149. ``LINK_PRIVATE`` are linked to, but are not made part of the
  150. :prop_tgt:`INTERFACE_LINK_LIBRARIES` (or :prop_tgt:`LINK_INTERFACE_LIBRARIES`).
  151. Libraries for Dependents Only (Legacy)
  152. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  153. .. code-block:: cmake
  154. target_link_libraries(<target> LINK_INTERFACE_LIBRARIES <item>...)
  155. The ``LINK_INTERFACE_LIBRARIES`` mode appends the libraries to the
  156. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property instead of using them
  157. for linking. If policy :policy:`CMP0022` is not ``NEW``, then this mode
  158. also appends libraries to the :prop_tgt:`LINK_INTERFACE_LIBRARIES` and its
  159. per-configuration equivalent.
  160. This signature is for compatibility only. Prefer the ``INTERFACE`` mode
  161. instead.
  162. Libraries specified as ``debug`` are wrapped in a generator expression to
  163. correspond to debug builds. If policy :policy:`CMP0022` is
  164. not ``NEW``, the libraries are also appended to the
  165. :prop_tgt:`LINK_INTERFACE_LIBRARIES_DEBUG <LINK_INTERFACE_LIBRARIES_<CONFIG>>`
  166. property (or to the properties corresponding to configurations listed in
  167. the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  168. Libraries specified as ``optimized`` are appended to the
  169. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property. If policy :policy:`CMP0022`
  170. is not ``NEW``, they are also appended to the
  171. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Libraries specified as
  172. ``general`` (or without any keyword) are treated as if specified for both
  173. ``debug`` and ``optimized``.
  174. Linking Object Libraries
  175. ^^^^^^^^^^^^^^^^^^^^^^^^
  176. .. versionadded:: 3.12
  177. :ref:`Object Libraries` may be used as the ``<target>`` (first) argument
  178. of ``target_link_libraries`` to specify dependencies of their sources
  179. on other libraries. For example, the code
  180. .. code-block:: cmake
  181. add_library(A SHARED a.c)
  182. target_compile_definitions(A PUBLIC A)
  183. add_library(obj OBJECT obj.c)
  184. target_compile_definitions(obj PUBLIC OBJ)
  185. target_link_libraries(obj PUBLIC A)
  186. compiles ``obj.c`` with ``-DA -DOBJ`` and establishes usage requirements
  187. for ``obj`` that propagate to its dependents.
  188. Normal libraries and executables may link to :ref:`Object Libraries`
  189. to get their objects and usage requirements. Continuing the above
  190. example, the code
  191. .. code-block:: cmake
  192. add_library(B SHARED b.c)
  193. target_link_libraries(B PUBLIC obj)
  194. compiles ``b.c`` with ``-DA -DOBJ``, creates shared library ``B``
  195. with object files from ``b.c`` and ``obj.c``, and links ``B`` to ``A``.
  196. Furthermore, the code
  197. .. code-block:: cmake
  198. add_executable(main main.c)
  199. target_link_libraries(main B)
  200. compiles ``main.c`` with ``-DA -DOBJ`` and links executable ``main``
  201. to ``B`` and ``A``. The object library's usage requirements are
  202. propagated transitively through ``B``, but its object files are not.
  203. :ref:`Object Libraries` may "link" to other object libraries to get
  204. usage requirements, but since they do not have a link step nothing
  205. is done with their object files. Continuing from the above example,
  206. the code:
  207. .. code-block:: cmake
  208. add_library(obj2 OBJECT obj2.c)
  209. target_link_libraries(obj2 PUBLIC obj)
  210. add_executable(main2 main2.c)
  211. target_link_libraries(main2 obj2)
  212. compiles ``obj2.c`` with ``-DA -DOBJ``, creates executable ``main2``
  213. with object files from ``main2.c`` and ``obj2.c``, and links ``main2``
  214. to ``A``.
  215. In other words, when :ref:`Object Libraries` appear in a target's
  216. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property they will be
  217. treated as :ref:`Interface Libraries`, but when they appear in
  218. a target's :prop_tgt:`LINK_LIBRARIES` property their object files
  219. will be included in the link too.
  220. Cyclic Dependencies of Static Libraries
  221. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  222. The library dependency graph is normally acyclic (a DAG), but in the case
  223. of mutually-dependent ``STATIC`` libraries CMake allows the graph to
  224. contain cycles (strongly connected components). When another target links
  225. to one of the libraries, CMake repeats the entire connected component.
  226. For example, the code
  227. .. code-block:: cmake
  228. add_library(A STATIC a.c)
  229. add_library(B STATIC b.c)
  230. target_link_libraries(A B)
  231. target_link_libraries(B A)
  232. add_executable(main main.c)
  233. target_link_libraries(main A)
  234. links ``main`` to ``A B A B``. While one repetition is usually
  235. sufficient, pathological object file and symbol arrangements can require
  236. more. One may handle such cases by using the
  237. :prop_tgt:`LINK_INTERFACE_MULTIPLICITY` target property or by manually
  238. repeating the component in the last ``target_link_libraries`` call.
  239. However, if two archives are really so interdependent they should probably
  240. be combined into a single archive, perhaps by using :ref:`Object Libraries`.
  241. Creating Relocatable Packages
  242. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  243. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  244. .. include:: /include/INTERFACE_LINK_LIBRARIES_WARNING.txt