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