target_link_libraries.rst 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 in the current directory by
  15. a command such as :command:`add_executable` or :command:`add_library` and
  16. must not be an :ref:`ALIAS target <Alias Targets>`.
  17. Repeated calls for the same ``<target>`` append items in the order called.
  18. Each ``<item>`` may be:
  19. * **A library target name**: The generated link line will have the
  20. full path to the linkable library file associated with the target.
  21. The buildsystem will have a dependency to re-link ``<target>`` if
  22. the library file changes.
  23. The named target must be created by :command:`add_library` within
  24. the project or as an :ref:`IMPORTED library <Imported Targets>`.
  25. If it is created within the project an ordering dependency will
  26. automatically be added in the build system to make sure the named
  27. library target is up-to-date before the ``<target>`` links.
  28. If an imported library has the :prop_tgt:`IMPORTED_NO_SONAME`
  29. target property set, CMake may ask the linker to search for
  30. the library instead of using the full path
  31. (e.g. ``/usr/lib/libfoo.so`` becomes ``-lfoo``).
  32. * **A full path to a library file**: The generated link line will
  33. normally preserve the full path to the file. The buildsystem will
  34. have a dependency to re-link ``<target>`` if the library file changes.
  35. There are some cases where CMake may ask the linker to search for
  36. the library (e.g. ``/usr/lib/libfoo.so`` becomes ``-lfoo``), such
  37. as when a shared library is detected to have no ``SONAME`` field.
  38. See policy :policy:`CMP0060` for discussion of another case.
  39. If the library file is in a Mac OSX framework, the ``Headers`` directory
  40. of the framework will also be processed as a
  41. :ref:`usage requirement <Target Usage Requirements>`. This has the same
  42. effect as passing the framework directory as an include directory.
  43. On :ref:`Visual Studio Generators` for VS 2010 and above, library files
  44. ending in ``.targets`` will be treated as MSBuild targets files and
  45. imported into generated project files. This is not supported by other
  46. generators.
  47. * **A plain library name**: The generated link line will ask the linker
  48. to search for the library (e.g. ``foo`` becomes ``-lfoo`` or ``foo.lib``).
  49. * **A link flag**: Item names starting with ``-``, but not ``-l`` or
  50. ``-framework``, are treated as linker flags. Note that such flags will
  51. be treated like any other library link item for purposes of transitive
  52. dependencies, so they are generally safe to specify only as private link
  53. items that will not propagate to dependents.
  54. Link flags specified here are inserted into the link command in the same
  55. place as the link libraries. This might not be correct, depending on
  56. the linker. Use the :prop_tgt:`LINK_FLAGS` target property to add link
  57. flags explicitly. The flags will then be placed at the toolchain-defined
  58. flag position in the link command.
  59. * A ``debug``, ``optimized``, or ``general`` keyword immediately followed
  60. by another ``<item>``. The item following such a keyword will be used
  61. only for the corresponding build configuration. The ``debug`` keyword
  62. corresponds to the ``Debug`` configuration (or to configurations named
  63. in the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  64. The ``optimized`` keyword corresponds to all other configurations. The
  65. ``general`` keyword corresponds to all configurations, and is purely
  66. optional. Higher granularity may be achieved for per-configuration
  67. rules by creating and linking to
  68. :ref:`IMPORTED library targets <Imported Targets>`.
  69. Items containing ``::``, such as ``Foo::Bar``, are assumed to be
  70. :ref:`IMPORTED <Imported Targets>` or :ref:`ALIAS <Alias Targets>` library
  71. target names and will cause an error if no such target exists.
  72. See policy :policy:`CMP0028`.
  73. Arguments to ``target_link_libraries`` may use "generator expressions"
  74. with the syntax ``$<...>``. Note however, that generator expressions
  75. will not be used in OLD handling of :policy:`CMP0003` or :policy:`CMP0004`.
  76. See the :manual:`cmake-generator-expressions(7)` manual for available
  77. expressions. See the :manual:`cmake-buildsystem(7)` manual for more on
  78. defining buildsystem properties.
  79. Libraries for a Target and/or its Dependents
  80. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  81. ::
  82. target_link_libraries(<target>
  83. <PRIVATE|PUBLIC|INTERFACE> <item>...
  84. [<PRIVATE|PUBLIC|INTERFACE> <item>...]...)
  85. The ``PUBLIC``, ``PRIVATE`` and ``INTERFACE`` keywords can be used to
  86. specify both the link dependencies and the link interface in one command.
  87. Libraries and targets following ``PUBLIC`` are linked to, and are made
  88. part of the link interface. Libraries and targets following ``PRIVATE``
  89. are linked to, but are not made part of the link interface. Libraries
  90. following ``INTERFACE`` are appended to the link interface and are not
  91. used for linking ``<target>``.
  92. Libraries for both a Target and its Dependents
  93. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  94. ::
  95. target_link_libraries(<target> <item>...)
  96. Library dependencies are transitive by default with this signature.
  97. When this target is linked into another target then the libraries
  98. linked to this target will appear on the link line for the other
  99. target too. This transitive "link interface" is stored in the
  100. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property and may be overridden
  101. by setting the property directly. When :policy:`CMP0022` is not set to
  102. ``NEW``, transitive linking is built in but may be overridden by the
  103. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Calls to other signatures
  104. of this command may set the property making any libraries linked
  105. exclusively by this signature private.
  106. Libraries for a Target and/or its Dependents (Legacy)
  107. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  108. ::
  109. target_link_libraries(<target>
  110. <LINK_PRIVATE|LINK_PUBLIC> <lib>...
  111. [<LINK_PRIVATE|LINK_PUBLIC> <lib>...]...)
  112. The ``LINK_PUBLIC`` and ``LINK_PRIVATE`` modes can be used to specify both
  113. the link dependencies and the link interface in one command.
  114. This signature is for compatibility only. Prefer the ``PUBLIC`` or
  115. ``PRIVATE`` keywords instead.
  116. Libraries and targets following ``LINK_PUBLIC`` are linked to, and are
  117. made part of the :prop_tgt:`INTERFACE_LINK_LIBRARIES`. If policy
  118. :policy:`CMP0022` is not ``NEW``, they are also made part of the
  119. :prop_tgt:`LINK_INTERFACE_LIBRARIES`. Libraries and targets following
  120. ``LINK_PRIVATE`` are linked to, but are not made part of the
  121. :prop_tgt:`INTERFACE_LINK_LIBRARIES` (or :prop_tgt:`LINK_INTERFACE_LIBRARIES`).
  122. Libraries for Dependents Only (Legacy)
  123. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  124. ::
  125. target_link_libraries(<target> LINK_INTERFACE_LIBRARIES <item>...)
  126. The ``LINK_INTERFACE_LIBRARIES`` mode appends the libraries to the
  127. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property instead of using them
  128. for linking. If policy :policy:`CMP0022` is not ``NEW``, then this mode
  129. also appends libraries to the :prop_tgt:`LINK_INTERFACE_LIBRARIES` and its
  130. per-configuration equivalent.
  131. This signature is for compatibility only. Prefer the ``INTERFACE`` mode
  132. instead.
  133. Libraries specified as ``debug`` are wrapped in a generator expression to
  134. correspond to debug builds. If policy :policy:`CMP0022` is
  135. not ``NEW``, the libraries are also appended to the
  136. :prop_tgt:`LINK_INTERFACE_LIBRARIES_DEBUG <LINK_INTERFACE_LIBRARIES_<CONFIG>>`
  137. property (or to the properties corresponding to configurations listed in
  138. the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  139. Libraries specified as ``optimized`` are appended to the
  140. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property. If policy :policy:`CMP0022`
  141. is not ``NEW``, they are also appended to the
  142. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Libraries specified as
  143. ``general`` (or without any keyword) are treated as if specified for both
  144. ``debug`` and ``optimized``.
  145. Cyclic Dependencies of Static Libraries
  146. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  147. The library dependency graph is normally acyclic (a DAG), but in the case
  148. of mutually-dependent ``STATIC`` libraries CMake allows the graph to
  149. contain cycles (strongly connected components). When another target links
  150. to one of the libraries, CMake repeats the entire connected component.
  151. For example, the code
  152. .. code-block:: cmake
  153. add_library(A STATIC a.c)
  154. add_library(B STATIC b.c)
  155. target_link_libraries(A B)
  156. target_link_libraries(B A)
  157. add_executable(main main.c)
  158. target_link_libraries(main A)
  159. links ``main`` to ``A B A B``. While one repetition is usually
  160. sufficient, pathological object file and symbol arrangements can require
  161. more. One may handle such cases by using the
  162. :prop_tgt:`LINK_INTERFACE_MULTIPLICITY` target property or by manually
  163. repeating the component in the last ``target_link_libraries`` call.
  164. However, if two archives are really so interdependent they should probably
  165. be combined into a single archive, perhaps by using :ref:`Object Libraries`.
  166. Creating Relocatable Packages
  167. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  168. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  169. .. include:: /include/INTERFACE_LINK_LIBRARIES_WARNING.txt