target_link_libraries.rst 6.8 KB

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