Trying out examples
Everything is easier to grasp when there's a good practical example to support the theory. Obviously, we’d like to write some CMake code and try it out. However, since generator expressions aren’t evaluated until after the configuration is complete, we cannot use any configuration-time commands like message()
to experiment. We need to use some special tricks instead. To debug generator expressions, you can use either of these methods:
- Write it to a file (this particular version of the
file()
command supports generator expressions):file(GENERATE OUTPUT filename CONTENT "$<...>")
- Add a custom target and build it explicitly from the command line:
add_custom_target(gendbg COMMAND ${CMAKE_COMMAND} -E echo "$<...>")
I recommend the first option for simpler practice. Remember though, that we won’t be able to use all the expressions in these commands, as some are target specific. Having this covered, let’s...