I've been told it's bad practice to do things like seting CFLAGS
directly in CMake, and that, instead, I should use the target_compile_definitions()
command.
Ok, but - what if I want to use similar/identical definitions for multiple (independent) targets? I don't want to repeat myself over and over again.
I see three possible ways:
The preferred one using target_compile_definitions(... INTERFACE/PUBLIC ...)
which would self-propagate the compiler definitions to targets depending on it via target_link_libraries()
command.
Using the set_property(TARGET target1 target2 ... APPEND PROPERTY COMPILE_DEFINITIONS ...)
to set the same definitions to multiple targets.
You may still use the "old commands" of add_definitions()
and remove_definitions()
to modify COMPILE_DEFINITIONS
directory property (which would pre-set all COMPILE_DEFINITIONS
target properties in this directories scope).