0
votes

I have a CMake project that I want to be able to compile using g++, MinGW/g++ (4.x each), Clang (3.x) and MSVC++ (>= V8).

I want to have a precompiler symbol with the compiler arguments or command line.

This is useful to see whether optimization was enabled and which switches were enabled at the command line.

E.g. a program compiled with g++ -O3 -DNDEBUG -g0 should know that the flags are "-O3 -DNDEBUG -g0". When writing manual Makefiles, I could simply collect the flags in a variable and then additionally pass this to the compiler yielding a command line g++ -O3 -DNDEBUG -g0 -DCOMPILER_ARGS="-O3 -DNDEBUG -g0".

How can I achieve this using CMake?

2

2 Answers

0
votes

add_definitions("-DCOMPILER_ARGS=\"${CMAKE_CXX_FLAGS}\")

But be aware, that CMAKE_CXX_FLAGS variable contains only flags set by user. If you wish whole set of compile flags, use target property COMPILE_FLAGS. This value can be obtained by get_target_property() function.

0
votes

For all options except MSVC, rather than creating a preprocessor definition, you should probably just use make VERBOSE=1 when building or add

set(CMAKE_VERBOSE_MAKEFILE ON)

to your CMakeLists.txt.

For MSVC, you can examine each project's command line arguments from within the IDE: Property Pages -> Configuration Properties -> C/C++ -> Command Line and Property Pages -> Configuration Properties -> Linker -> Command Line.