1
0

cmake-buildsystem.7.rst 58 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550
  1. .. cmake-manual-description: CMake Buildsystem Reference
  2. cmake-buildsystem(7)
  3. ********************
  4. .. only:: html
  5. .. contents::
  6. Introduction
  7. ============
  8. A CMake-based buildsystem is organized as a set of high-level logical
  9. targets. Each target corresponds to an executable or library, or
  10. is a custom target containing custom commands. Dependencies between the
  11. targets are expressed in the buildsystem to determine the build order
  12. and the rules for regeneration in response to change.
  13. Binary Targets
  14. ==============
  15. Executables and libraries are defined using the :command:`add_executable`
  16. and :command:`add_library` commands. The resulting binary files have
  17. appropriate :prop_tgt:`PREFIX`, :prop_tgt:`SUFFIX` and extensions for the
  18. platform targeted. Dependencies between binary targets are expressed using
  19. the :command:`target_link_libraries` command:
  20. .. code-block:: cmake
  21. add_library(archive archive.cpp zip.cpp lzma.cpp)
  22. add_executable(zipapp zipapp.cpp)
  23. target_link_libraries(zipapp archive)
  24. ``archive`` is defined as a ``STATIC`` library -- an archive containing objects
  25. compiled from ``archive.cpp``, ``zip.cpp``, and ``lzma.cpp``. ``zipapp``
  26. is defined as an executable formed by compiling and linking ``zipapp.cpp``.
  27. When linking the ``zipapp`` executable, the ``archive`` static library is
  28. linked in.
  29. .. _`Executables`:
  30. Executables
  31. -----------
  32. Executables are binaries created by linking object files together,
  33. one of which contains a program entry point, e.g., ``main``.
  34. The :command:`add_executable` command defines an executable target:
  35. .. code-block:: cmake
  36. add_executable(mytool mytool.cpp)
  37. CMake generates build rules to compile the source files into object
  38. files and link them into an executable.
  39. Link dependencies of executables may be specified using the
  40. :command:`target_link_libraries` command. Linkers start with the
  41. object files compiled from the executable's own source files, and
  42. then resolve remaining symbol dependencies by searching linked libraries.
  43. Commands such as :command:`add_custom_command`, which generates rules to be
  44. run at build time can transparently use an :prop_tgt:`EXECUTABLE <TYPE>`
  45. target as a ``COMMAND`` executable. The buildsystem rules will ensure that
  46. the executable is built before attempting to run the command.
  47. .. _`Static Libraries`:
  48. Static Libraries
  49. ----------------
  50. Static libraries are archives of object files. They are produced by an
  51. archiver, not a linker. `Executables`_, `Shared Libraries`_, and
  52. `Module Libraries`_ may link to static libraries as dependencies.
  53. Linkers select subsets of object files from static libraries as needed
  54. to resolve symbols and link them into consuming binaries. Each binary
  55. that links to a static library gets its own copy of the symbols, and
  56. the static library itself is not needed at runtime.
  57. The :command:`add_library` command defines a static library target
  58. when called with the ``STATIC`` library type:
  59. .. code-block:: cmake
  60. add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)
  61. or, when the :variable:`BUILD_SHARED_LIBS` variable is false, with no type:
  62. .. code-block:: cmake
  63. add_library(archive archive.cpp zip.cpp lzma.cpp)
  64. CMake generates build rules to compile the source files into object
  65. files and archive them into a static library.
  66. Link dependencies of static libraries may be specified using the
  67. :command:`target_link_libraries` command. Since static libraries are
  68. archives rather than linked binaries, object files from their link
  69. dependencies are not included in the libraries themselves (except for
  70. `Object Libraries`_ specified as *direct* link dependencies).
  71. Instead, CMake records static libraries' link dependencies for
  72. transitive use when linking consuming binaries.
  73. .. _`Shared Libraries`:
  74. Shared Libraries
  75. ----------------
  76. Shared libraries are binaries created by linking object files together.
  77. `Executables`_, other shared libraries, and `Module Libraries`_ may link
  78. to shared libraries as dependencies. Linkers record references to shared
  79. libraries in consuming binaries. At runtime, a dynamic loader searches
  80. for referenced shared libraries on disk and loads their symbols.
  81. The :command:`add_library` command defines a shared library target
  82. when called with the ``SHARED`` library type:
  83. .. code-block:: cmake
  84. add_library(archive SHARED archive.cpp zip.cpp lzma.cpp)
  85. or, when the :variable:`BUILD_SHARED_LIBS` variable is true, with no type:
  86. .. code-block:: cmake
  87. add_library(archive archive.cpp zip.cpp lzma.cpp)
  88. CMake generates build rules to compile the source files into object
  89. files and link them into a shared library.
  90. Link dependencies of shared libraries may be specified using the
  91. :command:`target_link_libraries` command. Linkers start with the
  92. object files compiled from the shared library's own source files, and
  93. then resolve remaining symbol dependencies by searching linked libraries.
  94. .. note::
  95. CMake expects shared libraries to export at least one symbol. If a library
  96. does not export any unmanaged symbols, e.g., a Windows resource DLL or
  97. C++/CLI DLL, make it a `Module Library <Module Libraries_>`_ instead.
  98. .. _`Apple Frameworks`:
  99. Apple Frameworks
  100. ----------------
  101. `Shared Libraries`_ and `Static Libraries`_ may be marked with the
  102. :prop_tgt:`FRAMEWORK` target property to create a macOS or iOS Framework.
  103. A library with the ``FRAMEWORK`` target property should also set the
  104. :prop_tgt:`FRAMEWORK_VERSION` target property. This property is typically
  105. set to the value of "A" by macOS conventions.
  106. The ``MACOSX_FRAMEWORK_IDENTIFIER`` sets the ``CFBundleIdentifier`` key
  107. and it uniquely identifies the bundle.
  108. .. code-block:: cmake
  109. add_library(MyFramework SHARED MyFramework.cpp)
  110. set_target_properties(MyFramework PROPERTIES
  111. FRAMEWORK TRUE
  112. FRAMEWORK_VERSION A # Version "A" is macOS convention
  113. MACOSX_FRAMEWORK_IDENTIFIER org.cmake.MyFramework
  114. )
  115. .. _`Module Libraries`:
  116. Module Libraries
  117. ----------------
  118. Module libraries are binaries created by linking object files together.
  119. Unlike `Shared Libraries`_, module libraries may not be linked by other
  120. binaries as dependencies -- do not name them in the right-hand side of
  121. the :command:`target_link_libraries` command. Instead, module libraries
  122. are plugins that an application can dynamically load on-demand at runtime,
  123. e.g., by ``dlopen``.
  124. The :command:`add_library` command defines a module library target
  125. when called with the ``MODULE`` library type:
  126. .. code-block:: cmake
  127. add_library(archivePlugin MODULE 7z.cpp)
  128. CMake generates build rules to compile the source files into object
  129. files and link them into a module library.
  130. Link dependencies of module libraries may be specified using the
  131. :command:`target_link_libraries` command. Linkers start with the
  132. object files compiled from the module library's own source files, and
  133. then resolve remaining symbol dependencies by searching linked libraries.
  134. .. _`Object Libraries`:
  135. Object Libraries
  136. ----------------
  137. Object libraries are collections of object files created by compiling
  138. source files without any archiving or linking. The object files may be
  139. used when linking `Executables`_, `Shared Libraries`_, and
  140. `Module Libraries`_, or when archiving `Static Libraries`_.
  141. The :command:`add_library` command defines an object library target
  142. when called with the ``OBJECT`` library type:
  143. .. code-block:: cmake
  144. add_library(archiveObjs OBJECT archive.cpp zip.cpp lzma.cpp)
  145. CMake generates build rules to compile the source files into object files.
  146. Other targets may specify the object files as source inputs by using the
  147. :manual:`generator expression <cmake-generator-expressions(7)>` syntax
  148. :genex:`$<TARGET_OBJECTS:name>`:
  149. .. code-block:: cmake
  150. add_library(archiveExtras STATIC $<TARGET_OBJECTS:archiveObjs> extras.cpp)
  151. add_executable(test_exe $<TARGET_OBJECTS:archiveObjs> test.cpp)
  152. The consuming targets are linked (or archived) using object files
  153. both from their own sources and from the named object libraries.
  154. Alternatively, object libraries may be specified as link dependencies
  155. of other targets:
  156. .. code-block:: cmake
  157. add_library(archiveExtras STATIC extras.cpp)
  158. target_link_libraries(archiveExtras PUBLIC archiveObjs)
  159. add_executable(test_exe test.cpp)
  160. target_link_libraries(test_exe archiveObjs)
  161. The consuming targets are linked (or archived) using object files
  162. both from their own sources and from object libraries specified as
  163. *direct* link dependencies by :command:`target_link_libraries`.
  164. See :ref:`Linking Object Libraries`.
  165. Object libraries may not be used as the ``TARGET`` in a use of the
  166. :command:`add_custom_command(TARGET)` command signature. However,
  167. the list of objects can be used by :command:`add_custom_command(OUTPUT)`
  168. or :command:`file(GENERATE)` by using ``$<TARGET_OBJECTS:objlib>``.
  169. Build Specification and Usage Requirements
  170. ==========================================
  171. Targets build according to their own
  172. `build specification <Target Build Specification_>`_ in combination with
  173. `usage requirements <Target Usage Requirements_>`_ propagated from their
  174. link dependencies. Both may be specified using target-specific
  175. `commands <Target Commands_>`_.
  176. For example:
  177. .. code-block:: cmake
  178. add_library(archive SHARED archive.cpp zip.cpp)
  179. if (LZMA_FOUND)
  180. # Add a source implementing support for lzma.
  181. target_sources(archive PRIVATE lzma.cpp)
  182. # Compile the 'archive' library sources with '-DBUILDING_WITH_LZMA'.
  183. target_compile_definitions(archive PRIVATE BUILDING_WITH_LZMA)
  184. endif()
  185. target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
  186. add_executable(consumer consumer.cpp)
  187. # Link 'consumer' to 'archive'. This also consumes its usage requirements,
  188. # so 'consumer.cpp' is compiled with '-DUSING_ARCHIVE_LIB'.
  189. target_link_libraries(consumer archive)
  190. Target Commands
  191. ---------------
  192. Target-specific commands populate the
  193. `build specification <Target Build Specification_>`_ of `Binary Targets`_ and
  194. `usage requirements <Target Usage Requirements_>`_ of `Binary Targets`_,
  195. `Interface Libraries`_, and `Imported Targets`_.
  196. .. _`Target Command Scope`:
  197. Invocations must specify scope keywords, each affecting the visibility
  198. of arguments following it. The scopes are:
  199. ``PUBLIC``
  200. Populates both properties for `building <Target Build Specification_>`_
  201. and properties for `using <Target Usage Requirements_>`_ a target.
  202. ``PRIVATE``
  203. Populates only properties for `building <Target Build Specification_>`_
  204. a target.
  205. ``INTERFACE``
  206. Populates only properties for `using <Target Usage Requirements_>`_
  207. a target.
  208. The commands are:
  209. :command:`target_compile_definitions`
  210. Populates the :prop_tgt:`COMPILE_DEFINITIONS` build specification and
  211. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` usage requirement properties.
  212. For example, the call
  213. .. code-block:: cmake
  214. target_compile_definitions(archive
  215. PRIVATE BUILDING_WITH_LZMA
  216. INTERFACE USING_ARCHIVE_LIB
  217. )
  218. appends ``BUILDING_WITH_LZMA`` to the target's ``COMPILE_DEFINITIONS``
  219. property and appends ``USING_ARCHIVE_LIB`` to the target's
  220. ``INTERFACE_COMPILE_DEFINITIONS`` property.
  221. :command:`target_compile_options`
  222. Populates the :prop_tgt:`COMPILE_OPTIONS` build specification and
  223. :prop_tgt:`INTERFACE_COMPILE_OPTIONS` usage requirement properties.
  224. :command:`target_compile_features`
  225. .. versionadded:: 3.1
  226. Populates the :prop_tgt:`COMPILE_FEATURES` build specification and
  227. :prop_tgt:`INTERFACE_COMPILE_FEATURES` usage requirement properties.
  228. :command:`target_include_directories`
  229. Populates the :prop_tgt:`INCLUDE_DIRECTORIES` build specification
  230. and :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` usage requirement
  231. properties. With the ``SYSTEM`` option, it also populates the
  232. :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` usage requirement.
  233. For convenience, the :variable:`CMAKE_INCLUDE_CURRENT_DIR` variable
  234. may be enabled to add the source directory and corresponding build
  235. directory as ``INCLUDE_DIRECTORIES`` on all targets. Similarly,
  236. the :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE` variable may
  237. be enabled to add them as ``INTERFACE_INCLUDE_DIRECTORIES`` on all
  238. targets.
  239. :command:`target_sources`
  240. .. versionadded:: 3.1
  241. Populates the :prop_tgt:`SOURCES` build specification and
  242. :prop_tgt:`INTERFACE_SOURCES` usage requirement properties.
  243. It also supports specifying :ref:`File Sets`, which can add C++ module
  244. sources and headers not listed in the ``SOURCES`` and ``INTERFACE_SOURCES``
  245. properties. File sets may also populate the :prop_tgt:`INCLUDE_DIRECTORIES`
  246. build specification and :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` usage
  247. requirement properties with the include directories containing the headers.
  248. :command:`target_precompile_headers`
  249. .. versionadded:: 3.16
  250. Populates the :prop_tgt:`PRECOMPILE_HEADERS` build specification and
  251. :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS` usage requirement properties.
  252. :command:`target_link_libraries`
  253. Populates the :prop_tgt:`LINK_LIBRARIES` build specification
  254. and :prop_tgt:`INTERFACE_LINK_LIBRARIES` usage requirement properties.
  255. This is the primary mechanism by which link dependencies and their
  256. `usage requirements <Target Usage Requirements_>`_ are transitively
  257. propagated to affect compilation and linking of a target.
  258. :command:`target_link_directories`
  259. .. versionadded:: 3.13
  260. Populates the :prop_tgt:`LINK_DIRECTORIES` build specification and
  261. :prop_tgt:`INTERFACE_LINK_DIRECTORIES` usage requirement properties.
  262. :command:`target_link_options`
  263. .. versionadded:: 3.13
  264. Populates the :prop_tgt:`LINK_OPTIONS` build specification and
  265. :prop_tgt:`INTERFACE_LINK_OPTIONS` usage requirement properties.
  266. .. _`Target Build Specification`:
  267. Target Build Specification
  268. --------------------------
  269. The build specification of `Binary Targets`_ is represented by target
  270. properties. For each of the following `compile <Target Compile Properties_>`_
  271. and `link <Target Link Properties_>`_ properties, compilation and linking
  272. of the target is affected both by its own value and by the corresponding
  273. `usage requirement <Target Usage Requirements_>`_ property, named with
  274. an ``INTERFACE_`` prefix, collected from the transitive closure of link
  275. dependencies.
  276. .. _`Target Compile Properties`:
  277. Target Compile Properties
  278. ^^^^^^^^^^^^^^^^^^^^^^^^^
  279. These represent the `build specification <Target Build Specification_>`_
  280. for compiling a target.
  281. :prop_tgt:`COMPILE_DEFINITIONS`
  282. List of compile definitions for compiling sources in the target.
  283. These are passed to the compiler with ``-D`` flags, or equivalent,
  284. in an unspecified order.
  285. The :prop_tgt:`DEFINE_SYMBOL` target property is also used
  286. as a compile definition as a special convenience case for
  287. ``SHARED`` and ``MODULE`` library targets.
  288. :prop_tgt:`COMPILE_OPTIONS`
  289. List of compile options for compiling sources in the target.
  290. These are passed to the compiler as flags, in the order of appearance.
  291. Compile options are automatically escaped for the shell.
  292. Some compile options are best specified via dedicated settings,
  293. such as the :prop_tgt:`POSITION_INDEPENDENT_CODE` target property.
  294. :prop_tgt:`COMPILE_FEATURES`
  295. .. versionadded:: 3.1
  296. List of :manual:`compile features <cmake-compile-features(7)>` needed
  297. for compiling sources in the target. Typically these ensure the
  298. target's sources are compiled using a sufficient language standard level.
  299. :prop_tgt:`INCLUDE_DIRECTORIES`
  300. List of include directories for compiling sources in the target.
  301. These are passed to the compiler with ``-I`` or ``-isystem`` flags,
  302. or equivalent, in the order of appearance.
  303. For convenience, the :variable:`CMAKE_INCLUDE_CURRENT_DIR` variable
  304. may be enabled to add the source directory and corresponding build
  305. directory as ``INCLUDE_DIRECTORIES`` on all targets.
  306. :prop_tgt:`SOURCES`
  307. List of source files associated with the target. This includes sources
  308. specified when the target was created by the :command:`add_executable`,
  309. :command:`add_library`, or :command:`add_custom_target` command.
  310. It also includes sources added by the :command:`target_sources` command,
  311. but does not include :ref:`File Sets`.
  312. :prop_tgt:`PRECOMPILE_HEADERS`
  313. .. versionadded:: 3.16
  314. List of header files to precompile and include when compiling
  315. sources in the target.
  316. :prop_tgt:`AUTOMOC_MACRO_NAMES`
  317. .. versionadded:: 3.10
  318. List of macro names used by :prop_tgt:`AUTOMOC` to determine if a
  319. C++ source in the target needs to be processed by ``moc``.
  320. :prop_tgt:`AUTOUIC_OPTIONS`
  321. .. versionadded:: 3.0
  322. List of options used by :prop_tgt:`AUTOUIC` when invoking ``uic``
  323. for the target.
  324. .. _`Target Link Properties`:
  325. Target Link Properties
  326. ^^^^^^^^^^^^^^^^^^^^^^
  327. These represent the `build specification <Target Build Specification_>`_
  328. for linking a target.
  329. :prop_tgt:`LINK_LIBRARIES`
  330. List of link libraries for linking the target, if it is an executable,
  331. shared library, or module library. Entries for `Static Libraries`_
  332. and `Shared Libraries`_ are passed to the linker either via paths to
  333. their link artifacts, or with ``-l`` flags or equivalent. Entries for
  334. `Object Libraries`_ are passed to the linker via paths to their object
  335. files.
  336. Additionally, for compiling and linking the target itself,
  337. `usage requirements <Target Usage Requirements_>`_ are propagated from
  338. ``LINK_LIBRARIES`` entries naming `Static Libraries`_, `Shared Libraries`_,
  339. `Interface Libraries`_, `Object Libraries`_, and `Imported Targets`_,
  340. collected over the transitive closure of their
  341. :prop_tgt:`INTERFACE_LINK_LIBRARIES` properties.
  342. :prop_tgt:`LINK_DIRECTORIES`
  343. .. versionadded:: 3.13
  344. List of link directories for linking the target, if it is an executable,
  345. shared library, or module library. The directories are passed to the
  346. linker with ``-L`` flags, or equivalent.
  347. :prop_tgt:`LINK_OPTIONS`
  348. .. versionadded:: 3.13
  349. List of link options for linking the target, if it is an executable,
  350. shared library, or module library. The options are passed to the
  351. linker as flags, in the order of appearance.
  352. Link options are automatically escaped for the shell.
  353. :prop_tgt:`LINK_DEPENDS`
  354. List of files on which linking the target depends, if it is an executable,
  355. shared library, or module library. For example, linker scripts specified
  356. via :prop_tgt:`LINK_OPTIONS` may be listed here such that changing them
  357. causes binaries to be linked again.
  358. .. _`Target Usage Requirements`:
  359. Target Usage Requirements
  360. -------------------------
  361. The *usage requirements* of a target are settings that propagate to consumers,
  362. which link to the target via :command:`target_link_libraries`, in order to
  363. correctly compile and link with it. They are represented by transitive
  364. `compile <Transitive Compile Properties_>`_ and
  365. `link <Transitive Link Properties_>`_ properties.
  366. Note that usage requirements are not designed as a way to make downstreams
  367. use particular :prop_tgt:`COMPILE_OPTIONS`, :prop_tgt:`COMPILE_DEFINITIONS`,
  368. etc. for convenience only. The contents of the properties must be
  369. **requirements**, not merely recommendations.
  370. See the :ref:`Creating Relocatable Packages` section of the
  371. :manual:`cmake-packages(7)` manual for discussion of additional care
  372. that must be taken when specifying usage requirements while creating
  373. packages for redistribution.
  374. The usage requirements of a target can transitively propagate to the dependents.
  375. The :command:`target_link_libraries` command has ``PRIVATE``,
  376. ``INTERFACE`` and ``PUBLIC`` keywords to control the propagation.
  377. .. code-block:: cmake
  378. add_library(archive archive.cpp)
  379. target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)
  380. add_library(serialization serialization.cpp)
  381. target_compile_definitions(serialization INTERFACE USING_SERIALIZATION_LIB)
  382. add_library(archiveExtras extras.cpp)
  383. target_link_libraries(archiveExtras PUBLIC archive)
  384. target_link_libraries(archiveExtras PRIVATE serialization)
  385. # archiveExtras is compiled with -DUSING_ARCHIVE_LIB
  386. # and -DUSING_SERIALIZATION_LIB
  387. add_executable(consumer consumer.cpp)
  388. # consumer is compiled with -DUSING_ARCHIVE_LIB
  389. target_link_libraries(consumer archiveExtras)
  390. Because the ``archive`` is a ``PUBLIC`` dependency of ``archiveExtras``, the
  391. usage requirements of it are propagated to ``consumer`` too.
  392. Because
  393. ``serialization`` is a ``PRIVATE`` dependency of ``archiveExtras``, the usage
  394. requirements of it are not propagated to ``consumer``.
  395. Generally, a dependency should be specified in a use of
  396. :command:`target_link_libraries` with the ``PRIVATE`` keyword if it is used by
  397. only the implementation of a library, and not in the header files. If a
  398. dependency is additionally used in the header files of a library (e.g. for
  399. class inheritance), then it should be specified as a ``PUBLIC`` dependency.
  400. A dependency which is not used by the implementation of a library, but only by
  401. its headers should be specified as an ``INTERFACE`` dependency. The
  402. :command:`target_link_libraries` command may be invoked with multiple uses of
  403. each keyword:
  404. .. code-block:: cmake
  405. target_link_libraries(archiveExtras
  406. PUBLIC archive
  407. PRIVATE serialization
  408. )
  409. Usage requirements are propagated by reading the ``INTERFACE_`` variants
  410. of target properties from dependencies and appending the values to the
  411. non-``INTERFACE_`` variants of the operand. For example, the
  412. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of dependencies is read and
  413. appended to the :prop_tgt:`INCLUDE_DIRECTORIES` of the operand. In cases
  414. where order is relevant and maintained, and the order resulting from the
  415. :command:`target_link_libraries` calls does not allow correct compilation,
  416. use of an appropriate command to set the property directly may update the
  417. order.
  418. For example, if the linked libraries for a target must be specified
  419. in the order ``lib1`` ``lib2`` ``lib3`` , but the include directories must
  420. be specified in the order ``lib3`` ``lib1`` ``lib2``:
  421. .. code-block:: cmake
  422. target_link_libraries(myExe lib1 lib2 lib3)
  423. target_include_directories(myExe
  424. PRIVATE $<TARGET_PROPERTY:lib3,INTERFACE_INCLUDE_DIRECTORIES>)
  425. Note that care must be taken when specifying usage requirements for targets
  426. which will be exported for installation using the :command:`install(EXPORT)`
  427. command. See :ref:`Creating Packages` for more.
  428. .. _`Transitive Compile Properties`:
  429. Transitive Compile Properties
  430. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  431. These represent `usage requirements <Target Usage Requirements_>`_ for
  432. compiling consumers.
  433. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`
  434. List of compile definitions for compiling sources in the target's consumers.
  435. Typically these are used by the target's header files.
  436. :prop_tgt:`INTERFACE_COMPILE_OPTIONS`
  437. List of compile options for compiling sources in the target's consumers.
  438. :prop_tgt:`INTERFACE_COMPILE_FEATURES`
  439. .. versionadded:: 3.1
  440. List of :manual:`compile features <cmake-compile-features(7)>` needed
  441. for compiling sources in the target's consumers. Typically these
  442. ensure the target's header files are processed when compiling consumers
  443. using a sufficient language standard level.
  444. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`
  445. List of include directories for compiling sources in the target's consumers.
  446. Typically these are the locations of the target's header files.
  447. :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES`
  448. List of directories that, when specified as include directories, e.g., by
  449. :prop_tgt:`INCLUDE_DIRECTORIES` or :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
  450. should be treated as "system" include directories when compiling sources
  451. in the target's consumers.
  452. :prop_tgt:`INTERFACE_SOURCES`
  453. List of source files to associate with the target's consumers.
  454. :prop_tgt:`INTERFACE_PRECOMPILE_HEADERS`
  455. .. versionadded:: 3.16
  456. List of header files to precompile and include when compiling
  457. sources in the target's consumers.
  458. :prop_tgt:`INTERFACE_AUTOMOC_MACRO_NAMES`
  459. .. versionadded:: 3.27
  460. List of macro names used by :prop_tgt:`AUTOMOC` to determine if a
  461. C++ source in the target's consumers needs to be processed by ``moc``.
  462. :prop_tgt:`INTERFACE_AUTOUIC_OPTIONS`
  463. .. versionadded:: 3.0
  464. List of options used by :prop_tgt:`AUTOUIC` when invoking ``uic``
  465. for the target's consumers.
  466. .. _`Transitive Link Properties`:
  467. Transitive Link Properties
  468. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  469. These represent `usage requirements <Target Usage Requirements_>`_ for
  470. linking consumers.
  471. :prop_tgt:`INTERFACE_LINK_LIBRARIES`
  472. List of link libraries for linking the target's consumers, for
  473. those that are executables, shared libraries, or module libraries.
  474. These are the transitive dependencies of the target.
  475. Additionally, for compiling and linking the target's consumers,
  476. `usage requirements <Target Usage Requirements_>`_ are collected from
  477. the transitive closure of ``INTERFACE_LINK_LIBRARIES`` entries naming
  478. `Static Libraries`_, `Shared Libraries`_, `Interface Libraries`_,
  479. `Object Libraries`_, and `Imported Targets`_,
  480. :prop_tgt:`INTERFACE_LINK_DIRECTORIES`
  481. .. versionadded:: 3.13
  482. List of link directories for linking the target's consumers, for
  483. those that are executables, shared libraries, or module libraries.
  484. :prop_tgt:`INTERFACE_LINK_OPTIONS`
  485. .. versionadded:: 3.13
  486. List of link options for linking the target's consumers, for
  487. those that are executables, shared libraries, or module libraries.
  488. :prop_tgt:`INTERFACE_LINK_DEPENDS`
  489. .. versionadded:: 3.13
  490. List of files on which linking the target's consumers depends, for
  491. those that are executables, shared libraries, or module libraries.
  492. .. _`Custom Transitive Properties`:
  493. Custom Transitive Properties
  494. ----------------------------
  495. .. versionadded:: 3.30
  496. The :genex:`TARGET_PROPERTY` generator expression evaluates the above
  497. `build specification <Target Build Specification_>`_ and
  498. `usage requirement <Target Usage Requirements_>`_ properties
  499. as builtin transitive properties. It also supports custom transitive
  500. properties defined by the :prop_tgt:`TRANSITIVE_COMPILE_PROPERTIES`
  501. and :prop_tgt:`TRANSITIVE_LINK_PROPERTIES` properties on the target
  502. and its link dependencies.
  503. For example:
  504. .. code-block:: cmake
  505. add_library(example INTERFACE)
  506. set_target_properties(example PROPERTIES
  507. TRANSITIVE_COMPILE_PROPERTIES "CUSTOM_C"
  508. TRANSITIVE_LINK_PROPERTIES "CUSTOM_L"
  509. INTERFACE_CUSTOM_C "EXAMPLE_CUSTOM_C"
  510. INTERFACE_CUSTOM_L "EXAMPLE_CUSTOM_L"
  511. )
  512. add_library(mylib STATIC mylib.c)
  513. target_link_libraries(mylib PRIVATE example)
  514. set_target_properties(mylib PROPERTIES
  515. CUSTOM_C "MYLIB_PRIVATE_CUSTOM_C"
  516. CUSTOM_L "MYLIB_PRIVATE_CUSTOM_L"
  517. INTERFACE_CUSTOM_C "MYLIB_IFACE_CUSTOM_C"
  518. INTERFACE_CUSTOM_L "MYLIB_IFACE_CUSTOM_L"
  519. )
  520. add_executable(myexe myexe.c)
  521. target_link_libraries(myexe PRIVATE mylib)
  522. set_target_properties(myexe PROPERTIES
  523. CUSTOM_C "MYEXE_CUSTOM_C"
  524. CUSTOM_L "MYEXE_CUSTOM_L"
  525. )
  526. add_custom_target(print ALL VERBATIM
  527. COMMAND ${CMAKE_COMMAND} -E echo
  528. # Prints "MYLIB_PRIVATE_CUSTOM_C;EXAMPLE_CUSTOM_C"
  529. "$<TARGET_PROPERTY:mylib,CUSTOM_C>"
  530. # Prints "MYLIB_PRIVATE_CUSTOM_L;EXAMPLE_CUSTOM_L"
  531. "$<TARGET_PROPERTY:mylib,CUSTOM_L>"
  532. # Prints "MYEXE_CUSTOM_C"
  533. "$<TARGET_PROPERTY:myexe,CUSTOM_C>"
  534. # Prints "MYEXE_CUSTOM_L;MYLIB_IFACE_CUSTOM_L;EXAMPLE_CUSTOM_L"
  535. "$<TARGET_PROPERTY:myexe,CUSTOM_L>"
  536. )
  537. .. _`Compatible Interface Properties`:
  538. Compatible Interface Properties
  539. -------------------------------
  540. Some target properties are required to be compatible between a target and
  541. the interface of each dependency. For example, the
  542. :prop_tgt:`POSITION_INDEPENDENT_CODE` target property may specify a
  543. boolean value of whether a target should be compiled as
  544. position-independent-code, which has platform-specific consequences.
  545. A target may also specify the usage requirement
  546. :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` to communicate that
  547. consumers must be compiled as position-independent-code.
  548. .. code-block:: cmake
  549. add_executable(exe1 exe1.cpp)
  550. set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE ON)
  551. add_library(lib1 SHARED lib1.cpp)
  552. set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  553. add_executable(exe2 exe2.cpp)
  554. target_link_libraries(exe2 lib1)
  555. Here, both ``exe1`` and ``exe2`` will be compiled as position-independent-code.
  556. ``lib1`` will also be compiled as position-independent-code because that is the
  557. default setting for ``SHARED`` libraries. If dependencies have conflicting,
  558. non-compatible requirements :manual:`cmake(1)` issues a diagnostic:
  559. .. code-block:: cmake
  560. add_library(lib1 SHARED lib1.cpp)
  561. set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  562. add_library(lib2 SHARED lib2.cpp)
  563. set_property(TARGET lib2 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)
  564. add_executable(exe1 exe1.cpp)
  565. target_link_libraries(exe1 lib1)
  566. set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE OFF)
  567. add_executable(exe2 exe2.cpp)
  568. target_link_libraries(exe2 lib1 lib2)
  569. The ``lib1`` requirement ``INTERFACE_POSITION_INDEPENDENT_CODE`` is not
  570. "compatible" with the :prop_tgt:`POSITION_INDEPENDENT_CODE` property of
  571. the ``exe1`` target. The library requires that consumers are built as
  572. position-independent-code, while the executable specifies to not built as
  573. position-independent-code, so a diagnostic is issued.
  574. The ``lib1`` and ``lib2`` requirements are not "compatible". One of them
  575. requires that consumers are built as position-independent-code, while
  576. the other requires that consumers are not built as position-independent-code.
  577. Because ``exe2`` links to both and they are in conflict, a CMake error message
  578. is issued::
  579. CMake Error: The INTERFACE_POSITION_INDEPENDENT_CODE property of "lib2" does
  580. not agree with the value of POSITION_INDEPENDENT_CODE already determined
  581. for "exe2".
  582. To be "compatible", the :prop_tgt:`POSITION_INDEPENDENT_CODE` property,
  583. if set must be either the same, in a boolean sense, as the
  584. :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` property of all transitively
  585. specified dependencies on which that property is set.
  586. This property of "compatible interface requirement" may be extended to other
  587. properties by specifying the property in the content of the
  588. :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` target property. Each specified property
  589. must be compatible between the consuming target and the corresponding property
  590. with an ``INTERFACE_`` prefix from each dependency:
  591. .. code-block:: cmake
  592. add_library(lib1Version2 SHARED lib1_v2.cpp)
  593. set_property(TARGET lib1Version2 PROPERTY INTERFACE_CUSTOM_PROP ON)
  594. set_property(TARGET lib1Version2 APPEND PROPERTY
  595. COMPATIBLE_INTERFACE_BOOL CUSTOM_PROP
  596. )
  597. add_library(lib1Version3 SHARED lib1_v3.cpp)
  598. set_property(TARGET lib1Version3 PROPERTY INTERFACE_CUSTOM_PROP OFF)
  599. add_executable(exe1 exe1.cpp)
  600. target_link_libraries(exe1 lib1Version2) # CUSTOM_PROP will be ON
  601. add_executable(exe2 exe2.cpp)
  602. target_link_libraries(exe2 lib1Version2 lib1Version3) # Diagnostic
  603. Non-boolean properties may also participate in "compatible interface"
  604. computations. Properties specified in the
  605. :prop_tgt:`COMPATIBLE_INTERFACE_STRING`
  606. property must be either unspecified or compare to the same string among
  607. all transitively specified dependencies. This can be useful to ensure
  608. that multiple incompatible versions of a library are not linked together
  609. through transitive requirements of a target:
  610. .. code-block:: cmake
  611. add_library(lib1Version2 SHARED lib1_v2.cpp)
  612. set_property(TARGET lib1Version2 PROPERTY INTERFACE_LIB_VERSION 2)
  613. set_property(TARGET lib1Version2 APPEND PROPERTY
  614. COMPATIBLE_INTERFACE_STRING LIB_VERSION
  615. )
  616. add_library(lib1Version3 SHARED lib1_v3.cpp)
  617. set_property(TARGET lib1Version3 PROPERTY INTERFACE_LIB_VERSION 3)
  618. add_executable(exe1 exe1.cpp)
  619. target_link_libraries(exe1 lib1Version2) # LIB_VERSION will be "2"
  620. add_executable(exe2 exe2.cpp)
  621. target_link_libraries(exe2 lib1Version2 lib1Version3) # Diagnostic
  622. The :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` target property specifies
  623. that content will be evaluated numerically and the maximum number among all
  624. specified will be calculated:
  625. .. code-block:: cmake
  626. add_library(lib1Version2 SHARED lib1_v2.cpp)
  627. set_property(TARGET lib1Version2 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 200)
  628. set_property(TARGET lib1Version2 APPEND PROPERTY
  629. COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED
  630. )
  631. add_library(lib1Version3 SHARED lib1_v3.cpp)
  632. set_property(TARGET lib1Version3 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 1000)
  633. add_executable(exe1 exe1.cpp)
  634. # CONTAINER_SIZE_REQUIRED will be "200"
  635. target_link_libraries(exe1 lib1Version2)
  636. add_executable(exe2 exe2.cpp)
  637. # CONTAINER_SIZE_REQUIRED will be "1000"
  638. target_link_libraries(exe2 lib1Version2 lib1Version3)
  639. Similarly, the :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN` may be used to
  640. calculate the numeric minimum value for a property from dependencies.
  641. Each calculated "compatible" property value may be read in the consumer at
  642. generate-time using generator expressions.
  643. Note that for each dependee, the set of properties specified in each
  644. compatible interface property must not intersect with the set specified in
  645. any of the other properties.
  646. Property Origin Debugging
  647. -------------------------
  648. Because build specifications can be determined by dependencies, the lack of
  649. locality of code which creates a target and code which is responsible for
  650. setting build specifications may make the code more difficult to reason about.
  651. :manual:`cmake(1)` provides a debugging facility to print the origin of the
  652. contents of properties which may be determined by dependencies. The properties
  653. which can be debugged are listed in the
  654. :variable:`CMAKE_DEBUG_TARGET_PROPERTIES` variable documentation:
  655. .. code-block:: cmake
  656. set(CMAKE_DEBUG_TARGET_PROPERTIES
  657. INCLUDE_DIRECTORIES
  658. COMPILE_DEFINITIONS
  659. POSITION_INDEPENDENT_CODE
  660. CONTAINER_SIZE_REQUIRED
  661. LIB_VERSION
  662. )
  663. add_executable(exe1 exe1.cpp)
  664. In the case of properties listed in :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` or
  665. :prop_tgt:`COMPATIBLE_INTERFACE_STRING`, the debug output shows which target
  666. was responsible for setting the property, and which other dependencies also
  667. defined the property. In the case of
  668. :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` and
  669. :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN`, the debug output shows the
  670. value of the property from each dependency, and whether the value determines
  671. the new extreme.
  672. Build Specification with Generator Expressions
  673. ----------------------------------------------
  674. Build specifications may use
  675. :manual:`generator expressions <cmake-generator-expressions(7)>` containing
  676. content which may be conditional or known only at generate-time. For example,
  677. the calculated "compatible" value of a property may be read with the
  678. ``TARGET_PROPERTY`` expression:
  679. .. code-block:: cmake
  680. add_library(lib1Version2 SHARED lib1_v2.cpp)
  681. set_property(TARGET lib1Version2 PROPERTY
  682. INTERFACE_CONTAINER_SIZE_REQUIRED 200)
  683. set_property(TARGET lib1Version2 APPEND PROPERTY
  684. COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED
  685. )
  686. add_executable(exe1 exe1.cpp)
  687. target_link_libraries(exe1 lib1Version2)
  688. target_compile_definitions(exe1 PRIVATE
  689. CONTAINER_SIZE=$<TARGET_PROPERTY:CONTAINER_SIZE_REQUIRED>
  690. )
  691. In this case, the ``exe1`` source files will be compiled with
  692. ``-DCONTAINER_SIZE=200``.
  693. The unary ``TARGET_PROPERTY`` generator expression and the ``TARGET_POLICY``
  694. generator expression are evaluated with the consuming target context. This
  695. means that a usage requirement specification may be evaluated differently based
  696. on the consumer:
  697. .. code-block:: cmake
  698. add_library(lib1 lib1.cpp)
  699. target_compile_definitions(lib1 INTERFACE
  700. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:LIB1_WITH_EXE>
  701. $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:LIB1_WITH_SHARED_LIB>
  702. $<$<TARGET_POLICY:CMP0182>:CONSUMER_CMP0182_NEW>
  703. )
  704. add_executable(exe1 exe1.cpp)
  705. target_link_libraries(exe1 lib1)
  706. cmake_policy(SET CMP0182 NEW)
  707. add_library(shared_lib shared_lib.cpp)
  708. target_link_libraries(shared_lib lib1)
  709. The ``exe1`` executable will be compiled with ``-DLIB1_WITH_EXE``, while the
  710. ``shared_lib`` shared library will be compiled with ``-DLIB1_WITH_SHARED_LIB``
  711. and ``-DCONSUMER_CMP0182_NEW``, because policy :policy:`CMP0182` is
  712. ``NEW`` at the point where the ``shared_lib`` target is created.
  713. The ``BUILD_INTERFACE`` expression wraps requirements which are only used when
  714. consumed from a target in the same buildsystem, or when consumed from a target
  715. exported to the build directory using the :command:`export` command. The
  716. ``INSTALL_INTERFACE`` expression wraps requirements which are only used when
  717. consumed from a target which has been installed and exported with the
  718. :command:`install(EXPORT)` command:
  719. .. code-block:: cmake
  720. add_library(ClimbingStats climbingstats.cpp)
  721. target_compile_definitions(ClimbingStats INTERFACE
  722. $<BUILD_INTERFACE:ClimbingStats_FROM_BUILD_LOCATION>
  723. $<INSTALL_INTERFACE:ClimbingStats_FROM_INSTALLED_LOCATION>
  724. )
  725. install(TARGETS ClimbingStats EXPORT libExport ${InstallArgs})
  726. install(EXPORT libExport NAMESPACE Upstream::
  727. DESTINATION lib/cmake/ClimbingStats)
  728. export(EXPORT libExport NAMESPACE Upstream::)
  729. add_executable(exe1 exe1.cpp)
  730. target_link_libraries(exe1 ClimbingStats)
  731. In this case, the ``exe1`` executable will be compiled with
  732. ``-DClimbingStats_FROM_BUILD_LOCATION``. The exporting commands generate
  733. :prop_tgt:`IMPORTED` targets with either the ``INSTALL_INTERFACE`` or the
  734. ``BUILD_INTERFACE`` omitted, and the ``*_INTERFACE`` marker stripped away.
  735. A separate project consuming the ``ClimbingStats`` package would contain:
  736. .. code-block:: cmake
  737. find_package(ClimbingStats REQUIRED)
  738. add_executable(Downstream main.cpp)
  739. target_link_libraries(Downstream Upstream::ClimbingStats)
  740. Depending on whether the ``ClimbingStats`` package was used from the build
  741. location or the install location, the ``Downstream`` target would be compiled
  742. with either ``-DClimbingStats_FROM_BUILD_LOCATION`` or
  743. ``-DClimbingStats_FROM_INSTALL_LOCATION``. For more about packages and
  744. exporting see the :manual:`cmake-packages(7)` manual.
  745. .. _`Include Directories and Usage Requirements`:
  746. Include Directories and Usage Requirements
  747. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  748. Include directories require some special consideration when specified as usage
  749. requirements and when used with generator expressions. The
  750. :command:`target_include_directories` command accepts both relative and
  751. absolute include directories:
  752. .. code-block:: cmake
  753. add_library(lib1 lib1.cpp)
  754. target_include_directories(lib1 PRIVATE
  755. /absolute/path
  756. relative/path
  757. )
  758. Relative paths are interpreted relative to the source directory where the
  759. command appears. Relative paths are not allowed in the
  760. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of :prop_tgt:`IMPORTED` targets.
  761. In cases where a non-trivial generator expression is used, the
  762. ``INSTALL_PREFIX`` expression may be used within the argument of an
  763. ``INSTALL_INTERFACE`` expression. It is a replacement marker which
  764. expands to the installation prefix when imported by a consuming project.
  765. Include directories usage requirements commonly differ between the build-tree
  766. and the install-tree. The ``BUILD_INTERFACE`` and ``INSTALL_INTERFACE``
  767. generator expressions can be used to describe separate usage requirements
  768. based on the usage location. Relative paths are allowed within the
  769. ``INSTALL_INTERFACE`` expression and are interpreted relative to the
  770. installation prefix. For example:
  771. .. code-block:: cmake
  772. add_library(ClimbingStats climbingstats.cpp)
  773. target_include_directories(ClimbingStats INTERFACE
  774. $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>
  775. $<INSTALL_INTERFACE:/absolute/path>
  776. $<INSTALL_INTERFACE:relative/path>
  777. $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/$<CONFIG>/generated>
  778. )
  779. Two convenience APIs are provided relating to include directories usage
  780. requirements. The :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE` variable
  781. may be enabled, with an equivalent effect to:
  782. .. code-block:: cmake
  783. set_property(TARGET tgt APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES
  784. $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_BINARY_DIR}>
  785. )
  786. for each target affected. The convenience for installed targets is
  787. an ``INCLUDES DESTINATION`` component with the :command:`install(TARGETS)`
  788. command:
  789. .. code-block:: cmake
  790. install(TARGETS foo bar bat EXPORT tgts ${dest_args}
  791. INCLUDES DESTINATION include
  792. )
  793. install(EXPORT tgts ${other_args})
  794. install(FILES ${headers} DESTINATION include)
  795. This is equivalent to appending ``${CMAKE_INSTALL_PREFIX}/include`` to the
  796. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of each of the installed
  797. :prop_tgt:`IMPORTED` targets when generated by :command:`install(EXPORT)`.
  798. When the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of an
  799. :ref:`imported target <Imported targets>` is consumed, the entries in the
  800. property may be treated as system include directories. The effects of that
  801. are toolchain-dependent, but one common effect is to omit compiler warnings
  802. for headers found in those directories. The :prop_tgt:`SYSTEM` property of
  803. the installed target determines this behavior (see the
  804. :prop_tgt:`EXPORT_NO_SYSTEM` property for how to modify the installed value
  805. for a target). It is also possible to change how consumers interpret the
  806. system behavior of consumed imported targets by setting the
  807. :prop_tgt:`NO_SYSTEM_FROM_IMPORTED` target property on the *consumer*.
  808. If a binary target is linked transitively to a macOS :prop_tgt:`FRAMEWORK`, the
  809. ``Headers`` directory of the framework is also treated as a usage requirement.
  810. This has the same effect as passing the framework directory as an include
  811. directory.
  812. Link Libraries and Generator Expressions
  813. ----------------------------------------
  814. Like build specifications, :prop_tgt:`link libraries <LINK_LIBRARIES>` may be
  815. specified with generator expression conditions. However, as consumption of
  816. usage requirements is based on collection from linked dependencies, there is
  817. an additional limitation that the link dependencies must form a "directed
  818. acyclic graph". That is, if linking to a target is dependent on the value of
  819. a target property, that target property may not be dependent on the linked
  820. dependencies:
  821. .. code-block:: cmake
  822. add_library(lib1 lib1.cpp)
  823. add_library(lib2 lib2.cpp)
  824. target_link_libraries(lib1 PUBLIC
  825. $<$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>:lib2>
  826. )
  827. add_library(lib3 lib3.cpp)
  828. set_property(TARGET lib3 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  829. add_executable(exe1 exe1.cpp)
  830. target_link_libraries(exe1 lib1 lib3)
  831. As the value of the :prop_tgt:`POSITION_INDEPENDENT_CODE` property of
  832. the ``exe1`` target is dependent on the linked libraries (``lib3``), and the
  833. edge of linking ``exe1`` is determined by the same
  834. :prop_tgt:`POSITION_INDEPENDENT_CODE` property, the dependency graph above
  835. contains a cycle. :manual:`cmake(1)` issues an error message.
  836. .. _`Output Artifacts`:
  837. Output Artifacts
  838. ----------------
  839. The buildsystem targets created by the :command:`add_library` and
  840. :command:`add_executable` commands create rules to create binary outputs.
  841. The exact output location of the binaries can only be determined at
  842. generate-time because it can depend on the build-configuration and the
  843. link-language of linked dependencies etc. ``TARGET_FILE``,
  844. ``TARGET_LINKER_FILE`` and related expressions can be used to access the
  845. name and location of generated binaries. These expressions do not work
  846. for ``OBJECT`` libraries however, as there is no single file generated
  847. by such libraries which is relevant to the expressions.
  848. There are three kinds of output artifacts that may be build by targets
  849. as detailed in the following sections. Their classification differs
  850. between DLL platforms and non-DLL platforms. All Windows-based
  851. systems including Cygwin are DLL platforms.
  852. .. _`Runtime Output Artifacts`:
  853. Runtime Output Artifacts
  854. ^^^^^^^^^^^^^^^^^^^^^^^^
  855. A *runtime* output artifact of a buildsystem target may be:
  856. * The executable file (e.g. ``.exe``) of an executable target
  857. created by the :command:`add_executable` command.
  858. * On DLL platforms: the executable file (e.g. ``.dll``) of a shared
  859. library target created by the :command:`add_library` command
  860. with the ``SHARED`` option.
  861. The :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` and :prop_tgt:`RUNTIME_OUTPUT_NAME`
  862. target properties may be used to control runtime output artifact locations
  863. and names in the build tree.
  864. .. _`Library Output Artifacts`:
  865. Library Output Artifacts
  866. ^^^^^^^^^^^^^^^^^^^^^^^^
  867. A *library* output artifact of a buildsystem target may be:
  868. * The loadable module file (e.g. ``.dll`` or ``.so``) of a module
  869. library target created by the :command:`add_library` command
  870. with the ``MODULE`` option.
  871. * On non-DLL platforms: the shared library file (e.g. ``.so`` or ``.dylib``)
  872. of a shared library target created by the :command:`add_library`
  873. command with the ``SHARED`` option.
  874. The :prop_tgt:`LIBRARY_OUTPUT_DIRECTORY` and :prop_tgt:`LIBRARY_OUTPUT_NAME`
  875. target properties may be used to control library output artifact locations
  876. and names in the build tree.
  877. .. _`Archive Output Artifacts`:
  878. Archive Output Artifacts
  879. ^^^^^^^^^^^^^^^^^^^^^^^^
  880. An *archive* output artifact of a buildsystem target may be:
  881. * The static library file (e.g. ``.lib`` or ``.a``) of a static
  882. library target created by the :command:`add_library` command
  883. with the ``STATIC`` option.
  884. * On DLL platforms: the import library file (e.g. ``.lib``) of a shared
  885. library target created by the :command:`add_library` command
  886. with the ``SHARED`` option. This file is only guaranteed to exist if
  887. the library exports at least one unmanaged symbol.
  888. * On DLL platforms: the import library file (e.g. ``.lib``) of an
  889. executable target created by the :command:`add_executable` command
  890. when its :prop_tgt:`ENABLE_EXPORTS` target property is set.
  891. * On AIX: the linker import file (e.g. ``.imp``) of an executable target
  892. created by the :command:`add_executable` command when its
  893. :prop_tgt:`ENABLE_EXPORTS` target property is set.
  894. * On macOS: the linker import file (e.g. ``.tbd``) of a shared library target
  895. created by the :command:`add_library` command with the ``SHARED`` option and
  896. when its :prop_tgt:`ENABLE_EXPORTS` target property is set.
  897. The :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY` and :prop_tgt:`ARCHIVE_OUTPUT_NAME`
  898. target properties may be used to control archive output artifact locations
  899. and names in the build tree.
  900. Directory-Scoped Commands
  901. -------------------------
  902. The :command:`target_include_directories`,
  903. :command:`target_compile_definitions` and
  904. :command:`target_compile_options` commands have an effect on only one
  905. target at a time. The commands :command:`add_compile_definitions`,
  906. :command:`add_compile_options` and :command:`include_directories` have
  907. a similar function, but operate at directory scope instead of target
  908. scope for convenience.
  909. .. _`Build Configurations`:
  910. Build Configurations
  911. ====================
  912. Configurations determine specifications for a certain type of build, such
  913. as ``Release`` or ``Debug``. The way this is specified depends on the type
  914. of :manual:`generator <cmake-generators(7)>` being used. For single
  915. configuration generators like :ref:`Makefile Generators` and
  916. :generator:`Ninja`, the configuration is specified at configure time by the
  917. :variable:`CMAKE_BUILD_TYPE` variable. For multi-configuration generators
  918. like :ref:`Visual Studio <Visual Studio Generators>`, :generator:`Xcode`, and
  919. :generator:`Ninja Multi-Config`, the configuration is chosen by the user at
  920. build time and :variable:`CMAKE_BUILD_TYPE` is ignored. In the
  921. multi-configuration case, the set of *available* configurations is specified
  922. at configure time by the :variable:`CMAKE_CONFIGURATION_TYPES` variable,
  923. but the actual configuration used cannot be known until the build stage.
  924. This difference is often misunderstood, leading to problematic code like the
  925. following:
  926. .. code-block:: cmake
  927. # WARNING: This is wrong for multi-config generators because they don't use
  928. # and typically don't even set CMAKE_BUILD_TYPE
  929. string(TOLOWER ${CMAKE_BUILD_TYPE} build_type)
  930. if (build_type STREQUAL debug)
  931. target_compile_definitions(exe1 PRIVATE DEBUG_BUILD)
  932. endif()
  933. :manual:`Generator expressions <cmake-generator-expressions(7)>` should be
  934. used instead to handle configuration-specific logic correctly, regardless of
  935. the generator used. For example:
  936. .. code-block:: cmake
  937. # Works correctly for both single and multi-config generators
  938. target_compile_definitions(exe1 PRIVATE
  939. $<$<CONFIG:Debug>:DEBUG_BUILD>
  940. )
  941. In the presence of :prop_tgt:`IMPORTED` targets, the content of
  942. :prop_tgt:`MAP_IMPORTED_CONFIG_DEBUG <MAP_IMPORTED_CONFIG_<CONFIG>>` is also
  943. accounted for by the above :genex:`$<CONFIG:Debug>` expression.
  944. Case Sensitivity
  945. ----------------
  946. :variable:`CMAKE_BUILD_TYPE` and :variable:`CMAKE_CONFIGURATION_TYPES` are
  947. just like other variables in that any string comparisons made with their
  948. values will be case-sensitive. The :genex:`$<CONFIG>` generator expression also
  949. preserves the casing of the configuration as set by the user or CMake defaults.
  950. For example:
  951. .. code-block:: cmake
  952. # NOTE: Don't use these patterns, they are for illustration purposes only.
  953. set(CMAKE_BUILD_TYPE Debug)
  954. if(CMAKE_BUILD_TYPE STREQUAL DEBUG)
  955. # ... will never get here, "Debug" != "DEBUG"
  956. endif()
  957. add_custom_target(print_config ALL
  958. # Prints "Config is Debug" in this single-config case
  959. COMMAND ${CMAKE_COMMAND} -E echo "Config is $<CONFIG>"
  960. VERBATIM
  961. )
  962. set(CMAKE_CONFIGURATION_TYPES Debug Release)
  963. if(DEBUG IN_LIST CMAKE_CONFIGURATION_TYPES)
  964. # ... will never get here, "Debug" != "DEBUG"
  965. endif()
  966. In contrast, CMake treats the configuration type case-insensitively when
  967. using it internally in places that modify behavior based on the configuration.
  968. For example, the :genex:`$<CONFIG:Debug>` generator expression will evaluate to 1
  969. for a configuration of not only ``Debug``, but also ``DEBUG``, ``debug`` or
  970. even ``DeBuG``. Therefore, you can specify configuration types in
  971. :variable:`CMAKE_BUILD_TYPE` and :variable:`CMAKE_CONFIGURATION_TYPES` with
  972. any mixture of upper and lowercase, although there are strong conventions
  973. (see the next section). If you must test the value in string comparisons,
  974. always convert the value to upper or lowercase first and adjust the test
  975. accordingly.
  976. Default And Custom Configurations
  977. ---------------------------------
  978. By default, CMake defines a number of standard configurations:
  979. * ``Debug``
  980. * ``Release``
  981. * ``RelWithDebInfo``
  982. * ``MinSizeRel``
  983. In multi-config generators, the :variable:`CMAKE_CONFIGURATION_TYPES` variable
  984. will be populated with (potentially a subset of) the above list by default,
  985. unless overridden by the project or user. The actual configuration used is
  986. selected by the user at build time.
  987. For single-config generators, the configuration is specified with the
  988. :variable:`CMAKE_BUILD_TYPE` variable at configure time and cannot be changed
  989. at build time. The default value will often be none of the above standard
  990. configurations and will instead be an empty string. A common misunderstanding
  991. is that this is the same as ``Debug``, but that is not the case. Users should
  992. always explicitly specify the build type instead to avoid this common problem.
  993. The above standard configuration types provide reasonable behavior on most
  994. platforms, but they can be extended to provide other types. Each configuration
  995. defines a set of compiler and linker flag variables for the language in use.
  996. These variables follow the convention :variable:`CMAKE_<LANG>_FLAGS_<CONFIG>`,
  997. where ``<CONFIG>`` is always the uppercase configuration name. When defining
  998. a custom configuration type, make sure these variables are set appropriately,
  999. typically as cache variables.
  1000. Pseudo Targets
  1001. ==============
  1002. Some target types do not represent outputs of the buildsystem, but only inputs
  1003. such as external dependencies, aliases or other non-build artifacts. Pseudo
  1004. targets are not represented in the generated buildsystem.
  1005. .. _`Imported Targets`:
  1006. Imported Targets
  1007. ----------------
  1008. An :prop_tgt:`IMPORTED` target represents a pre-existing dependency. Usually
  1009. such targets are defined by an upstream package and should be treated as
  1010. immutable. After declaring an :prop_tgt:`IMPORTED` target one can adjust its
  1011. target properties by using the customary commands such as
  1012. :command:`target_compile_definitions`, :command:`target_include_directories`,
  1013. :command:`target_compile_options` or :command:`target_link_libraries` just like
  1014. with any other regular target.
  1015. :prop_tgt:`IMPORTED` targets may have the same usage requirement properties
  1016. populated as binary targets, such as
  1017. :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,
  1018. :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,
  1019. :prop_tgt:`INTERFACE_COMPILE_OPTIONS`,
  1020. :prop_tgt:`INTERFACE_LINK_LIBRARIES`, and
  1021. :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.
  1022. The :prop_tgt:`LOCATION` may also be read from an IMPORTED target, though there
  1023. is rarely reason to do so. Commands such as :command:`add_custom_command` can
  1024. transparently use an :prop_tgt:`IMPORTED` :prop_tgt:`EXECUTABLE <TYPE>` target
  1025. as a ``COMMAND`` executable.
  1026. The scope of the definition of an :prop_tgt:`IMPORTED` target is the directory
  1027. where it was defined. It may be accessed and used from subdirectories, but
  1028. not from parent directories or sibling directories. The scope is similar to
  1029. the scope of a cmake variable.
  1030. It is also possible to define a ``GLOBAL`` :prop_tgt:`IMPORTED` target which is
  1031. accessible globally in the buildsystem.
  1032. See the :manual:`cmake-packages(7)` manual for more on creating packages
  1033. with :prop_tgt:`IMPORTED` targets.
  1034. .. _`Alias Targets`:
  1035. Alias Targets
  1036. -------------
  1037. An ``ALIAS`` target is a name which may be used interchangeably with
  1038. a binary target name in read-only contexts. A primary use-case for ``ALIAS``
  1039. targets is for example or unit test executables accompanying a library, which
  1040. may be part of the same buildsystem or built separately based on user
  1041. configuration.
  1042. .. code-block:: cmake
  1043. add_library(lib1 lib1.cpp)
  1044. install(TARGETS lib1 EXPORT lib1Export ${dest_args})
  1045. install(EXPORT lib1Export NAMESPACE Upstream:: ${other_args})
  1046. add_library(Upstream::lib1 ALIAS lib1)
  1047. In another directory, we can link unconditionally to the ``Upstream::lib1``
  1048. target, which may be an :prop_tgt:`IMPORTED` target from a package, or an
  1049. ``ALIAS`` target if built as part of the same buildsystem.
  1050. .. code-block:: cmake
  1051. if (NOT TARGET Upstream::lib1)
  1052. find_package(lib1 REQUIRED)
  1053. endif()
  1054. add_executable(exe1 exe1.cpp)
  1055. target_link_libraries(exe1 Upstream::lib1)
  1056. ``ALIAS`` targets are not mutable, installable or exportable. They are
  1057. entirely local to the buildsystem description. A name can be tested for
  1058. whether it is an ``ALIAS`` name by reading the :prop_tgt:`ALIASED_TARGET`
  1059. property from it:
  1060. .. code-block:: cmake
  1061. get_target_property(_aliased Upstream::lib1 ALIASED_TARGET)
  1062. if(_aliased)
  1063. message(STATUS "The name Upstream::lib1 is an ALIAS for ${_aliased}.")
  1064. endif()
  1065. .. _`Interface Libraries`:
  1066. Interface Libraries
  1067. -------------------
  1068. An ``INTERFACE`` library target does not compile sources and does not
  1069. produce a library artifact on disk, so it has no :prop_tgt:`LOCATION`.
  1070. It may specify `usage requirements <Target Usage Requirements_>`_,
  1071. `compatible interface properties <Compatible Interface Properties_>`_, and
  1072. `custom transitive properties <Custom Transitive Properties_>`_.
  1073. Only the ``INTERFACE`` modes of the :command:`target_include_directories`,
  1074. :command:`target_compile_definitions`, :command:`target_compile_options`,
  1075. :command:`target_sources`, and :command:`target_link_libraries` commands
  1076. may be used with ``INTERFACE`` libraries.
  1077. Since CMake 3.19, an ``INTERFACE`` library target may optionally contain
  1078. source files. An interface library that contains source files will be
  1079. included as a build target in the generated buildsystem. It does not
  1080. compile sources, but may contain custom commands to generate other sources.
  1081. Additionally, IDEs will show the source files as part of the target for
  1082. interactive reading and editing.
  1083. A primary use-case for ``INTERFACE`` libraries is header-only libraries.
  1084. Since CMake 3.23, header files may be associated with a library by adding
  1085. them to a header set using the :command:`target_sources` command:
  1086. .. code-block:: cmake
  1087. add_library(Eigen INTERFACE)
  1088. target_sources(Eigen PUBLIC
  1089. FILE_SET HEADERS
  1090. BASE_DIRS src
  1091. FILES src/eigen.h src/vector.h src/matrix.h
  1092. )
  1093. add_executable(exe1 exe1.cpp)
  1094. target_link_libraries(exe1 Eigen)
  1095. When we specify the ``FILE_SET`` here, the ``BASE_DIRS`` we define automatically
  1096. become include directories in the usage requirements for the target ``Eigen``.
  1097. The usage requirements from the target are consumed and used when compiling, but
  1098. have no effect on linking.
  1099. Another use-case is to employ an entirely target-focussed design for usage
  1100. requirements:
  1101. .. code-block:: cmake
  1102. add_library(pic_on INTERFACE)
  1103. set_property(TARGET pic_on PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)
  1104. add_library(pic_off INTERFACE)
  1105. set_property(TARGET pic_off PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)
  1106. add_library(enable_rtti INTERFACE)
  1107. target_compile_options(enable_rtti INTERFACE
  1108. $<$<OR:$<COMPILER_ID:GNU>,$<COMPILER_ID:Clang>>:-rtti>
  1109. )
  1110. add_executable(exe1 exe1.cpp)
  1111. target_link_libraries(exe1 pic_on enable_rtti)
  1112. This way, the build specification of ``exe1`` is expressed entirely as linked
  1113. targets, and the complexity of compiler-specific flags is encapsulated in an
  1114. ``INTERFACE`` library target.
  1115. ``INTERFACE`` libraries may be installed and exported. We can install the
  1116. default header set along with the target:
  1117. .. code-block:: cmake
  1118. add_library(Eigen INTERFACE)
  1119. target_sources(Eigen PUBLIC
  1120. FILE_SET HEADERS
  1121. BASE_DIRS src
  1122. FILES src/eigen.h src/vector.h src/matrix.h
  1123. )
  1124. install(TARGETS Eigen EXPORT eigenExport
  1125. FILE_SET HEADERS DESTINATION include/Eigen)
  1126. install(EXPORT eigenExport NAMESPACE Upstream::
  1127. DESTINATION lib/cmake/Eigen
  1128. )
  1129. Here, the headers defined in the header set are installed to ``include/Eigen``.
  1130. The install destination automatically becomes an include directory that is a
  1131. usage requirement for consumers.
  1132. Properties Allowed on Interface Libraries
  1133. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  1134. Since CMake 3.19, interface libraries allow setting or reading target
  1135. properties with any name, just like other target kinds always have.
  1136. Prior to CMake 3.19, interface libraries only allowed setting or reading
  1137. target properties with a limited set of names:
  1138. * Properties named with an ``INTERFACE_`` prefix, either builtin
  1139. `usage requirements <Target Usage Requirements_>`_, or custom names.
  1140. * Built-in properties named with a ``COMPATIBLE_INTERFACE_`` prefix
  1141. (`compatible interface properties <Compatible Interface Properties_>`_).
  1142. * Built-in properties :prop_tgt:`NAME`, :prop_tgt:`EXPORT_NAME`,
  1143. :prop_tgt:`EXPORT_PROPERTIES`, :prop_tgt:`MANUALLY_ADDED_DEPENDENCIES`,
  1144. :prop_tgt:`IMPORTED`, :prop_tgt:`IMPORTED_LIBNAME_<CONFIG>`, and
  1145. :prop_tgt:`MAP_IMPORTED_CONFIG_<CONFIG>`.
  1146. * .. versionadded:: 3.11
  1147. Properties named with a leading underscore (``_``)
  1148. or lowercase ASCII character.