target_link_libraries.rst 6.5 KB

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