cmake-buildsystem.7.rst 52 KB

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