cmake-buildsystem.7.rst 54 KB

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