【原创】GCC选项-g和-ggdb的区别



很多人不清楚 gcc 编译选项 -g 和 -ggdb 的区别是什么,这里根据 stackoverflow 上的一个帖子,以及相关手册上的内容进行解释说明。

Debugging-Options of GCC 手册说明

-g  Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information.
-g 选项可以产生符合操作系统本地格式的调试信息(stabs、COFF、XCOFF ,或者 DWARF 2)。GDB 可以基于这里调试信息进行工作。
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).
在大多数使用 stabs 格式的系统中,-g 选项会使能额外的、只有 GDB 能用的调试信息;这种额外信息能够令 GDB 更好的进行调试,但可能使得其他调试器发生崩溃,或者拒绝对可执行程序进行解析。如果你想要针对是否额外信息进行控制,可以使用 -gstabs+, -gstabs, -gxcoff+, -gxcoff, 或 -gvms 等选项。

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.
GCC 允许你同时使用 -g 和 -O 选项。代码在优化后可能会产生令人惊奇的结果:一些你声明的变量可能已经不存在了;控制流可能走到了你未曾想象到的位置;一些语句可能不会被执行,因为其计算结果是常量,或者其结果早已经被获得;一些语句可能在不同的地方被执行,因为其被移出了当前循环。

Nevertheless it proves possible to debug optimized output. This makes it reasonable to use the optimizer for programs that might have bugs.
无论怎样,针对优化后的输出进行调试还是可能的。这也就是带 bug 的优化器仍旧被使用的背后原因。

The following options are useful when GCC is generated with the capability for more than one debugging format. 


-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. 
-ggdb 产生 GDB 所需的调试信息。这意味着将会使用可用的、最具表达力的格式(DWARF 2 、stabs ,或者在前两者不支持情况下的其他本地格式),如果可能的话还会包含 GDB 扩展信息。


-gstabs+
Produce debugging information in stabs format (if that is supported), using GNU extensions understood only by the GNU debugger (GDB). The use of these extensions is likely to make other debuggers crash or refuse to read the program. 
产生 stabs 格式的调试信息(如果支持的话),并且会使用只有 GNU 调试器(GDB)才理解的  GNU 扩展。这些扩展的使用可能会导致其他调试器崩溃或者拒绝读取编译出来的可执行程序。

-gstabs
Produce debugging information in stabs format (if that is supported), without GDB extensions. This is the format used by DBX on most BSD systems. On MIPS, Alpha and System V Release 4 systems this option produces stabs debugging output that is not understood by DBX or SDB. On System V Release 4 systems this option requires the GNU assembler. 
产生 stabs 格式的调试信息(如果支持的话),其中不包含 GDB 扩展。用于 DBX 调试器。


-gxcoff
Produce debugging information in XCOFF format (if that is supported). This is the format used by the DBX debugger on IBM RS/6000 systems. 
产生 XCOFF 格式的调试信息(如果支持的话) ,用于 DBX 调试器。

-gxcoff+
Produce debugging information in XCOFF format (if that is supported), using GNU extensions understood only by the GNU debugger (GDB). The use of these extensions is likely to make other debuggers crash or refuse to read the program, and may cause assemblers other than the GNU assembler (GAS) to fail with an error. 
产生 XCOFF 格式的调试信息(如果支持的话) ,并且会使用只有 GNU 调试器(GDB)才理解的  GNU 扩展。 这些扩展的使用可能会导致其他调试器崩溃或者拒绝读取编译出来的可执行程序,并且可能造成除 GNU 汇编器(GAS) 以外的其他汇编器的执行失败。

-gvms
Produce debugging information in Alpha/VMS debug format (if that is supported). This is the format used by DEBUG on Alpha/VMS systems. 
产生  Alpha/VMS 格式的调试信息(如果支持的话)



Re: difference between -g, -ggdb and -ggdb3 邮件列表说明

I was wondering what the difference between -g, -ggdb and -ggdb3 as option for gcc?

-g produces debugging information in the OS¹s native format (stabs, COFF, XCOFF, or DWARF 2).
-ggdb produces debugging information specifically intended for gdb.
-ggdb3 produces extra debugging information, for example: including macro definitions.
-ggdb by itself without specifying the level defaults to -ggdb2 (i.e., gdb for level 2).

It is *possible* (I've heard) that the -ggdb can produce debugging information that can  confuse other debuggers if you are not using gdb. 
only use gdb, so I cannot confirm or refute that rumor.

So...
If you use gdb, use -ggdb (same as -ggdb2), or -ggdb1 (to help reduce the
debug footprint), or -ggdb3 (for additional debugging info).

If you do not use gdb...
Use -g and keep in mind that gdb works with the OS's native format too.

I use -g even though I use gdb exclusively when I use gcc/g++.
I could use -ggdb ... but I forget to type those extra three characters.


One thing is that "-g" is portable (e.g. in Makefiles destined to be executed on non-GNU platforms). I had a portability issue regarding -g vs. -ggdb on an AIX machine recently, that's why I bring it up.
No idea on what -ggdb adds in usability, though.




你可能感兴趣的:(gdb,-g,gnu,-ggdb)