Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

gcc -g0 and without the -g option

Tags:

c

linux

gcc

What is the difference between compiling without the -g option and compiling with the -g0 option in gcc? Is there any specific reason behind giving the -g0 option in gcc? I tried compiling and found the "no debugging symbols" message with both cases in GDB.

like image 753
Pratik Panda Avatar asked Dec 25 '22 06:12

Pratik Panda


2 Answers

If you go to the GCC manual, you will find that it says:

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

So, don't use -g0 if you want debug information. If you do want debug information, remember to use the -g option both when creating the object files (-c) and when linking the program.

And compiling without -g at all and compiling with -g0 are equivalent and there is no debugging information in the resulting binaries — as you found by experimentation.

like image 177
Jonathan Leffler Avatar answered Dec 26 '22 20:12

Jonathan Leffler


From the manual:

-glevel

-ggdblevel

-gstabslevel

-gcofflevel

-gxcofflevel

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

-gdwarf-2 does not accept a concatenated debug level, because GCC used to support an option -gdwarf that meant to generate debug information in version 1 of the DWARF format (which is very different from version 2), and it would have been too confusing. That debug format is long obsolete, but the option cannot be changed now. Instead use an additional -glevel option to change the debug level for DWARF.

And for completeness, about the default level -g/-g2

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

GCC allows you to use -g with -O. The shortcuts taken by optimized code may occasionally produce surprising results: some variables you declared may not exist at all; flow of control may briefly move where you did not expect it; some statements may not be executed because they compute constant results or their values are already at hand; some statements may execute in different places because they have been moved out of loops.

Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.

like image 32
xvan Avatar answered Dec 26 '22 18:12

xvan