Set CFLAGS and CXXFLAGS options using CMake
Asked Answered
S

4

116

I just want to debug some code running on Linux and I need a debug build (-O0 -ggdb). So I added these things to my CMakeLists.txt file:

set(CMAKE_BUILD_TYPE DEBUG)
set(CMAKE_C_FLAGS "-O0 -ggdb")
set(CMAKE_C_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_C_FLAGS_RELEASE "-O0 -ggdb")
set(CMAKE_CXX_FLAGS "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_DEBUG "-O0 -ggdb")
set(CMAKE_CXX_FLAGS_RELEASE "-O0 -ggdb")

When I tried to compile I turned verbose on using make VERBOSE=1 And I observed the output, like this

... /usr/bin/c++ -D_BSD_SOURCE **-O0 -ggdb** -Wnon-virtual-dtor 
-Wno-long-long -ansi -Wundef -Wcast-align -Wchar-subscripts -Wall -W 
-Wpointer-arith -Wformat-security -fno-exceptions -DQT_NO_EXCEPTIONS 
-fno-check-new -fno-common -Woverloaded-virtual -fno-threadsafe-statics 
-fvisibility=hidden -fvisibility-inlines-hidden **-g -O2** 
-fno-reorder-blocks -fno-schedule-insns -fno-inline ...

Apparently the code is compiled with "-g -O2" and this is not what I want. How can I force it to use "-O0 -ggdb" only?

Solemn answered 10/4, 2012 at 9:10 Comment(1)
If you want a debuggable build, just do a debug configure at the command line. "cmake -DCMAKE_BUILD_TYPE=Debug". The resulting build will have the debug flags on for the given build system. No reason to modify the cmake file itself. You can also send in the CMAKE_C_FLAGS value with another -D argument.Niemeyer
I
110

You need to set the flags after the project command in your CMakeLists.txt.

Also, if you're calling include(${QT_USE_FILE}) or add_definitions(${QT_DEFINITIONS}), you should include these set commands after the Qt ones since these would append further flags. If that is the case, you maybe just want to append your flags to the Qt ones, so change to e.g.

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb")
Industrial answered 10/4, 2012 at 10:33 Comment(5)
I did put those flags after the project command. But the output is the same.Solemn
I just noticed the QT_NO_EXCEPTIONS in your flags, so I've extended my answer a bit. I'm guessing that ${QT_DEFINITIONS} is appending the -g -O2.Industrial
Yes, This is it. Putting those set commands after the Qt ones solves the problem. Thank you very much.Solemn
Thanks a lot for the hint about the project() command! This solved a problem where we want set the CMAKE_CXX_FLAGS for all projects via a common include but did so after the project command.Steading
I am getting "cl : Command line warning D9002 : ignoring unknown option '-ggdb'"Borlow
I
35

The easiest solution working fine for me is this:

export CFLAGS=-ggdb
export CXXFLAGS=-ggdb

CMake will append them to all configurations' flags. Just make sure to clear CMake cache.

Ihs answered 25/1, 2017 at 4:48 Comment(4)
Setting the flags in the environment like this is the only way to do it without modifying your CMakeLists.txt. Note that the environment value is only read on first configuration and is then put into the cache, so if you want to change this you need to clear the CMake cache and re-run the configure step with the new values in the env. Docs: cmake.org/cmake/help/latest/envvar/CFLAGS.html cmake.org/cmake/help/latest/envvar/CXXFLAGS.htmlAlanealanine
That's not "easiest", that's the only right way to do it.Vestibule
Minor catch, if the generator is configuring a specific flag for a specific build type (e.g. in CMAKE_CXX_FLAGS_DEBUG etc.), then including it in CXXFLAGS (and thereby injecting it into CMAKE_CXX_FLAGS) doesn't actually suffice for passing the option to the compiler. The per build type configuration takes precedence over user input.Advertence
But just how do you clear the CMake cache? If you're building into a subdir like _build, erase and recreate it.Vaishnava
D
19

You must change the CMake CFLAGS/CXXFLAGS default flags.

According to CMAKE_BUILD_TYPE={DEBUG/MINSIZEREL/RELWITHDEBINFO/RELEASE}, put in the main CMakeLists.txt one of:

For C

set(CMAKE_C_FLAGS_DEBUG "put your flags")
set(CMAKE_C_FLAGS_MINSIZEREL "put your flags")
set(CMAKE_C_FLAGS_RELWITHDEBINFO "put your flags")
set(CMAKE_C_FLAGS_RELEASE "put your flags")

For C++

set(CMAKE_CXX_FLAGS_DEBUG "put your flags")
set(CMAKE_CXX_FLAGS_MINSIZEREL "put your flags")
set(CMAKE_CXX_FLAGS_RELWITHDEBINFO "put your flags")
set(CMAKE_CXX_FLAGS_RELEASE "put your flags")

This will override the values defined in CMakeCache.txt.

Dipetalous answered 27/8, 2015 at 13:16 Comment(2)
that overrides your cmake flags that are current. so it breaks debugging unless you add the -g flag yourself (gcc)Excrete
Is there the same think using "Modern Cmake" syntax?Flaunch
W
8

On Unix systems, for several projects, I added these lines into the CMakeLists.txt and it was compiling successfully because base (/usr/include) and local includes (/usr/local/include) go into separated directories:

set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -I/usr/local/include -L/usr/local/lib")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -I/usr/local/include")
set(CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} -L/usr/local/lib")

It appends the correct directory, including paths for the C and C++ compiler flags and the correct directory path for the linker flags.

Note: C++ compiler (c++) doesn't support -L, so we have to use CMAKE_EXE_LINKER_FLAGS

Washwoman answered 18/4, 2016 at 18:55 Comment(2)
Isnt it better to use include_directories instead of the -I flag?Trotline
Thank you so much. I was placing my linker flags in CMAKE_CXX_FLAGS, which caused them to be ignored. Now that I placed them in CMAKE_EXE_LINKER_FLAGS based on your answer, they are working just fine.Tolentino

© 2022 - 2024 — McMap. All rights reserved.