TExcelTAD
Home › Modern CMake (with CTest & CPack) › Target Properties

Target Properties

Every target_* command you have used so far is a convenience wrapper around properties: named values stored on a target. target_include_directories(lib PUBLIC inc) appends inc to both INCLUDE_DIRECTORIES and INTERFACE_INCLUDE_DIRECTORIES. Knowing the properties lets you read and set anything CMake knows about a target.

Reading and writing properties

cmake
set_target_properties(app PROPERTIES OUTPUT_NAME "tad-app" CXX_EXTENSIONS OFF)
set_property(TARGET app APPEND PROPERTY COMPILE_DEFINITIONS EXTRA=1)
get_target_property(var app OUTPUT_NAME)           # var = tad-app (or var-NOTFOUND)

Properties also exist on other scopes (GLOBAL, DIRECTORY, SOURCE, INSTALL, TEST, CACHE), set with set_property(<SCOPE> …) and read with get_property.

bash · try it yourself
mkdir -p props && cd props
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Props VERSION 2.1.0 LANGUAGES CXX)
add_library(core STATIC core.cpp)
target_include_directories(core PUBLIC include PRIVATE src)
target_compile_definitions(core PUBLIC CORE_API=1 PRIVATE CORE_BUILDING)
target_compile_features(core PUBLIC cxx_std_20)
set_target_properties(core PROPERTIES VERSION ${PROJECT_VERSION} OUTPUT_NAME tadcore)

include(CMakePrintHelpers)
cmake_print_properties(TARGETS core PROPERTIES
  TYPE OUTPUT_NAME VERSION
  INCLUDE_DIRECTORIES INTERFACE_INCLUDE_DIRECTORIES
  COMPILE_DEFINITIONS INTERFACE_COMPILE_DEFINITIONS
  INTERFACE_COMPILE_FEATURES)
get_target_property(missing core SOVERSION)
message("SOVERSION = ${missing}")
CM
mkdir -p include src && echo 'int core() { return 0; }' > core.cpp
# cmake_print_properties reports with message(STATUS), so keep the status lines this time
cmake -S . -B build -G Ninja 2>&1 | grep -v -e '^--' -e '^
  

 Properties for TARGET core:
   core.TYPE = "STATIC_LIBRARY"
   core.OUTPUT_NAME = "tadcore"
   core.VERSION = "2.1.0"
   core.INCLUDE_DIRECTORIES = "/home/learner/props/include;/home/learner/props/src"
   core.INTERFACE_INCLUDE_DIRECTORIES = "/home/learner/props/include"
   core.COMPILE_DEFINITIONS = "CORE_API=1;CORE_BUILDING"
   core.INTERFACE_COMPILE_DEFINITIONS = "CORE_API=1"
   core.INTERFACE_COMPILE_FEATURES = "cxx_std_20"
SOVERSION = missing-NOTFOUND

This is the same build/usage split as before, now visible: PRIVATE src only in INCLUDE_DIRECTORIES, PUBLIC include in both, and CORE_BUILDING only in COMPILE_DEFINITIONS. cmake_print_properties (from the CMakePrintHelpers module) is a handy debugging tool; an unset property reads as <var>-NOTFOUND.

Default values from CMAKE_ variables

Many target properties are initialised from a variable named CMAKE_<PROPERTY> at the moment the target is created. That's how set(CMAKE_CXX_STANDARD 20) works: it sets the default for the CXX_STANDARD property of later targets. Order matters:

bash · try it yourself
mkdir -p order && cd order
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Order LANGUAGES CXX)
add_library(early STATIC a.cpp)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
add_library(late STATIC a.cpp)
foreach(t early late)
  get_target_property(std ${t} CXX_STANDARD)
  get_target_property(pic ${t} POSITION_INDEPENDENT_CODE)
  message("${t}: CXX_STANDARD=${std} POSITION_INDEPENDENT_CODE=${pic}")
endforeach()
CM
echo 'int a() { return 0; }' > a.cpp
cmake -S . -B build -G Ninja --log-level=NOTICE | grep -v '^--'
early: CXX_STANDARD=std-NOTFOUND POSITION_INDEPENDENT_CODE=pic-NOTFOUND
late: CXX_STANDARD=20 POSITION_INDEPENDENT_CODE=ON

So set these variables near the top of the top-level CMakeLists.txt, before any add_subdirectory.

Asking CMake about a property

The reference documentation for every property, command, variable, module and policy is built into CMake. --help-property-list lists the property names, and --help-property <name> prints one page as reStructuredText (the same source as the online docs). A page can be long, so pipe it through head:

bash · try it yourself
cmake --help-property POSITION_INDEPENDENT_CODE | head -n 12
echo ---
cmake --help-property-list | grep '^INTERFACE_' | head -n 3
POSITION_INDEPENDENT_CODE
-------------------------

A target property that specifies whether to create a target that has
position-independent code enabled.

The ``POSITION_INDEPENDENT_CODE`` target property determines whether
position-independent executables or libraries will be created.  This
property is ``True`` by default for ``SHARED`` and ``MODULE`` library
targets.  For other targets, this property is initialized by the value
of the ``CMAKE_POSITION_INDEPENDENT_CODE`` variable if it is set
when the target is created, or ``False`` otherwise.
---
INTERFACE_AUTOMOC_MACRO_NAMES
INTERFACE_AUTOUIC_OPTIONS
INTERFACE_COMPILE_DEFINITIONS

The same works for --help-command target_link_libraries, --help-variable CMAKE_BUILD_TYPE, --help-module FetchContent and --help-policy CMP0168, which is useful on a machine without a browser. One trap: some distributions split the documentation into a separate package (or slim container images delete it). Without those files CMake gives a misleading message, reporting a perfectly valid property as unknown: Argument "POSITION_INDEPENDENT_CODE" to --help-property is not a CMake property. If you see that for a name you know exists, the docs are missing, not the property.

The online reference at cmake.org/cmake/help/latest/ has the same pages, with a version switcher for older releases.

Custom properties

You can define your own properties to attach metadata to targets, for example a component name that a later function reads:

cmake
define_property(TARGET PROPERTY TAD_COMPONENT BRIEF_DOCS "Packaging component")
set_property(TARGET core PROPERTY TAD_COMPONENT runtime)

Prefix custom properties with your project name so they can't collide with future CMake properties.

Test yourself

What does target_include_directories(lib PUBLIC inc) change?

  1. Only INCLUDE_DIRECTORIES
  2. Only INTERFACE_INCLUDE_DIRECTORIES
  3. Both INCLUDE_DIRECTORIES and INTERFACE_INCLUDE_DIRECTORIES
  4. The directory property INCLUDE_DIRECTORIES

set(CMAKE_CXX_STANDARD 20) is placed after add_library(early ...). What is early's CXX_STANDARD?

  1. 20
  2. Not set: the variable only initialises targets created after it
  3. 17
  4. An error

How do you read the documentation of a property offline?

  1. cmake --help-property <NAME>
  2. man cmake-property
  3. cmake --docs <NAME>
  4. It's not possible