Don’t modify CMAKE_CXX_FLAGS
in your project. Leave variables like those for the user to manipulate. If the project needs to add flags project-wide, it can use add_compile_options()
from the top level CMakeLists.txt
instead. If you need to leave out some of those options for a specific target, then that suggests those flags shouldn’t have been applied project-wide to begin with. That said, there may be scenarios where that is justified (and I have had to do this before myself). If you’ve set a common set of options at the top level, you could search for those specific options in the COMPILE_OPTIONS
directory property in a particular target’s CMakeLists.txt
and remove them from that property. This assumes you want to remove those options for all targets in that CMakeLists.txt
file’s directory scope. That’s the tradeoff to using this approach. It’s also quite fragile, because you’re assuming contents of those directory properties. It gets much worse if you start mixing in generator expressions, etc. Consider very carefully if this is really what you want to do before you commit to this approach.
1 Like