cmake-buildsystem.7.rst 52 KB

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