I have a code base (say, a repository) containing several software components (executables, libraries). All is built using CMake with individual targets for each component.
Now, I know that the modern way to set compiler flags is target-specific:
target_compile_options(foo PRIVATE -Werror -Wall -Wextra -pedantic)
target_compile_features(foo PRIVATE cxx_std_17)
However, I want to have the same flags for all targets in the entire project. Writing these lines for each single target leads to code duplication and is error-prone (especially when the number of flags increases). Is there some way to attach compiler flags to projects rather than targets?
I know that I can add options to the top-level directory like that:
add_compile_options(-Werror -Wall -Wextra -pedantic)
But I'm not sure if that's the way to go, and also I'm not sure how to do the same for the compile features (cxx_std_17).