Learning the basic rules of general expression syntax
To use generator expressions, we’ll need to add them to a CMake listfile through a command that supports generator expression evaluation. Most of the target-specific commands do, and there are plenty of others (review the official documentation of a particular command to learn more). A command that is often used with generator exception is target_compile_definitions()
. To use a generator expression, we’ll need to provide it as a command argument like so:
target_compile_definitions(foo PUBLIC BAR=$<TARGET_FILE:baz>)
This command adds a -D
definition flag to the compiler's arguments (ignore PUBLIC
for now) that sets the BAR
preprocessor definition to the path at which the binary artifact of the foo
target will be produced. This works, because the generator expression is stored in the current form in a variable. The expansion is effectively postponed until the generation stage, when many things are fully configured...