cmake-buildsystem.7.rst 43 KB

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