...

/

Target Properties

Target Properties

Let's learn about target properties and transitive usage requirements, as well as how to deal with conflicting propagated properties in CMake.

Targets have properties that work similarly to fields of C++ objects. We can modify some of these properties, and others are read-only. CMake defines a large list of "known properties" that are available depending on the type of the target (executable, library, or custom). We can also add our own properties if we like.

Manipulating target properties

Use the following commands to manipulate the properties of a target:

Press + to interact
get_target_property(<var> <target> <property-name>)
set_target_properties(<target1> <target2> ...
PROPERTIES <prop1-name> <value1>
<prop2-name> <value2> ...)

To print a target property on the screen, we store it in the <var> variable and then message() it to the user; we have to read them one by one. On the other hand, setting properties on a target allows us to specify multiple properties at the same time, on multiple targets.

The concept of properties isn't unique to targets; CMake supports setting properties of other scopes as well: GLOBAL, DIRECTORY, SOURCE, INSTALL, TEST, and CACHE. To manipulate all kinds of properties, there are general get_property() and set_ property() commands. We can use these low-level commands to do exactly what the set_target_properties() command does, just with a bit more work:

set_property(TARGET <target> PROPERTY <name> <value>)

Generally, it's better to use as many high-level commands as possible. CMake offers more of these, even narrower in their scope, such as setting specific properties on a target. For example, add_dependencies(<target> <dep>) is appending dependencies to the MANUALLY_ADDED_DEPENDENCIES target property. In this case, we can query it with get_target_property() exactly as with any other property. However, we can't use set_target_property() to change it (it's read-only), as CMake insists on using the add_dependencies() command to restrict operations to appending only.

We'll introduce more property-setting commands when we discuss compiling and linking. Meanwhile, let's focus on how the properties of one target can transition to another.

What are transitive usage requirements?

Let's just agree that naming is hard, and sometimes one ends up with a result that's hard to understand. "Transitive usage requirements" is, unfortunately, one of those cryptic titles that we will encounter in the online CMake ...