如何使用 CMake 启用 gdb -g3 调试级别?

How to enabled gdb -g3 debug level with CMake?

如果您想在 CMake 上启用调试,请使用 cmake .. -DCMAKE_BUILD_TYPE=Debug,但这只会设置 -g,这是默认的调试级别。

-glevel Request debugging information and also use level to specify how much information. The default level is 2.

Level 0 produces no debug information at all. Thus, -g0 negates -g.

Level 1 produces minimal information, enough for making backtraces in parts of the program that you don't plan to debug. This includes descriptions of functions and external variables, but no information about local variables and no line numbers.

Level 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

如何将 cmake .. -DCMAKE_BUILD_TYPE=Debug 设置为 -g3 而不是 -g

  1. https://sourceware.org/bugzilla/show_bug.cgi?id=11067

设置CMAKE_C_FLAGS "-Og -g3 ...

您还可以设置 CMAKE_CXX_FLAGS_DEBUG,因此它只会在 CMake 在调试模式下构建时影响代码:

set(CMAKE_CXX_FLAGS_DEBUG "${CMAKE_CXX_FLAGS_DEBUG} -g3 -ggdb")

参考文献:

  1. CMake - compile with /MT instead of /MD