【原创】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入门与应用
图数据库(Graph Database,简称GDB)是一种支持Property Graph图模型、用于处理高度连接数据查询与存储的实时、可靠的在线数据库服务。它支持Apache TinkerPop Gremlin查询语言,可以帮您快速构建基于高度连接的数据集的应用程序。GDB非常适合社交网络、欺诈检测、推荐引擎、实时图谱、网络/IT运营这类高度互连数据集的场景。 GDB由阿里云自主研发,具备如下优势: 标准图查询语言:支持属性图,高度兼容Gremlin图查询语言。 高度优化的自研引擎:高度优化的自研图计算层和存储层,云盘多副本保障数据超高可靠,支持ACID事务。 服务高可用:支持高可用实例,节点故障迅速转移,保障业务连续性。 易运维:提供备份恢复、自动升级、监控告警、故障切换等丰富的运维功能,大幅降低运维成本。 产品主页:https://www.aliyun.com/product/gdb
目录
相关文章
|
4月前
|
编译器 Linux 开发工具
|
15天前
|
前端开发 C语言
gcc动态库升级
gcc动态库升级
|
15天前
|
编译器 开发工具 C语言
Gcc 链接文件
Gcc 链接文件
18 4
|
27天前
|
编译器 C语言 C++
MinGW安装gcc
MinGW安装gcc
33 0
|
3月前
|
自然语言处理 编译器 Go
GCC:GNU编译器
GCC:GNU编译器
|
3月前
|
Java 编译器 Linux
技术经验解读:【转载】详解GCC的下载和安装(源码安装)
技术经验解读:【转载】详解GCC的下载和安装(源码安装)
60 0
|
3月前
|
C语言
关于如何解决mingw64安装后配置完环境变量仍然执行不了gcc命令
关于如何解决mingw64安装后配置完环境变量仍然执行不了gcc命令
|
4月前
|
C语言
gcc的简易用法
【5月更文挑战第10天】gcc的简易用法。
49 8
|
4月前
|
C语言
gcc的简易用法(编译、参数与链接)
【5月更文挑战第14天】gcc的简易用法(编译、参数与链接)。
37 1
|
4月前
|
Unix Java 编译器
安装gcc
【5月更文挑战第14天】安装gcc。
71 1