Cmake CFLAGS CXXFLAGS modification

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 :

 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 switched to verbose when using make VERBOSE=1 And I noticed a result 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 ... 

The code seems to be compiled using "-g -O2" , and that is not what I want. How to make it use "-O0 -ggdb" only?

+48
cmake cflags
Apr 10 '12 at 9:10
source share
4 answers

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

In addition, if you call include(${QT_USE_FILE}) or add_definitions(${QT_DEFINITIONS}) , you must include these set commands after Qt, as they will add extra flags. If so, you can just add your flags in Qt, so change, for example, to

 set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -O0 -ggdb") 
+43
Apr 10 2018-12-12T00:
source share

You must change the default FLAGS Cake C / CXX.

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

At 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

+7
Aug 27 '15 at 13:16
source share

The easiest solution for me works fine:

 export CFLAGS=-ggdb export CXXFLAGS=-ggdb 

CMake will add them to all configuration flags.

+3
Jan 25 '17 at 4:48 on
source share

On Unix systems for several projects, I added these lines to CMakeLists.txt and compiled successfully, because the base (/ usr / include) and local includes (/ usr / local / include) go to the separate 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 adds the correct directory, including the paths for the C and C ++ compiler flags, and the correct directory path for the linker flags.

Note: C ++ - the compiler (C ++) does not support -L, so we must use CMAKE_EXE_LINKER_FLAGS

+2
Apr 18 '16 at 18:55
source share



All Articles