| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011 | .. cmake-manual-description: CMake Buildsystem Referencecmake-buildsystem(7)********************.. only:: html   .. contents::Introduction============A CMake-based buildsystem is organized as a set of high-level logicaltargets.  Each target corresponds to an executable or library, oris a custom target containing custom commands.  Dependencies between thetargets are expressed in the buildsystem to determine the build orderand the rules for regeneration in response to change.Binary Targets==============Executables and libraries are defined using the :command:`add_executable`and :command:`add_library` commands.  The resulting binary files haveappropriate :prop_tgt:`PREFIX`, :prop_tgt:`SUFFIX` and extensions for the platform targeted.Dependencies between binary targets are expressed using the:command:`target_link_libraries` command:.. code-block:: cmake  add_library(archive archive.cpp zip.cpp lzma.cpp)  add_executable(zipapp zipapp.cpp)  target_link_libraries(zipapp archive)``archive`` is defined as a ``STATIC`` library -- an archive containing objectscompiled from ``archive.cpp``, ``zip.cpp``, and ``lzma.cpp``.  ``zipapp``is defined as an executable formed by compiling and linking ``zipapp.cpp``.When linking the ``zipapp`` executable, the ``archive`` static library islinked in.Binary Executables------------------The :command:`add_executable` command defines an executable target:.. code-block:: cmake  add_executable(mytool mytool.cpp)Commands such as :command:`add_custom_command`, which generates rules to berun at build time can transparently use an :prop_tgt:`EXECUTABLE <TYPE>`target as a ``COMMAND`` executable.  The buildsystem rules will ensure thatthe executable is built before attempting to run the command.Binary Library Types--------------------.. _`Normal Libraries`:Normal Libraries^^^^^^^^^^^^^^^^By default, the :command:`add_library` command defines a ``STATIC`` library,unless a type is specified.  A type may be specified when using the command:.. code-block:: cmake  add_library(archive SHARED archive.cpp zip.cpp lzma.cpp).. code-block:: cmake  add_library(archive STATIC archive.cpp zip.cpp lzma.cpp)The :variable:`BUILD_SHARED_LIBS` variable may be enabled to change thebehavior of :command:`add_library` to build shared libraries by default.In the context of the buildsystem definition as a whole, it is largelyirrelevant whether particular libraries are ``SHARED`` or ``STATIC`` --the commands, dependency specifications and other APIs work similarlyregardless of the library type.  The ``MODULE`` library type isdissimilar in that it is generally not linked to -- it is not used inthe right-hand-side of the :command:`target_link_libraries` command.It is a type which is loaded as a plugin using runtime techniques.If the library does not export any unmanaged symbols (e.g. Windowsresource DLL, C++/CLI DLL), it is required that the library not be a``SHARED`` library because CMake expects ``SHARED`` libraries to exportat least one symbol... code-block:: cmake  add_library(archive MODULE 7z.cpp).. _`Apple Frameworks`:Apple Frameworks""""""""""""""""A ``SHARED`` library may be marked with the :prop_tgt:`FRAMEWORK`target property to create an macOS or iOS Framework Bundle.The ``MACOSX_FRAMEWORK_IDENTIFIER`` sets ``CFBundleIdentifier`` keyand it uniquely identifies the bundle... code-block:: cmake  add_library(MyFramework SHARED MyFramework.cpp)  set_target_properties(MyFramework PROPERTIES    FRAMEWORK TRUE    FRAMEWORK_VERSION A    MACOSX_FRAMEWORK_IDENTIFIER org.cmake.MyFramework  ).. _`Object Libraries`:Object Libraries^^^^^^^^^^^^^^^^The ``OBJECT`` library type defines a non-archival collection of object filesresulting from compiling the given source files.  The object files collectionmay be used as source inputs to other targets:.. code-block:: cmake  add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)  add_library(archiveExtras STATIC $<TARGET_OBJECTS:archive> extras.cpp)  add_executable(test_exe $<TARGET_OBJECTS:archive> test.cpp)The link (or archiving) step of those other targets will use the objectfiles collection in addition to those from their own sources.Alternatively, object libraries may be linked into other targets:.. code-block:: cmake  add_library(archive OBJECT archive.cpp zip.cpp lzma.cpp)  add_library(archiveExtras STATIC extras.cpp)  target_link_libraries(archiveExtras PUBLIC archive)  add_executable(test_exe test.cpp)  target_link_libraries(test_exe archive)The link (or archiving) step of those other targets will use the objectfiles from ``OBJECT`` libraries that are *directly* linked.  Additionally,usage requirements of the ``OBJECT`` libraries will be honored when compilingsources in those other targets.  Furthermore, those usage requirementswill propagate transitively to dependents of those other targets.Object libraries may not be used as the ``TARGET`` in a use of the:command:`add_custom_command(TARGET)` command signature.  However,the list of objects can be used by :command:`add_custom_command(OUTPUT)`or :command:`file(GENERATE)` by using ``$<TARGET_OBJECTS:objlib>``.Build Specification and Usage Requirements==========================================The :command:`target_include_directories`, :command:`target_compile_definitions`and :command:`target_compile_options` commands specify the build specificationsand the usage requirements of binary targets.  The commands populate the:prop_tgt:`INCLUDE_DIRECTORIES`, :prop_tgt:`COMPILE_DEFINITIONS` and:prop_tgt:`COMPILE_OPTIONS` target properties respectively, and/or the:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`, :prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`and :prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties.Each of the commands has a ``PRIVATE``, ``PUBLIC`` and ``INTERFACE`` mode.  The``PRIVATE`` mode populates only the non-``INTERFACE_`` variant of the targetproperty and the ``INTERFACE`` mode populates only the ``INTERFACE_`` variants.The ``PUBLIC`` mode populates both variants of the respective target property.Each command may be invoked with multiple uses of each keyword:.. code-block:: cmake  target_compile_definitions(archive    PRIVATE BUILDING_WITH_LZMA    INTERFACE USING_ARCHIVE_LIB  )Note that usage requirements are not designed as a way to make downstreamsuse particular :prop_tgt:`COMPILE_OPTIONS` or:prop_tgt:`COMPILE_DEFINITIONS` etc for convenience only.  The contents ofthe properties must be **requirements**, not merely recommendations orconvenience.See the :ref:`Creating Relocatable Packages` section of the:manual:`cmake-packages(7)` manual for discussion of additional carethat must be taken when specifying usage requirements while creatingpackages for redistribution.Target Properties-----------------The contents of the :prop_tgt:`INCLUDE_DIRECTORIES`,:prop_tgt:`COMPILE_DEFINITIONS` and :prop_tgt:`COMPILE_OPTIONS` targetproperties are used appropriately when compiling the source files of abinary target.Entries in the :prop_tgt:`INCLUDE_DIRECTORIES` are added to the compile linewith ``-I`` or ``-isystem`` prefixes and in the order of appearance in theproperty value.Entries in the :prop_tgt:`COMPILE_DEFINITIONS` are prefixed with ``-D`` or``/D`` and added to the compile line in an unspecified order.  The:prop_tgt:`DEFINE_SYMBOL` target property is also added as a compiledefinition as a special convenience case for ``SHARED`` and ``MODULE``library targets.Entries in the :prop_tgt:`COMPILE_OPTIONS` are escaped for the shell and addedin the order of appearance in the property value.  Several compile options havespecial separate handling, such as :prop_tgt:`POSITION_INDEPENDENT_CODE`.The contents of the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,:prop_tgt:`INTERFACE_COMPILE_DEFINITIONS` and:prop_tgt:`INTERFACE_COMPILE_OPTIONS` target properties are*Usage Requirements* -- they specify content which consumersmust use to correctly compile and link with the target they appear on.For any binary target, the contents of each ``INTERFACE_`` property oneach target specified in a :command:`target_link_libraries` command isconsumed:.. code-block:: cmake  set(srcs archive.cpp zip.cpp)  if (LZMA_FOUND)    list(APPEND srcs lzma.cpp)  endif()  add_library(archive SHARED ${srcs})  if (LZMA_FOUND)    # The archive library sources are compiled with -DBUILDING_WITH_LZMA    target_compile_definitions(archive PRIVATE BUILDING_WITH_LZMA)  endif()  target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)  add_executable(consumer)  # Link consumer to archive and consume its usage requirements. The consumer  # executable sources are compiled with -DUSING_ARCHIVE_LIB.  target_link_libraries(consumer archive)Because it is common to require that the source directory and correspondingbuild directory are added to the :prop_tgt:`INCLUDE_DIRECTORIES`, the:variable:`CMAKE_INCLUDE_CURRENT_DIR` variable can be enabled to convenientlyadd the corresponding directories to the :prop_tgt:`INCLUDE_DIRECTORIES` ofall targets.  The variable :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE`can be enabled to add the corresponding directories to the:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of all targets.  This makes use oftargets in multiple different directories convenient through use of the:command:`target_link_libraries` command... _`Target Usage Requirements`:Transitive Usage Requirements-----------------------------The usage requirements of a target can transitively propagate to dependents.The :command:`target_link_libraries` command has ``PRIVATE``,``INTERFACE`` and ``PUBLIC`` keywords to control the propagation... code-block:: cmake  add_library(archive archive.cpp)  target_compile_definitions(archive INTERFACE USING_ARCHIVE_LIB)  add_library(serialization serialization.cpp)  target_compile_definitions(serialization INTERFACE USING_SERIALIZATION_LIB)  add_library(archiveExtras extras.cpp)  target_link_libraries(archiveExtras PUBLIC archive)  target_link_libraries(archiveExtras PRIVATE serialization)  # archiveExtras is compiled with -DUSING_ARCHIVE_LIB  # and -DUSING_SERIALIZATION_LIB  add_executable(consumer consumer.cpp)  # consumer is compiled with -DUSING_ARCHIVE_LIB  target_link_libraries(consumer archiveExtras)Because ``archive`` is a ``PUBLIC`` dependency of ``archiveExtras``, theusage requirements of it are propagated to ``consumer`` too.  Because``serialization`` is a ``PRIVATE`` dependency of ``archiveExtras``, the usagerequirements of it are not propagated to ``consumer``.Generally, a dependency should be specified in a use of:command:`target_link_libraries` with the ``PRIVATE`` keyword if it is used byonly the implementation of a library, and not in the header files.  If adependency is additionally used in the header files of a library (e.g. forclass inheritance), then it should be specified as a ``PUBLIC`` dependency.A dependency which is not used by the implementation of a library, but only byits headers should be specified as an ``INTERFACE`` dependency.  The:command:`target_link_libraries` command may be invoked with multiple uses ofeach keyword:.. code-block:: cmake  target_link_libraries(archiveExtras    PUBLIC archive    PRIVATE serialization  )Usage requirements are propagated by reading the ``INTERFACE_`` variantsof target properties from dependencies and appending the values to thenon-``INTERFACE_`` variants of the operand.  For example, the:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of dependencies is read andappended to the :prop_tgt:`INCLUDE_DIRECTORIES` of the operand.  In caseswhere order is relevant and maintained, and the order resulting from the:command:`target_link_libraries` calls does not allow correct compilation,use of an appropriate command to set the property directly may update theorder.For example, if the linked libraries for a target must be specifiedin the order ``lib1`` ``lib2`` ``lib3`` , but the include directories mustbe specified in the order ``lib3`` ``lib1`` ``lib2``:.. code-block:: cmake  target_link_libraries(myExe lib1 lib2 lib3)  target_include_directories(myExe    PRIVATE $<TARGET_PROPERTY:lib3,INTERFACE_INCLUDE_DIRECTORIES>)Note that care must be taken when specifying usage requirements for targetswhich will be exported for installation using the :command:`install(EXPORT)`command.  See :ref:`Creating Packages` for more... _`Compatible Interface Properties`:Compatible Interface Properties-------------------------------Some target properties are required to be compatible between a target andthe interface of each dependency.  For example, the:prop_tgt:`POSITION_INDEPENDENT_CODE` target property may specify aboolean value of whether a target should be compiled asposition-independent-code, which has platform-specific consequences.A target may also specify the usage requirement:prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` to communicate thatconsumers must be compiled as position-independent-code... code-block:: cmake  add_executable(exe1 exe1.cpp)  set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE ON)  add_library(lib1 SHARED lib1.cpp)  set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)  add_executable(exe2 exe2.cpp)  target_link_libraries(exe2 lib1)Here, both ``exe1`` and ``exe2`` will be compiled as position-independent-code.``lib1`` will also be compiled as position-independent-code because that is thedefault setting for ``SHARED`` libraries.  If dependencies have conflicting,non-compatible requirements :manual:`cmake(1)` issues a diagnostic:.. code-block:: cmake  add_library(lib1 SHARED lib1.cpp)  set_property(TARGET lib1 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)  add_library(lib2 SHARED lib2.cpp)  set_property(TARGET lib2 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 lib1)  set_property(TARGET exe1 PROPERTY POSITION_INDEPENDENT_CODE OFF)  add_executable(exe2 exe2.cpp)  target_link_libraries(exe2 lib1 lib2)The ``lib1`` requirement ``INTERFACE_POSITION_INDEPENDENT_CODE`` is not"compatible" with the :prop_tgt:`POSITION_INDEPENDENT_CODE` property ofthe ``exe1`` target.  The library requires that consumers are built asposition-independent-code, while the executable specifies to not built asposition-independent-code, so a diagnostic is issued.The ``lib1`` and ``lib2`` requirements are not "compatible".  One of themrequires that consumers are built as position-independent-code, whilethe other requires that consumers are not built as position-independent-code.Because ``exe2`` links to both and they are in conflict, a diagnostic isissued.To be "compatible", the :prop_tgt:`POSITION_INDEPENDENT_CODE` property,if set must be either the same, in a boolean sense, as the:prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE` property of all transitivelyspecified dependencies on which that property is set.This property of "compatible interface requirement" may be extended to otherproperties by specifying the property in the content of the:prop_tgt:`COMPATIBLE_INTERFACE_BOOL` target property.  Each specified propertymust be compatible between the consuming target and the corresponding propertywith an ``INTERFACE_`` prefix from each dependency:.. code-block:: cmake  add_library(lib1Version2 SHARED lib1_v2.cpp)  set_property(TARGET lib1Version2 PROPERTY INTERFACE_CUSTOM_PROP ON)  set_property(TARGET lib1Version2 APPEND PROPERTY    COMPATIBLE_INTERFACE_BOOL CUSTOM_PROP  )  add_library(lib1Version3 SHARED lib1_v3.cpp)  set_property(TARGET lib1Version3 PROPERTY INTERFACE_CUSTOM_PROP OFF)  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 lib1Version2) # CUSTOM_PROP will be ON  add_executable(exe2 exe2.cpp)  target_link_libraries(exe2 lib1Version2 lib1Version3) # DiagnosticNon-boolean properties may also participate in "compatible interface"computations.  Properties specified in the:prop_tgt:`COMPATIBLE_INTERFACE_STRING`property must be either unspecified or compare to the same string amongall transitively specified dependencies. This can be useful to ensurethat multiple incompatible versions of a library are not linked togetherthrough transitive requirements of a target:.. code-block:: cmake  add_library(lib1Version2 SHARED lib1_v2.cpp)  set_property(TARGET lib1Version2 PROPERTY INTERFACE_LIB_VERSION 2)  set_property(TARGET lib1Version2 APPEND PROPERTY    COMPATIBLE_INTERFACE_STRING LIB_VERSION  )  add_library(lib1Version3 SHARED lib1_v3.cpp)  set_property(TARGET lib1Version3 PROPERTY INTERFACE_LIB_VERSION 3)  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 lib1Version2) # LIB_VERSION will be "2"  add_executable(exe2 exe2.cpp)  target_link_libraries(exe2 lib1Version2 lib1Version3) # DiagnosticThe :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` target property specifiesthat content will be evaluated numerically and the maximum number among allspecified will be calculated:.. code-block:: cmake  add_library(lib1Version2 SHARED lib1_v2.cpp)  set_property(TARGET lib1Version2 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 200)  set_property(TARGET lib1Version2 APPEND PROPERTY    COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED  )  add_library(lib1Version3 SHARED lib1_v3.cpp)  set_property(TARGET lib1Version3 PROPERTY INTERFACE_CONTAINER_SIZE_REQUIRED 1000)  add_executable(exe1 exe1.cpp)  # CONTAINER_SIZE_REQUIRED will be "200"  target_link_libraries(exe1 lib1Version2)  add_executable(exe2 exe2.cpp)  # CONTAINER_SIZE_REQUIRED will be "1000"  target_link_libraries(exe2 lib1Version2 lib1Version3)Similarly, the :prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN` may be used tocalculate the numeric minimum value for a property from dependencies.Each calculated "compatible" property value may be read in the consumer atgenerate-time using generator expressions.Note that for each dependee, the set of properties specified in eachcompatible interface property must not intersect with the set specified inany of the other properties.Property Origin Debugging-------------------------Because build specifications can be determined by dependencies, the lack oflocality of code which creates a target and code which is responsible forsetting build specifications may make the code more difficult to reason about.:manual:`cmake(1)` provides a debugging facility to print the origin of thecontents of properties which may be determined by dependencies.  The propertieswhich can be debugged are listed in the:variable:`CMAKE_DEBUG_TARGET_PROPERTIES` variable documentation:.. code-block:: cmake  set(CMAKE_DEBUG_TARGET_PROPERTIES    INCLUDE_DIRECTORIES    COMPILE_DEFINITIONS    POSITION_INDEPENDENT_CODE    CONTAINER_SIZE_REQUIRED    LIB_VERSION  )  add_executable(exe1 exe1.cpp)In the case of properties listed in :prop_tgt:`COMPATIBLE_INTERFACE_BOOL` or:prop_tgt:`COMPATIBLE_INTERFACE_STRING`, the debug output shows which targetwas responsible for setting the property, and which other dependencies alsodefined the property.  In the case of:prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MAX` and:prop_tgt:`COMPATIBLE_INTERFACE_NUMBER_MIN`, the debug output shows thevalue of the property from each dependency, and whether the value determinesthe new extreme.Build Specification with Generator Expressions----------------------------------------------Build specifications may use:manual:`generator expressions <cmake-generator-expressions(7)>` containingcontent which may be conditional or known only at generate-time.  For example,the calculated "compatible" value of a property may be read with the``TARGET_PROPERTY`` expression:.. code-block:: cmake  add_library(lib1Version2 SHARED lib1_v2.cpp)  set_property(TARGET lib1Version2 PROPERTY    INTERFACE_CONTAINER_SIZE_REQUIRED 200)  set_property(TARGET lib1Version2 APPEND PROPERTY    COMPATIBLE_INTERFACE_NUMBER_MAX CONTAINER_SIZE_REQUIRED  )  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 lib1Version2)  target_compile_definitions(exe1 PRIVATE      CONTAINER_SIZE=$<TARGET_PROPERTY:CONTAINER_SIZE_REQUIRED>  )In this case, the ``exe1`` source files will be compiled with``-DCONTAINER_SIZE=200``.Configuration determined build specifications may be conveniently set usingthe ``CONFIG`` generator expression... code-block:: cmake  target_compile_definitions(exe1 PRIVATE      $<$<CONFIG:Debug>:DEBUG_BUILD>  )The ``CONFIG`` parameter is compared case-insensitively with the configurationbeing built.  In the presence of :prop_tgt:`IMPORTED` targets, the content of:prop_tgt:`MAP_IMPORTED_CONFIG_DEBUG <MAP_IMPORTED_CONFIG_<CONFIG>>` is alsoaccounted for by this expression.Some buildsystems generated by :manual:`cmake(1)` have a predeterminedbuild-configuration set in the :variable:`CMAKE_BUILD_TYPE` variable.  Thebuildsystem for the IDEs such as Visual Studio and Xcode are generatedindependent of the build-configuration, and the actual build configurationis not known until build-time.  Therefore, code such as.. code-block:: cmake  string(TOLOWER ${CMAKE_BUILD_TYPE} _type)  if (_type STREQUAL debug)    target_compile_definitions(exe1 PRIVATE DEBUG_BUILD)  endif()may appear to work for :ref:`Makefile Generators` and :generator:`Ninja`generators, but is not portable to IDE generators.  Additionally,the :prop_tgt:`IMPORTED` configuration-mappings are not accounted forwith code like this, so it should be avoided.The unary ``TARGET_PROPERTY`` generator expression and the ``TARGET_POLICY``generator expression are evaluated with the consuming target context.  Thismeans that a usage requirement specification may be evaluated differently basedon the consumer:.. code-block:: cmake  add_library(lib1 lib1.cpp)  target_compile_definitions(lib1 INTERFACE    $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,EXECUTABLE>:LIB1_WITH_EXE>    $<$<STREQUAL:$<TARGET_PROPERTY:TYPE>,SHARED_LIBRARY>:LIB1_WITH_SHARED_LIB>    $<$<TARGET_POLICY:CMP0041>:CONSUMER_CMP0041_NEW>  )  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 lib1)  cmake_policy(SET CMP0041 NEW)  add_library(shared_lib shared_lib.cpp)  target_link_libraries(shared_lib lib1)The ``exe1`` executable will be compiled with ``-DLIB1_WITH_EXE``, while the``shared_lib`` shared library will be compiled with ``-DLIB1_WITH_SHARED_LIB``and ``-DCONSUMER_CMP0041_NEW``, because policy :policy:`CMP0041` is``NEW`` at the point where the ``shared_lib`` target is created.The ``BUILD_INTERFACE`` expression wraps requirements which are only used whenconsumed from a target in the same buildsystem, or when consumed from a targetexported to the build directory using the :command:`export` command.  The``INSTALL_INTERFACE`` expression wraps requirements which are only used whenconsumed from a target which has been installed and exported with the:command:`install(EXPORT)` command:.. code-block:: cmake  add_library(ClimbingStats climbingstats.cpp)  target_compile_definitions(ClimbingStats INTERFACE    $<BUILD_INTERFACE:ClimbingStats_FROM_BUILD_LOCATION>    $<INSTALL_INTERFACE:ClimbingStats_FROM_INSTALLED_LOCATION>  )  install(TARGETS ClimbingStats EXPORT libExport ${InstallArgs})  install(EXPORT libExport NAMESPACE Upstream::          DESTINATION lib/cmake/ClimbingStats)  export(EXPORT libExport NAMESPACE Upstream::)  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 ClimbingStats)In this case, the ``exe1`` executable will be compiled with``-DClimbingStats_FROM_BUILD_LOCATION``.  The exporting commands generate:prop_tgt:`IMPORTED` targets with either the ``INSTALL_INTERFACE`` or the``BUILD_INTERFACE`` omitted, and the ``*_INTERFACE`` marker stripped away.A separate project consuming the ``ClimbingStats`` package would contain:.. code-block:: cmake  find_package(ClimbingStats REQUIRED)  add_executable(Downstream main.cpp)  target_link_libraries(Downstream Upstream::ClimbingStats)Depending on whether the ``ClimbingStats`` package was used from the buildlocation or the install location, the ``Downstream`` target would be compiledwith either ``-DClimbingStats_FROM_BUILD_LOCATION`` or``-DClimbingStats_FROM_INSTALL_LOCATION``.  For more about packages andexporting see the :manual:`cmake-packages(7)` manual... _`Include Directories and Usage Requirements`:Include Directories and Usage Requirements^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Include directories require some special consideration when specified as usagerequirements and when used with generator expressions.  The:command:`target_include_directories` command accepts both relative andabsolute include directories:.. code-block:: cmake  add_library(lib1 lib1.cpp)  target_include_directories(lib1 PRIVATE    /absolute/path    relative/path  )Relative paths are interpreted relative to the source directory where thecommand appears.  Relative paths are not allowed in the:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of :prop_tgt:`IMPORTED` targets.In cases where a non-trivial generator expression is used, the``INSTALL_PREFIX`` expression may be used within the argument of an``INSTALL_INTERFACE`` expression.  It is a replacement marker whichexpands to the installation prefix when imported by a consuming project.Include directories usage requirements commonly differ between the build-treeand the install-tree.  The ``BUILD_INTERFACE`` and ``INSTALL_INTERFACE``generator expressions can be used to describe separate usage requirementsbased on the usage location.  Relative paths are allowed within the``INSTALL_INTERFACE`` expression and are interpreted relative to theinstallation prefix.  For example:.. code-block:: cmake  add_library(ClimbingStats climbingstats.cpp)  target_include_directories(ClimbingStats INTERFACE    $<BUILD_INTERFACE:${CMAKE_CURRENT_BINARY_DIR}/generated>    $<INSTALL_INTERFACE:/absolute/path>    $<INSTALL_INTERFACE:relative/path>    $<INSTALL_INTERFACE:$<INSTALL_PREFIX>/$<CONFIG>/generated>  )Two convenience APIs are provided relating to include directories usagerequirements.  The :variable:`CMAKE_INCLUDE_CURRENT_DIR_IN_INTERFACE` variablemay be enabled, with an equivalent effect to:.. code-block:: cmake  set_property(TARGET tgt APPEND PROPERTY INTERFACE_INCLUDE_DIRECTORIES    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR};${CMAKE_CURRENT_BINARY_DIR}>  )for each target affected.  The convenience for installed targets isan ``INCLUDES DESTINATION`` component with the :command:`install(TARGETS)`command:.. code-block:: cmake  install(TARGETS foo bar bat EXPORT tgts ${dest_args}    INCLUDES DESTINATION include  )  install(EXPORT tgts ${other_args})  install(FILES ${headers} DESTINATION include)This is equivalent to appending ``${CMAKE_INSTALL_PREFIX}/include`` to the:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of each of the installed:prop_tgt:`IMPORTED` targets when generated by :command:`install(EXPORT)`.When the :prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES` of an:ref:`imported target <Imported targets>` is consumed, the entries in theproperty are treated as ``SYSTEM`` include directories, as if they werelisted in the :prop_tgt:`INTERFACE_SYSTEM_INCLUDE_DIRECTORIES` of thedependency. This can result in omission of compiler warnings for headersfound in those directories.  This behavior for :ref:`imported targets` maybe controlled by setting the :prop_tgt:`NO_SYSTEM_FROM_IMPORTED` targetproperty on the *consumers* of imported targets.If a binary target is linked transitively to a macOS :prop_tgt:`FRAMEWORK`, the``Headers`` directory of the framework is also treated as a usage requirement.This has the same effect as passing the framework directory as an includedirectory.Link Libraries and Generator Expressions----------------------------------------Like build specifications, :prop_tgt:`link libraries <LINK_LIBRARIES>` may bespecified with generator expression conditions.  However, as consumption ofusage requirements is based on collection from linked dependencies, there isan additional limitation that the link dependencies must form a "directedacyclic graph".  That is, if linking to a target is dependent on the value ofa target property, that target property may not be dependent on the linkeddependencies:.. code-block:: cmake  add_library(lib1 lib1.cpp)  add_library(lib2 lib2.cpp)  target_link_libraries(lib1 PUBLIC    $<$<TARGET_PROPERTY:POSITION_INDEPENDENT_CODE>:lib2>  )  add_library(lib3 lib3.cpp)  set_property(TARGET lib3 PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 lib1 lib3)As the value of the :prop_tgt:`POSITION_INDEPENDENT_CODE` property ofthe ``exe1`` target is dependent on the linked libraries (``lib3``), and theedge of linking ``exe1`` is determined by the same:prop_tgt:`POSITION_INDEPENDENT_CODE` property, the dependency graph abovecontains a cycle.  :manual:`cmake(1)` issues a diagnostic in this case... _`Output Artifacts`:Output Artifacts----------------The buildsystem targets created by the :command:`add_library` and:command:`add_executable` commands create rules to create binary outputs.The exact output location of the binaries can only be determined atgenerate-time because it can depend on the build-configuration and thelink-language of linked dependencies etc.  ``TARGET_FILE``,``TARGET_LINKER_FILE`` and related expressions can be used to access thename and location of generated binaries.  These expressions do not workfor ``OBJECT`` libraries however, as there is no single file generatedby such libraries which is relevant to the expressions.There are three kinds of output artifacts that may be build by targetsas detailed in the following sections.  Their classification differsbetween DLL platforms and non-DLL platforms.  All Windows-basedsystems including Cygwin are DLL platforms... _`Runtime Output Artifacts`:Runtime Output Artifacts^^^^^^^^^^^^^^^^^^^^^^^^A *runtime* output artifact of a buildsystem target may be:* The executable file (e.g. ``.exe``) of an executable target  created by the :command:`add_executable` command.* On DLL platforms: the executable file (e.g. ``.dll``) of a shared  library target created by the :command:`add_library` command  with the ``SHARED`` option.The :prop_tgt:`RUNTIME_OUTPUT_DIRECTORY` and :prop_tgt:`RUNTIME_OUTPUT_NAME`target properties may be used to control runtime output artifact locationsand names in the build tree... _`Library Output Artifacts`:Library Output Artifacts^^^^^^^^^^^^^^^^^^^^^^^^A *library* output artifact of a buildsystem target may be:* The loadable module file (e.g. ``.dll`` or ``.so``) of a module  library target created by the :command:`add_library` command  with the ``MODULE`` option.* On non-DLL platforms: the shared library file (e.g. ``.so`` or ``.dylib``)  of a shared library target created by the :command:`add_library`  command with the ``SHARED`` option.The :prop_tgt:`LIBRARY_OUTPUT_DIRECTORY` and :prop_tgt:`LIBRARY_OUTPUT_NAME`target properties may be used to control library output artifact locationsand names in the build tree... _`Archive Output Artifacts`:Archive Output Artifacts^^^^^^^^^^^^^^^^^^^^^^^^An *archive* output artifact of a buildsystem target may be:* The static library file (e.g. ``.lib`` or ``.a``) of a static  library target created by the :command:`add_library` command  with the ``STATIC`` option.* On DLL platforms: the import library file (e.g. ``.lib``) of a shared  library target created by the :command:`add_library` command  with the ``SHARED`` option.  This file is only guaranteed to exist if  the library exports at least one unmanaged symbol.* On DLL platforms: the import library file (e.g. ``.lib``) of an  executable target created by the :command:`add_executable` command  when its :prop_tgt:`ENABLE_EXPORTS` target property is set.* On AIX: the linker import file (e.g. ``.imp``) of an executable target  created by the :command:`add_executable` command when its  :prop_tgt:`ENABLE_EXPORTS` target property is set.The :prop_tgt:`ARCHIVE_OUTPUT_DIRECTORY` and :prop_tgt:`ARCHIVE_OUTPUT_NAME`target properties may be used to control archive output artifact locationsand names in the build tree.Directory-Scoped Commands-------------------------The :command:`target_include_directories`,:command:`target_compile_definitions` and:command:`target_compile_options` commands have an effect on only onetarget at a time.  The commands :command:`add_compile_definitions`,:command:`add_compile_options` and :command:`include_directories` havea similar function, but operate at directory scope instead of targetscope for convenience.Pseudo Targets==============Some target types do not represent outputs of the buildsystem, but only inputssuch as external dependencies, aliases or other non-build artifacts.  Pseudotargets are not represented in the generated buildsystem... _`Imported Targets`:Imported Targets----------------An :prop_tgt:`IMPORTED` target represents a pre-existing dependency.  Usuallysuch targets are defined by an upstream package and should be treated asimmutable. After declaring an :prop_tgt:`IMPORTED` target one can adjust itstarget properties by using the customary commands such as:command:`target_compile_definitions`, :command:`target_include_directories`,:command:`target_compile_options` or :command:`target_link_libraries` just likewith any other regular target.:prop_tgt:`IMPORTED` targets may have the same usage requirement propertiespopulated as binary targets, such as:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,:prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,:prop_tgt:`INTERFACE_COMPILE_OPTIONS`,:prop_tgt:`INTERFACE_LINK_LIBRARIES`, and:prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.The :prop_tgt:`LOCATION` may also be read from an IMPORTED target, though thereis rarely reason to do so.  Commands such as :command:`add_custom_command` cantransparently use an :prop_tgt:`IMPORTED` :prop_tgt:`EXECUTABLE <TYPE>` targetas a ``COMMAND`` executable.The scope of the definition of an :prop_tgt:`IMPORTED` target is the directorywhere it was defined.  It may be accessed and used from subdirectories, butnot from parent directories or sibling directories.  The scope is similar tothe scope of a cmake variable.It is also possible to define a ``GLOBAL`` :prop_tgt:`IMPORTED` target which isaccessible globally in the buildsystem.See the :manual:`cmake-packages(7)` manual for more on creating packageswith :prop_tgt:`IMPORTED` targets... _`Alias Targets`:Alias Targets-------------An ``ALIAS`` target is a name which may be used interchangeably witha binary target name in read-only contexts.  A primary use-case for ``ALIAS``targets is for example or unit test executables accompanying a library, whichmay be part of the same buildsystem or built separately based on userconfiguration... code-block:: cmake  add_library(lib1 lib1.cpp)  install(TARGETS lib1 EXPORT lib1Export ${dest_args})  install(EXPORT lib1Export NAMESPACE Upstream:: ${other_args})  add_library(Upstream::lib1 ALIAS lib1)In another directory, we can link unconditionally to the ``Upstream::lib1``target, which may be an :prop_tgt:`IMPORTED` target from a package, or an``ALIAS`` target if built as part of the same buildsystem... code-block:: cmake  if (NOT TARGET Upstream::lib1)    find_package(lib1 REQUIRED)  endif()  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 Upstream::lib1)``ALIAS`` targets are not mutable, installable or exportable.  They areentirely local to the buildsystem description.  A name can be tested forwhether it is an ``ALIAS`` name by reading the :prop_tgt:`ALIASED_TARGET`property from it:.. code-block:: cmake  get_target_property(_aliased Upstream::lib1 ALIASED_TARGET)  if(_aliased)    message(STATUS "The name Upstream::lib1 is an ALIAS for ${_aliased}.")  endif().. _`Interface Libraries`:Interface Libraries-------------------An ``INTERFACE`` target has no :prop_tgt:`LOCATION` and is mutable, but isotherwise similar to an :prop_tgt:`IMPORTED` target.It may specify usage requirements such as:prop_tgt:`INTERFACE_INCLUDE_DIRECTORIES`,:prop_tgt:`INTERFACE_COMPILE_DEFINITIONS`,:prop_tgt:`INTERFACE_COMPILE_OPTIONS`,:prop_tgt:`INTERFACE_LINK_LIBRARIES`,:prop_tgt:`INTERFACE_SOURCES`,and :prop_tgt:`INTERFACE_POSITION_INDEPENDENT_CODE`.Only the ``INTERFACE`` modes of the :command:`target_include_directories`,:command:`target_compile_definitions`, :command:`target_compile_options`,:command:`target_sources`, and :command:`target_link_libraries` commandsmay be used with ``INTERFACE`` libraries.A primary use-case for ``INTERFACE`` libraries is header-only libraries... code-block:: cmake  add_library(Eigen INTERFACE)  target_include_directories(Eigen INTERFACE    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>    $<INSTALL_INTERFACE:include/Eigen>  )  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 Eigen)Here, the usage requirements from the ``Eigen`` target are consumed and usedwhen compiling, but it has no effect on linking.Another use-case is to employ an entirely target-focussed design for usagerequirements:.. code-block:: cmake  add_library(pic_on INTERFACE)  set_property(TARGET pic_on PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE ON)  add_library(pic_off INTERFACE)  set_property(TARGET pic_off PROPERTY INTERFACE_POSITION_INDEPENDENT_CODE OFF)  add_library(enable_rtti INTERFACE)  target_compile_options(enable_rtti INTERFACE    $<$<OR:$<COMPILER_ID:GNU>,$<COMPILER_ID:Clang>>:-rtti>  )  add_executable(exe1 exe1.cpp)  target_link_libraries(exe1 pic_on enable_rtti)This way, the build specification of ``exe1`` is expressed entirely as linkedtargets, and the complexity of compiler-specific flags is encapsulated in an``INTERFACE`` library target.The properties permitted to be set on or read from an ``INTERFACE`` libraryare:* Properties matching ``INTERFACE_*``* Built-in properties matching ``COMPATIBLE_INTERFACE_*``* ``EXPORT_NAME``* ``EXPORT_PROPERTIES``* ``IMPORTED``* ``MANUALLY_ADDED_DEPENDENCIES``* ``NAME``* Properties matching ``IMPORTED_LIBNAME_*``* Properties matching ``MAP_IMPORTED_CONFIG_*````INTERFACE`` libraries may be installed and exported.  Any content they referto must be installed separately:.. code-block:: cmake  add_library(Eigen INTERFACE)  target_include_directories(Eigen INTERFACE    $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/src>    $<INSTALL_INTERFACE:include/Eigen>  )  install(TARGETS Eigen EXPORT eigenExport)  install(EXPORT eigenExport NAMESPACE Upstream::    DESTINATION lib/cmake/Eigen  )  install(FILES      ${CMAKE_CURRENT_SOURCE_DIR}/src/eigen.h      ${CMAKE_CURRENT_SOURCE_DIR}/src/vector.h      ${CMAKE_CURRENT_SOURCE_DIR}/src/matrix.h    DESTINATION include/Eigen  )
 |