Due to the following warning:
CMake Error at test/CMakeLists.txt:29 (get_target_property):
The LOCATION property may not be read from target \"my_exe\". Use t
While generator expression is stored at configuration stage (when corresponded CMake command is executed), evaluation of generator expressions is performed at build stage.
This is why message()
command prints generator expression in non-dereferenced form: value denoted by the generator expression is not known at this stage.
Moreover, CMake never dereferences generator expressions by itself. Instead, it generates appropriate string in the build file, which is then interpreted by build utility (make
, Visual Studio
, etc.).
Note, that not every CMake command accepts generator expressions. Each possible usage of generator expressions is explicitely described in documentation for specific command. Moreover, different CMake command flows or different options have different policy about using of generator expressions.
For example, command flow
add_test(NAME <name> COMMAND <executable>)
accepts generator expressions for COMMAND
option,
but command flow
add_test(<name> <executable>)
doesn't!
Another example of policies difference:
install(DIRECTORY <dir> DESTINATION <dest>)
In this command flow generator expressions are allowed for DESTINATION
, but not for DIRECTORY
option.
Again, read documentation carefully.
Here is a quick and easy way to print the value of a generator expression:
add_custom_target(print
${CMAKE_COMMAND} -E echo $<1:hello> $<0:world>
)
In this example, if you run cmake .
and then make print
, you will see "hello" (without the quotation marks) in the output.
However, if you just use message($<1:hello> $<0:world>)
, you will see "$<1:hello> $<0:world>
" as output (again, without the quotation marks).