1

CMAKE_C_FLAGS contains only common compilation flags for all build types. How can I get all compilation flags for current build type within CMakeLists.txt? So when CMAKE_BUILD_TYPE is "Release" I want to get all flags from CMAKE_C_FLAGS and CMAKE_C_FLAGS_RELEASE.

2 Answers2

1

One can define new variable

string(TOUPPER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_UPPER)

and then use "${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_${CMAKE_BUILD_TYPE_UPPER}}" to get all flags for current build type.

0

Within the CMakeCache.txt, there are also variables named after CMAKE_C_FLAGS_<config>:

cat CMakeCache.txt | ack CMAKE_C_FLAGS
CMAKE_C_FLAGS:STRING=
CMAKE_C_FLAGS_DEBUG:STRING=-g
CMAKE_C_FLAGS_MINSIZEREL:STRING=-Os -DNDEBUG
CMAKE_C_FLAGS_RELEASE:STRING=-O3 -DNDEBUG
CMAKE_C_FLAGS_RELWITHDEBINFO:STRING=-O2 -g -DNDEBUG

Note that based on how your build system is setup, these cache variables may not list all the flag effectively used.

J-Christophe
  • 1,975
  • 15
  • 13
  • How can I get all flags that will be used during compilation for current build type from CMake. – Anton Leontiev Aug 09 '18 at 14:28
  • Since flags can be different for every CMake targets, you will have to be more specific. It also be great if you could clarify what you mean by "during compilation". – J-Christophe Aug 09 '18 at 16:25
  • I mean common for all targets. "During compilation" means compilator flags, not linker flags. – Anton Leontiev Aug 10 '18 at 06:43
  • By "During compilation", do you mean you want to get the list of flags while the actual compilation is happening, or during the configuration of the project using CMake ? If you want the list of flags for a specific config (or build type) during the project configuration, you could simply use the variable listed above: `CMAKE_C_FLAGS_RELEASE` or `CMAKE_CXX_FLAGS_RELEASE`. – J-Christophe Aug 10 '18 at 18:08