target_link_libraries.rst 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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. .. |INTERFACE_PROPERTY_LINK| replace:: :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  44. .. include:: /include/INTERFACE_LINK_LIBRARIES_WARNING.txt
  45. If an ``<item>`` is a library in a Mac OX framework, the ``Headers``
  46. directory of the framework will also be processed as a
  47. :ref:`usage requirement <Target Usage Requirements>`. This has the same
  48. effect as passing the framework directory as an include directory.
  49. --------------------------------------------------------------------------
  50. ::
  51. target_link_libraries(<target>
  52. <PRIVATE|PUBLIC|INTERFACE> <lib> ...
  53. [<PRIVATE|PUBLIC|INTERFACE> <lib> ... ] ...])
  54. The ``PUBLIC``, ``PRIVATE`` and ``INTERFACE`` keywords can be used to
  55. specify both the link dependencies and the link interface in one command.
  56. Libraries and targets following ``PUBLIC`` are linked to, and are made
  57. part of the link interface. Libraries and targets following ``PRIVATE``
  58. are linked to, but are not made part of the link interface. Libraries
  59. following ``INTERFACE`` are appended to the link interface and are not
  60. used for linking ``<target>``.
  61. --------------------------------------------------------------------------
  62. ::
  63. target_link_libraries(<target> LINK_INTERFACE_LIBRARIES
  64. [[debug|optimized|general] <lib>] ...)
  65. The ``LINK_INTERFACE_LIBRARIES`` mode appends the libraries to the
  66. :prop_tgt:`INTERFACE_LINK_LIBRARIES` target property instead of using them
  67. for linking. If policy :policy:`CMP0022` is not ``NEW``, then this mode
  68. also appends libraries to the :prop_tgt:`LINK_INTERFACE_LIBRARIES` and its
  69. per-configuration equivalent.
  70. This signature is for compatibility only. Prefer the ``INTERFACE`` mode
  71. instead.
  72. Libraries specified as ``debug`` are wrapped in a generator expression to
  73. correspond to debug builds. If policy :policy:`CMP0022` is
  74. not ``NEW``, the libraries are also appended to the
  75. :prop_tgt:`LINK_INTERFACE_LIBRARIES_DEBUG <LINK_INTERFACE_LIBRARIES_<CONFIG>>`
  76. property (or to the properties corresponding to configurations listed in
  77. the :prop_gbl:`DEBUG_CONFIGURATIONS` global property if it is set).
  78. Libraries specified as ``optimized`` are appended to the
  79. :prop_tgt:`INTERFACE_LINK_LIBRARIES` property. If policy :policy:`CMP0022`
  80. is not ``NEW``, they are also appended to the
  81. :prop_tgt:`LINK_INTERFACE_LIBRARIES` property. Libraries specified as
  82. ``general`` (or without any keyword) are treated as if specified for both
  83. ``debug`` and ``optimized``.
  84. --------------------------------------------------------------------------
  85. ::
  86. target_link_libraries(<target>
  87. <LINK_PRIVATE|LINK_PUBLIC>
  88. [[debug|optimized|general] <lib>] ...
  89. [<LINK_PRIVATE|LINK_PUBLIC>
  90. [[debug|optimized|general] <lib>] ...])
  91. The ``LINK_PUBLIC`` and ``LINK_PRIVATE`` modes can be used to specify both
  92. the link dependencies and the link interface in one command.
  93. This signature is for compatibility only. Prefer the ``PUBLIC`` or
  94. ``PRIVATE`` keywords instead.
  95. Libraries and targets following ``LINK_PUBLIC`` are linked to, and are
  96. made part of the :prop_tgt:`INTERFACE_LINK_LIBRARIES`. If policy
  97. :policy:`CMP0022` is not ``NEW``, they are also made part of the
  98. :prop_tgt:`LINK_INTERFACE_LIBRARIES`. Libraries and targets following
  99. ``LINK_PRIVATE`` are linked to, but are not made part of the
  100. :prop_tgt:`INTERFACE_LINK_LIBRARIES` (or :prop_tgt:`LINK_INTERFACE_LIBRARIES`).
  101. The library dependency graph is normally acyclic (a DAG), but in the case
  102. of mutually-dependent ``STATIC`` libraries CMake allows the graph to
  103. contain cycles (strongly connected components). When another target links
  104. to one of the libraries, CMake repeats the entire connected component.
  105. For example, the code
  106. .. code-block:: cmake
  107. add_library(A STATIC a.c)
  108. add_library(B STATIC b.c)
  109. target_link_libraries(A B)
  110. target_link_libraries(B A)
  111. add_executable(main main.c)
  112. target_link_libraries(main A)
  113. links ``main`` to ``A B A B``. While one repetition is usually
  114. sufficient, pathological object file and symbol arrangements can require
  115. more. One may handle such cases by manually repeating the component in
  116. the last ``target_link_libraries`` call. However, if two archives are
  117. really so interdependent they should probably be combined into a single
  118. archive.
  119. Arguments to target_link_libraries may use "generator expressions"
  120. with the syntax ``$<...>``. Note however, that generator expressions
  121. will not be used in OLD handling of :policy:`CMP0003` or :policy:`CMP0004`.
  122. See the :manual:`cmake-generator-expressions(7)` manual for available
  123. expressions. See the :manual:`cmake-buildsystem(7)` manual for more on
  124. defining buildsystem properties.