target_link_libraries.rst 8.7 KB

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