target_link_libraries.rst 6.3 KB

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