Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GCC -g vs -g3 GDB Flag: What is the Difference?

When compiling C source code with either gcc or Clang, I always use the -g flag to generate debugging information for gdb.

gcc -g -o helloworld helloworld.c

I noticed that some people recommend -g3 instead. What is the difference between the -g and -g3 flags? Also is there a difference between -g and -ggdb?

like image 768
haziz Avatar asked May 06 '12 23:05

haziz


People also ask

What is gcc and GDB?

gcc is a debugger by GNU project. Gdb can step through your source code line-by-line or even instruction by instruction. You may also watch the value of any variable at run-time. In additon, it also helps to identify the place and the reason making the program crash.

What does the G compiler flag do?

(debug) Inserting the `g' flag tells the compiler to insert more information about the source code into the executable than it normally would.


2 Answers

From the docs:

-g

Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information. On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but probably makes other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

...


-ggdb

Produce debugging information for use by GDB. This means to use the most expressive format available (DWARF 2, stabs, or the native format if neither of those are supported), including GDB extensions if at all possible.


-gvmslevel

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 3 includes extra information, such as all the macro definitions present in the program. Some debuggers support macro expansion when you use -g3.

like image 186
MByD Avatar answered Oct 03 '22 06:10

MByD


tl;dr: To answer your specific question, -g3 "includes extra information such as macro definitions... Some debuggers support macro expansion when you use -g3", while -g does not include this extra information.

The broader answer is that gcc supports four levels of debug information, from -g0 (debug information disabled) through -g3 (maximum debug information).

Specifying -g is equivalent to -g2. Curiously, the gcc docs say little about what information -g/-g2 includes or excludes:

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, and line number tables, but no information about local variables.

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

like image 35
U007D Avatar answered Oct 01 '22 06:10

U007D