target_link_libraries.rst 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  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. In CMake versions prior to 4.0, see policy :policy:`CMP0060` for
  47. discussion of another case.
  48. If the library file is in a macOS framework, the ``Headers`` directory
  49. of the framework will also be processed as a
  50. :ref:`usage requirement <Target Usage Requirements>`. This has the same
  51. effect as passing the framework directory as an include directory.
  52. .. versionadded:: 3.28
  53. The library file may point to a ``.xcframework`` folder on Apple platforms.
  54. If it does, the target will get the selected library's ``Headers``
  55. directory as a usage requirement.
  56. .. versionadded:: 3.8
  57. On :ref:`Visual Studio Generators` for VS 2010 and above, library files
  58. ending in ``.targets`` will be treated as MSBuild targets files and
  59. imported into generated project files. This is not supported by other
  60. generators.
  61. The full path to the library file will be quoted/escaped for
  62. the shell automatically.
  63. * **A plain library name**: The generated link line will ask the linker
  64. to search for the library (e.g. ``foo`` becomes ``-lfoo`` or ``foo.lib``).
  65. The library name/flag is treated as a command-line string fragment and
  66. will be used with no extra quoting or escaping.
  67. * **A link flag**: Item names starting with ``-``, but not ``-l`` or
  68. ``-framework``, are treated as linker flags. Note that such flags will
  69. be treated like any other library link item for purposes of transitive
  70. dependencies, so they are generally safe to specify only as private link
  71. items that will not propagate to dependents.
  72. Link flags specified here are inserted into the link command in the same
  73. place as the link libraries. This might not be correct, depending on
  74. the linker. Use the :prop_tgt:`LINK_OPTIONS` target property or
  75. :command:`target_link_options` command to add link
  76. flags explicitly. The flags will then be placed at the toolchain-defined
  77. flag position in the link command.
  78. .. versionadded:: 3.13
  79. :prop_tgt:`LINK_OPTIONS` target property and :command:`target_link_options`
  80. command. For earlier versions of CMake, use :prop_tgt:`LINK_FLAGS`
  81. property instead.
  82. The link flag is treated as a command-line string fragment and
  83. will be used with no extra quoting or escaping.
  84. * **A generator expression**: A ``$<...>`` :manual:`generator expression
  85. <cmake-generator-expressions(7)>` may evaluate to any of the above
  86. items or to a :ref:`semicolon-separated list <CMake Language Lists>` of them.
  87. If the ``...`` contains any ``;`` characters, e.g. after evaluation
  88. of a ``${list}`` variable, be sure to use an explicitly quoted
  89. argument ``"$<...>"`` so that this command receives it as a
  90. single ``<item>``.
  91. Additionally, a generator expression may be used as a fragment of
  92. any of the above items, e.g. ``foo$<1:_d>``.
  93. Note that generator expressions will not be used in OLD handling of
  94. policy :policy:`CMP0003` or policy :policy:`CMP0004`.
  95. * A ``debug``, ``optimized``, or ``general`` keyword immediately followed
  96. by another ``<item>``. The item following such a keyword will be used
  97. only for the corresponding build configuration. The ``debug`` keyword
  98. corresponds to the ``Debug`` configuration (or to configurations named
  99. in the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  100. The ``optimized`` keyword corresponds to all other configurations. The
  101. ``general`` keyword corresponds to all configurations, and is purely
  102. optional. Higher granularity may be achieved for per-configuration
  103. rules by creating and linking to
  104. :ref:`IMPORTED library targets <Imported Targets>`.
  105. These keywords are interpreted immediately by this command and therefore
  106. have no special meaning when produced by a generator expression.
  107. Items containing ``::``, such as ``Foo::Bar``, are assumed to be
  108. :ref:`IMPORTED <Imported Targets>` or :ref:`ALIAS <Alias Targets>` library
  109. target names and will cause an error if no such target exists.
  110. See policy :policy:`CMP0028`.
  111. See the :variable:`CMAKE_LINK_LIBRARIES_STRATEGY` variable and
  112. corresponding :prop_tgt:`LINK_LIBRARIES_STRATEGY` target property
  113. for details on how CMake orders direct link dependencies on linker
  114. command lines.
  115. See the :manual:`cmake-buildsystem(7)` manual for more on defining
  116. buildsystem properties.
  117. .. include:: ../command/LINK_LIBRARIES_LINKER.txt
  118. Libraries for a Target and/or its Dependents
  119. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  120. .. code-block:: cmake
  121. target_link_libraries(<target>
  122. <PRIVATE|PUBLIC|INTERFACE> <item>...
  123. [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
  124. The ``PUBLIC``, ``PRIVATE`` and ``INTERFACE``
  125. :ref:`scope <Target Command Scope>` keywords can be used to
  126. specify both the link dependencies and the link interface in one command.
  127. Libraries and targets following ``PUBLIC`` are linked to, and are made
  128. part of the link interface. Libraries and targets following ``PRIVATE``
  129. are linked to, but are not made part of the link interface. Libraries
  130. following ``INTERFACE`` are appended to the link interface and are not
  131. used for linking ``<target>``.
  132. Libraries for both a Target and its Dependents
  133. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  134. .. code-block:: cmake
  135. target_link_libraries(<target> <item>...)
  136. Library dependencies are transitive by default with this signature.
  137. When this target is linked into another target then the libraries
  138. linked to this target will appear on the link line for the other
  139. target too. This transitive "link interface" is stored in the
  140. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property and may be overridden
  141. by setting the property directly. When :policy:`CMP0022` is not set to
  142. ``NEW``, transitive linking is built in but may be overridden by the
  143. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Calls to other signatures
  144. of this command may set the property making any libraries linked
  145. exclusively by this signature private.
  146. Libraries for a Target and/or its Dependents (Legacy)
  147. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  148. .. code-block:: cmake
  149. target_link_libraries(<target>
  150. <LINK_PRIVATE|LINK_PUBLIC> <lib>...
  151. [<LINK_PRIVATE|LINK_PUBLIC> <lib>...]...)
  152. The ``LINK_PUBLIC`` and ``LINK_PRIVATE`` modes can be used to specify both
  153. the link dependencies and the link interface in one command.
  154. This signature is for compatibility only. Prefer the ``PUBLIC`` or
  155. ``PRIVATE`` keywords instead.
  156. Libraries and targets following ``LINK_PUBLIC`` are linked to, and are
  157. made part of the :prop_tgt:`INTERFACE_LINK_LIBRARIES`. If policy
  158. :policy:`CMP0022` is not ``NEW``, they are also made part of the
  159. :prop_tgt:`LINK_INTERFACE_LIBRARIES`. Libraries and targets following
  160. ``LINK_PRIVATE`` are linked to, but are not made part of the
  161. :prop_tgt:`INTERFACE_LINK_LIBRARIES` (or :prop_tgt:`LINK_INTERFACE_LIBRARIES`).
  162. Libraries for Dependents Only (Legacy)
  163. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  164. .. code-block:: cmake
  165. target_link_libraries(<target> LINK_INTERFACE_LIBRARIES <item>...)
  166. The ``LINK_INTERFACE_LIBRARIES`` mode appends the libraries to the
  167. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property instead of using them
  168. for linking. If policy :policy:`CMP0022` is not ``NEW``, then this mode
  169. also appends libraries to the :prop_tgt:`LINK_INTERFACE_LIBRARIES` and its
  170. per-configuration equivalent.
  171. This signature is for compatibility only. Prefer the ``INTERFACE`` mode
  172. instead.
  173. Libraries specified as ``debug`` are wrapped in a generator expression to
  174. correspond to debug builds. If policy :policy:`CMP0022` is
  175. not ``NEW``, the libraries are also appended to the
  176. :prop_tgt:`LINK_INTERFACE_LIBRARIES_DEBUG <LINK_INTERFACE_LIBRARIES_<CONFIG>>`
  177. property (or to the properties corresponding to configurations listed in
  178. the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  179. Libraries specified as ``optimized`` are appended to the
  180. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property. If policy :policy:`CMP0022`
  181. is not ``NEW``, they are also appended to the
  182. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Libraries specified as
  183. ``general`` (or without any keyword) are treated as if specified for both
  184. ``debug`` and ``optimized``.
  185. .. _`Linking Object Libraries`:
  186. Linking Object Libraries
  187. ^^^^^^^^^^^^^^^^^^^^^^^^
  188. .. versionadded:: 3.12
  189. :ref:`Object Libraries` may be used as the ``<target>`` (first) argument
  190. of ``target_link_libraries`` to specify dependencies of their sources
  191. on other libraries. For example, the code
  192. .. code-block:: cmake
  193. add_library(A SHARED a.c)
  194. target_compile_definitions(A PUBLIC A)
  195. add_library(obj OBJECT obj.c)
  196. target_compile_definitions(obj PUBLIC OBJ)
  197. target_link_libraries(obj PUBLIC A)
  198. compiles ``obj.c`` with ``-DA -DOBJ`` and establishes usage requirements
  199. for ``obj`` that propagate to its dependents.
  200. Normal libraries and executables may link to :ref:`Object Libraries`
  201. to get their objects and usage requirements. Continuing the above
  202. example, the code
  203. .. code-block:: cmake
  204. add_library(B SHARED b.c)
  205. target_link_libraries(B PUBLIC obj)
  206. compiles ``b.c`` with ``-DA -DOBJ``, creates shared library ``B``
  207. with object files from ``b.c`` and ``obj.c``, and links ``B`` to ``A``.
  208. Furthermore, the code
  209. .. code-block:: cmake
  210. add_executable(main main.c)
  211. target_link_libraries(main B)
  212. compiles ``main.c`` with ``-DA -DOBJ`` and links executable ``main``
  213. to ``B`` and ``A``. The object library's usage requirements are
  214. propagated transitively through ``B``, but its object files are not.
  215. :ref:`Object Libraries` may "link" to other object libraries to get
  216. usage requirements, but since they do not have a link step nothing
  217. is done with their object files. Continuing from the above example,
  218. the code:
  219. .. code-block:: cmake
  220. add_library(obj2 OBJECT obj2.c)
  221. target_link_libraries(obj2 PUBLIC obj)
  222. add_executable(main2 main2.c)
  223. target_link_libraries(main2 obj2)
  224. compiles ``obj2.c`` with ``-DA -DOBJ``, creates executable ``main2``
  225. with object files from ``main2.c`` and ``obj2.c``, and links ``main2``
  226. to ``A``.
  227. In other words, when :ref:`Object Libraries` appear in a target's
  228. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property they will be
  229. treated as :ref:`Interface Libraries`, but when they appear in
  230. a target's :prop_tgt:`LINK_LIBRARIES` property their object files
  231. will be included in the link too.
  232. .. _`Linking Object Libraries via $<TARGET_OBJECTS>`:
  233. Linking Object Libraries via ``$<TARGET_OBJECTS>``
  234. """"""""""""""""""""""""""""""""""""""""""""""""""
  235. .. versionadded:: 3.21
  236. The object files associated with an object library may be referenced
  237. by the :genex:`$<TARGET_OBJECTS>` generator expression. Such object
  238. files are placed on the link line *before* all libraries, regardless
  239. of their relative order. Additionally, an ordering dependency will be
  240. added to the build system to make sure the object library is up-to-date
  241. before the dependent target links. For example, the code
  242. .. code-block:: cmake
  243. add_library(obj3 OBJECT obj3.c)
  244. target_compile_definitions(obj3 PUBLIC OBJ3)
  245. add_executable(main3 main3.c)
  246. target_link_libraries(main3 PRIVATE a3 $<TARGET_OBJECTS:obj3> b3)
  247. links executable ``main3`` with object files from ``main3.c``
  248. and ``obj3.c`` followed by the ``a3`` and ``b3`` libraries.
  249. ``main3.c`` is *not* compiled with usage requirements from ``obj3``,
  250. such as ``-DOBJ3``.
  251. This approach can be used to achieve transitive inclusion of object
  252. files in link lines as usage requirements. Continuing the above
  253. example, the code
  254. .. code-block:: cmake
  255. add_library(iface_obj3 INTERFACE)
  256. target_link_libraries(iface_obj3 INTERFACE obj3 $<TARGET_OBJECTS:obj3>)
  257. creates an interface library ``iface_obj3`` that forwards the ``obj3``
  258. usage requirements and adds the ``obj3`` object files to dependents'
  259. link lines. The code
  260. .. code-block:: cmake
  261. add_executable(use_obj3 use_obj3.c)
  262. target_link_libraries(use_obj3 PRIVATE iface_obj3)
  263. compiles ``use_obj3.c`` with ``-DOBJ3`` and links executable ``use_obj3``
  264. with object files from ``use_obj3.c`` and ``obj3.c``.
  265. This also works transitively through a static library. Since a static
  266. library does not link, it does not consume the object files from
  267. object libraries referenced this way. Instead, the object files
  268. become transitive link dependencies of the static library.
  269. Continuing the above example, the code
  270. .. code-block:: cmake
  271. add_library(static3 STATIC static3.c)
  272. target_link_libraries(static3 PRIVATE iface_obj3)
  273. add_executable(use_static3 use_static3.c)
  274. target_link_libraries(use_static3 PRIVATE static3)
  275. compiles ``static3.c`` with ``-DOBJ3`` and creates ``libstatic3.a``
  276. using only its own object file. ``use_static3.c`` is compiled *without*
  277. ``-DOBJ3`` because the usage requirement is not transitive through
  278. the private dependency of ``static3``. However, the link dependencies
  279. of ``static3`` are propagated, including the ``iface_obj3`` reference
  280. to ``$<TARGET_OBJECTS:obj3>``. The ``use_static3`` executable is
  281. created with object files from ``use_static3.c`` and ``obj3.c``, and
  282. linked to library ``libstatic3.a``.
  283. When using this approach, it is the project's responsibility to avoid
  284. linking multiple dependent binaries to ``iface_obj3``, because they will
  285. all get the ``obj3`` object files on their link lines.
  286. .. note::
  287. Referencing :genex:`$<TARGET_OBJECTS>` in ``target_link_libraries``
  288. calls worked in versions of CMake prior to 3.21 for some cases,
  289. but was not fully supported:
  290. * It did not place the object files before libraries on link lines.
  291. * It did not add an ordering dependency on the object library.
  292. * It did not work in Xcode with multiple architectures.
  293. Cyclic Dependencies of Static Libraries
  294. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  295. The library dependency graph is normally acyclic (a DAG), but in the case
  296. of mutually-dependent ``STATIC`` libraries CMake allows the graph to
  297. contain cycles (strongly connected components). When another target links
  298. to one of the libraries, CMake repeats the entire connected component.
  299. For example, the code
  300. .. code-block:: cmake
  301. add_library(A STATIC a.c)
  302. add_library(B STATIC b.c)
  303. target_link_libraries(A B)
  304. target_link_libraries(B A)
  305. add_executable(main main.c)
  306. target_link_libraries(main A)
  307. links ``main`` to ``A B A B``. While one repetition is usually
  308. sufficient, pathological object file and symbol arrangements can require
  309. more. One may handle such cases by using the
  310. :prop_tgt:`LINK_INTERFACE_MULTIPLICITY` target property or by manually
  311. repeating the component in the last ``target_link_libraries`` call.
  312. However, if two archives are really so interdependent they should probably
  313. be combined into a single archive, perhaps by using :ref:`Object Libraries`.
  314. Creating Relocatable Packages
  315. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  316. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  317. .. include:: /include/INTERFACE_LINK_LIBRARIES_WARNING.txt
  318. See Also
  319. ^^^^^^^^
  320. * :command:`target_compile_definitions`
  321. * :command:`target_compile_features`
  322. * :command:`target_compile_options`
  323. * :command:`target_include_directories`
  324. * :command:`target_link_directories`
  325. * :command:`target_link_options`
  326. * :command:`target_precompile_headers`
  327. * :command:`target_sources`