cmake-buildsystem.7.rst 43 KB

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