add_compile_options and propagation to CMAKE_C_FLAGS and toolchain files etc.

How should an invocation of add_compile_options be propagated to CMAKE_C_FLAGS? Should that be handled by cmake internals? Similarly what is the suggested way to propagate compile options to an external project built with a toolchain file?

Why should they be? add_compile_options works on the directory property and as such only affects the current directory (and subsequent children).

Understood. How should then e.g. a sanitize build be propagated to external projects that are build by using a toolchain file?

They’d need to be added to the usage requirements of the targets in question. I prefer to make sanitization an option in the build and then add them as INTERFACE requirements to a target that everything links to. If you want to force sanitizers on consumers, you can opt to not put the flags behind $<BUILD_INTERFACE>. You can see an example of how to set up the target in VTK.

But here every target needs to link against the interface target?

Any ideas how to introduce compiler warnings in an old cmake project?


If you don’t want to edit CMake code, I think all you have is the CMAKE_<LANG>_FLAGS cache variables.