If you can modify all relevant targets, I recommend using target_compile_options
, target_compile_definitions
and or/ target_link_options
to achieve your goals. You could introduce a cmake function, if the same compiler/linker options are required for multiple targets.
target_compile_options(my_target PRIVATE -Weffc++)
If you want to target a subdirectory, you should be able to use add_compile_options
/add_compile_definitions
/add_link_options
in a new scope using a function to restruct the scope for the settings. Note that you only need a function, if the options must not apply to every target added in the same CMakeLists.txt
file after adding the subdirectory and to targets created in subdirectories added from the CMakeLists.txt
file after adding the subdirectory. Using a function anyways doesn't "hurt" though and may even reduce the number of things you need to consider for future edits.
function(add_subdir_with_flags DIR)
add_compile_options(-Weffc++)
add_subdirectory(${DIR} ${ARGN}) # ARGN used to allow specifying the build dir for the subdir
endfunction()
...
add_subdir_with_flags(subdir1)
add_subdir_with_flags(subdir2 builddir2)
Note: If you want to go down to the source file level, you could do so by adding compile options on a per-file basis, but you should only use this option, if not all source files of a target should be compiled with these options.
# apply options only to foo.cpp and bar.cpp
set_source_files_properties(foo.cpp bar.cpp PROPERTIES COMPILE_OPTIONS -Weffc++)