112

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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
majie
  • 1,449
  • 2
  • 11
  • 10
  • 2
    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. – Atif May 16 '16 at 13:09

4 Answers4

107

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")
Fraser
  • 74,704
  • 20
  • 238
  • 215
  • I did put those flags after the project command. But the output is the same. – majie Apr 11 '12 at 01:40
  • 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`. – Fraser Apr 11 '12 at 02:26
  • 1
    Yes, This is it. Putting those `set` commands after the Qt ones solves the problem. Thank you very much. – majie Apr 11 '12 at 14:18
  • 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. – darkdragon Apr 28 '21 at 09:33
  • I am getting "cl : Command line warning D9002 : ignoring unknown option '-ggdb'" – Jaroslav Záruba Feb 19 '22 at 12:53
31

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.

Virus_7
  • 580
  • 4
  • 8
  • 10
    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: https://cmake.org/cmake/help/latest/envvar/CFLAGS.html https://cmake.org/cmake/help/latest/envvar/CXXFLAGS.html – bleater Jan 07 '20 at 01:25
  • 5
    That's not "easiest", that's the only right way to do it. – Slava Mar 11 '20 at 14:36
  • 1
    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. – Ext3h Oct 14 '20 at 08:20
  • 1
    But just _how_ do you clear the CMake cache? If you're building into a subdir like _build, erase and recreate it. – JimB Jan 19 '21 at 20:04
18

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
carl-erwin
  • 181
  • 1
  • 3
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

Peter David Carter
  • 2,548
  • 8
  • 25
  • 44
sbz
  • 893
  • 11
  • 9
  • 3
    Isnt it better to use `include_directories` instead of the `-I` flag? – Tejas Kale Feb 21 '19 at 16:59
  • 1
    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. – Nahiyan Aug 16 '20 at 05:33