This question is related to Instruct Cmake to use CXX and CXXFLAGS when driving link? In the former question, we are trying to instruct CMake to use CXXFLAGS
wh
CMAKE_CXX_FLAGS
is used to add flags for all C++ targets. That's handy to pass general arguments like warning levels or to selected required C++ standards. It has no effect on C or Fortran targets and the user might pass additional flags.
add_compile_options
adds the options to all targets within the directory and its sub-directories. This is handy if you have a library in a directory and you want to add options to all the targets related to the library, but unrelated to all other targets. Additionally, add_compile_options
can handle arguments with generator expressions. The documentation explicitly states, that
This command can be used to add any options, but alternative commands exist to add preprocessor definitions (
target_compile_definitions()
andadd_definitions()
) or include directories (target_include_directories()
andinclude_directories()
).
add_definitions
is intended to pass pre-processor values of the type -DFOO -DBAR=32
(/D
on Windows) which defines and sets pre-processor variables. You could pass any flag, but the flags of the above form are detected and added to [COMPILE_DEFINITIONS][2]
property, which you can later read and change. Here, you can use generator expressions, too. The documentation mentions scopes for directories, targets and source files.For a given target, CMake will collect all flags from CMAKE_CXX_FLAGS
, the target's and directory's COMPILE_DEFINITIONS
and from all add_compile_options
which affect the target.
CMAKE_CXX_FLAGS
are not altered by the other commands or vice versa. This would violate the scope of these commands.