In CMake, the flags for the C++ compiler can be influenced in various ways: setting CMAKE_CXX_FLAGS
manually, using add_definitions()
, forcing a certain C++ standard, and so forth.
In order to compile a target in the same project with different rules (a precompiled header, in my case), I need to reproduce the exact command that is used to compile files added by a command like add_executable()
in this directory.
Reading CMAKE_CXX_FLAGS
only returns the value set to it explicitly, CMAKE_CXX_FLAGS_DEBUG
and siblings only list default Debug/Release options. There is a special functions to retrieve the flags from add_definitions()
and add_compiler_options()
, but none seem to be able to return the final command line.
How can I get all flags passed to the compiler into a CMake variable?
To answer my own question: It seems like the only way of getting all compiler flags is to reconstruct them from the various sources. The code I'm working with now is the following (for GCC):
This could be extended to also include options induced by
add_compiler_options()
and more.Easiest way is to use
make VERBOSE=1
when compiling.This will do a single-threaded build, and make will print every shell command it runs just before it runs it. So you'll see output like: