1

When you would like to enable debug on CMake, you use cmake .. -DCMAKE_BUILD_TYPE=Debug, but this only sets -g which is the default debug level.

-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.

How to make the cmake .. -DCMAKE_BUILD_TYPE=Debug set -g3 instead of -g?

  1. https://stackoverflow.com/a/864902/4934640
  2. https://sourceware.org/bugzilla/show_bug.cgi?id=11067
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144

2 Answers2

2

You can also set the CMAKE_CXX_FLAGS_DEBUG, so it only a affects the code when CMake is building in Debug mode:

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

References:

  1. Change default value of CMAKE_CXX_FLAGS_DEBUG and friends in CMake
  2. CMake - compile with /MT instead of /MD
Evandro Coan
  • 8,560
  • 11
  • 83
  • 144
1

set the CMAKE_C_FLAGS "-Og -g3 ...

0___________
  • 60,014
  • 4
  • 34
  • 74
  • Can you show an example? i.e., `cmake .. -DCMAKE_BUILD_TYPE=Debug -DCMAKE_C_FLAGS "-Og -g3"`? – Evandro Coan Feb 11 '21 at 03:14
  • I set it in the cmake file `set(CMAKE_C_FLAGS "-Og -g3 ...` Usually when I set g3 I optimize -Og (not supported by all targets but for sure by arm-none-eabi) – 0___________ Feb 11 '21 at 03:28