target_link_libraries.rst 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. target_link_libraries
  2. ---------------------
  3. Link a target to given libraries.
  4. ::
  5. target_link_libraries(<target> [item1 [item2 [...]]]
  6. [[debug|optimized|general] <item>] ...)
  7. Specify libraries or flags to use when linking a given target. The
  8. named ``<target>`` must have been created in the current directory by a
  9. command such as :command:`add_executable` or :command:`add_library`. The
  10. remaining arguments specify library names or flags. Repeated calls for
  11. the same ``<target>`` append items in the order called.
  12. If a library name matches that of another target in the project a
  13. dependency will automatically be added in the build system to make sure
  14. the library being linked is up-to-date before the target links. Item names
  15. starting with ``-``, but not ``-l`` or ``-framework``, are treated as
  16. linker flags. Note that such flags will be treated like any other library
  17. link item for purposes of transitive dependencies, so they are generally
  18. safe to specify only as private link items that will not propagate to
  19. dependents of ``<target>``.
  20. A ``debug``, ``optimized``, or ``general`` keyword indicates that the
  21. library immediately following it is to be used only for the
  22. corresponding build configuration. The ``debug`` keyword corresponds to
  23. the Debug configuration (or to configurations named in the
  24. :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set). The
  25. ``optimized`` keyword corresponds to all other configurations. The
  26. ``general`` keyword corresponds to all configurations, and is purely
  27. optional (assumed if omitted). Higher granularity may be achieved for
  28. per-configuration rules by creating and linking to
  29. :ref:`IMPORTED library targets <Imported Targets>`.
  30. Library dependencies are transitive by default with this signature.
  31. When this target is linked into another target then the libraries
  32. linked to this target will appear on the link line for the other
  33. target too. This transitive "link interface" is stored in the
  34. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property and may be overridden
  35. by setting the property directly. When :policy:`CMP0022` is not set to
  36. ``NEW``, transitive linking is built in but may be overridden by the
  37. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Calls to other signatures
  38. of this command may set the property making any libraries linked
  39. exclusively by this signature private.
  40. CMake will also propagate :ref:`usage requirements <Target Usage Requirements>`
  41. from linked library targets. Usage requirements of dependencies affect
  42. compilation of sources in the ``<target>``.
  43. If an ``<item>`` is a library in a Mac OX framework, the ``Headers``
  44. directory 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. --------------------------------------------------------------------------
  48. ::
  49. target_link_libraries(<target>
  50. <PRIVATE|PUBLIC|INTERFACE> <lib> ...
  51. [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...])
  52. The ``PUBLIC``, ``PRIVATE`` and ``INTERFACE`` keywords can be used to
  53. specify both the link dependencies and the link interface in one command.
  54. Libraries and targets following ``PUBLIC`` are linked to, and are made
  55. part of the link interface. Libraries and targets following ``PRIVATE``
  56. are linked to, but are not made part of the link interface. Libraries
  57. following ``INTERFACE`` are appended to the link interface and are not
  58. used for linking ``<target>``.
  59. --------------------------------------------------------------------------
  60. ::
  61. target_link_libraries(<target> LINK_INTERFACE_LIBRARIES
  62. [[debug|optimized|general] <lib>] ...)
  63. The ``LINK_INTERFACE_LIBRARIES`` mode appends the libraries to the
  64. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property instead of using them
  65. for linking. If policy :policy:`CMP0022` is not ``NEW``, then this mode
  66. also appends libraries to the :prop_tgt:`LINK_INTERFACE_LIBRARIES` and its
  67. per-configuration equivalent.
  68. This signature is for compatibility only. Prefer the ``INTERFACE`` mode
  69. instead.
  70. Libraries specified as ``debug`` are wrapped in a generator expression to
  71. correspond to debug builds. If policy :policy:`CMP0022` is
  72. not ``NEW``, the libraries are also appended to the
  73. :prop_tgt:`LINK_INTERFACE_LIBRARIES_DEBUG <LINK_INTERFACE_LIBRARIES_<CONFIG>>`
  74. property (or to the properties corresponding to configurations listed in
  75. the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  76. Libraries specified as ``optimized`` are appended to the
  77. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property. If policy :policy:`CMP0022`
  78. is not ``NEW``, they are also appended to the
  79. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Libraries specified as
  80. ``general`` (or without any keyword) are treated as if specified for both
  81. ``debug`` and ``optimized``.
  82. --------------------------------------------------------------------------
  83. ::
  84. target_link_libraries(<target>
  85. <LINK_PRIVATE|LINK_PUBLIC>
  86. [[debug|optimized|general] <lib>] ...
  87. [<LINK_PRIVATE|LINK_PUBLIC>
  88. [[debug|optimized|general] <lib>] ...])
  89. The ``LINK_PUBLIC`` and ``LINK_PRIVATE`` modes can be used to specify both
  90. the link dependencies and the link interface in one command.
  91. This signature is for compatibility only. Prefer the ``PUBLIC`` or
  92. ``PRIVATE`` keywords instead.
  93. Libraries and targets following ``LINK_PUBLIC`` are linked to, and are
  94. made part of the :prop_tgt:`INTERFACE_LINK_LIBRARIES`. If policy
  95. :policy:`CMP0022` is not ``NEW``, they are also made part of the
  96. :prop_tgt:`LINK_INTERFACE_LIBRARIES`. Libraries and targets following
  97. ``LINK_PRIVATE`` are linked to, but are not made part of the
  98. :prop_tgt:`INTERFACE_LINK_LIBRARIES` (or :prop_tgt:`LINK_INTERFACE_LIBRARIES`).
  99. The library dependency graph is normally acyclic (a DAG), but in the case
  100. of mutually-dependent ``STATIC`` libraries CMake allows the graph to
  101. contain cycles (strongly connected components). When another target links
  102. to one of the libraries, CMake repeats the entire connected component.
  103. For example, the code
  104. .. code-block:: cmake
  105. add_library(A STATIC a.c)
  106. add_library(B STATIC b.c)
  107. target_link_libraries(A B)
  108. target_link_libraries(B A)
  109. add_executable(main main.c)
  110. target_link_libraries(main A)
  111. links ``main`` to ``A B A B``. While one repetition is usually
  112. sufficient, pathological object file and symbol arrangements can require
  113. more. One may handle such cases by manually repeating the component in
  114. the last ``target_link_libraries`` call. However, if two archives are
  115. really so interdependent they should probably be combined into a single
  116. archive.
  117. Arguments to target_link_libraries may use "generator expressions"
  118. with the syntax ``$<...>``. Note however, that generator expressions
  119. will not be used in OLD handling of :policy:`CMP0003` or :policy:`CMP0004`.
  120. See the :manual:`cmake-generator-expressions(7)` manual for available
  121. expressions. See the :manual:`cmake-buildsystem(7)` manual for more on
  122. defining buildsystem properties.
  123. Creating Relocatable Packages
  124. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  125. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  126. .. include:: /include/INTERFACE_LINK_LIBRARIES_WARNING.txt