178

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?

2
  • 31
    I came here because I'm using someone else's makefile, and the documentation is a beast to start from. Good question to have on StackOverflow to find an easy answer from google. Commented Sep 23, 2015 at 12:11
  • 12
    An overwhelming number of questions on SO are "covered my the documentation" as long as you are willing to dig far enough. That's not a valid reason to dismiss a question. Commented Oct 3, 2018 at 22:39

2 Answers 2

146

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Possible to explain still what is the difference between for instance "most expressive format" and "extra information"? Are these parameters complimentary? Many of them mention GDB...Thanks!
76

tl;dr: To answer your specific question, -g3 "includes extra information such as all the macro definitions present in the program. 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.

1 Comment

Because I didn't understand this at first: if you use -g3 then you'll be able to see the values of macros while debugging, as opposed to the debugger complaining about the variable not existing or some similar error.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.