target_link_libraries.rst 13 KB

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