【原创】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 OS1s 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.

时间: 2024-08-29 01:12:28

【原创】GCC选项-g和-ggdb的区别的相关文章

gcc选项-g与-rdynamic的异同

摘自http://www.tuicool.com/articles/EvIzUn gcc选项-g与-rdynamic的异同 gcc 的 -g ,应该没有人不知道它是一个调试选项,因此在一般需要进行程序调试的场景下,我们都会加上该选项,并且根据调试工具的不同,还能直接选择更有针对性的说明,比如 -ggdb .-g是一个编译选项,即在源代码编译的过程中起作用,让gcc把更多调试信息(也就包括符号信息)收集起来并将存放到最终的可执行文件内. 相比-g选项, -rdynamic 却是一个 连接选项 ,它

转载 cc、gcc、g++、CC的区别概括

gcc是C编译器:g++是C++编译器:linux下cc一般是一个符号连接,指向gcc:gcc和g++都是GUN(组织)的编译器.而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Linux/Unix都是大小写敏感的系统,这点一定要注意. cc是Unix系统的C Compiler,而gcc则是GNU Compiler Collection,GNU编译器套装.gcc原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C.C++.Objective

cc、gcc、g++、CC的区别概括 /bin/bash cc 命令找不到

gcc是C编译器:g++是C++编译器:linux下cc一般是一个符号连接,指向gcc:gcc和g++都是GUN(组织)的编译器.而CC则一般是makefile里面的一个名字,即宏定义,嘿,因为Linux/Unix都是大小写敏感的系统,这点一定要注意. cc是Unix系统的C Compiler,而gcc则是GNU Compiler Collection,GNU编译器套装.gcc原名为Gun C语言编译器,因为它原本只能处理C语言,但gcc很快地扩展,包含很多编译器(C.C++.Objective

GCC与G++那些事儿

StackOverflow上一个很有名的问题,gcc和g++到底有什么区别? 答案如下: GCC: GNU Compiler Collection 指的是GNU编译器所支持的所有不同的类型的语言 gcc: GNU C  Compiler g++:GNU C++ Compiler 主要的区别在于: 1.gcc会将.c文件和.cpp文件分别当做C和C++来编译. 2.g++会将.c文件和.cpp文件都当做c++文件来编译. 3.如果使用g++链接目标文件,它会自动链接std C++ lib.然而gc

转 gcc与g++区别

gcc和g++都是GNU(一个组织)的编译器. 1.对于.c后缀的文件,gcc把它当做是C程序:g++当做是C++程序: 2.对于.cpp后缀的文件,gcc和g++都会当做c++程序. 3.编译阶段,g++会调用gcc(也就是说); 4.连接阶段,通常会用g++来完成,这是因为gcc命令不能自动和c++程序使用的库连接. gcc可以用来编译c++但是它不会自动调用链接的c++库,你需要自己手动链接,使用如下命令: gcc -lstdc++main.cpp.g++则会自动调用链接的c++库. gc

gcc和g++的区别和联系

gcc和g++都是GNU(一个组织)的编译器. 1.对于.c后缀的文件,gcc把它当做是C程序:g++当做是C++程序: 2.对于.cpp后缀的文件,gcc和g++都会当做c++程序. 3.编译阶段,g++会调用gcc; 4.连接阶段,通常会用g++来完成,这是因为gcc命令不能自动和c++程序使用的库连接. gcc/g++在执行编译工作的时候,总共需要4步1.预处理,生成.i的文件[预处理器cpp] 2.将预处理后的文件转换成汇编语言,生成文件.s[编译器egcs] 3.有汇编变为目标代码(机

gcc 与 g++的区别

原文: http://www.cnblogs.com/wb118115/p/5969775.html ------------------------------------------------------------------------------------ 看的Linux公社的一篇文章,觉得不错,内容复制过来了. 其实在这之前,我一直以为gcc和g++是一个东西,只是有两个不同的名字而已,今天在linux下编译一个c代码时出现了错误才找了一下gcc和g++的区别. 先描述一下今天遇

gcc和g++的区别【转自中国源码网】

gcc和g++都是GNU(组织)的一个编译器. 误区一:gcc只能编译c代码,g++只能编译c++代码两者都可以,但是请注意:1.后缀为.c的,gcc把它当作是C程序,而g++当作是c++程序:后缀为.cpp的,两者都会认为是c++程序,注意,虽然c++是c的超集,但是两者对语法的要求是有区别的.C++的语法规则更加严谨一些.2.编译阶段,g++会调用gcc,对于c++代码,两者是等价的,但是因为gcc命令不能自动和C++程序使用的库联接,所以通常用g++来完成链接,为了统一起见,干脆编译/链接

GCC的gcc和g++区别

看的Linux公社的一篇文章,觉得不错,内容复制过来了. 其实在这之前,我一直以为gcc和g++是一个东西,只是有两个不同的名字而已,今天在linux下编译一个c代码时出现了错误才找了一下gcc和g++的区别. 先描述一下今天遇到的错误: 因为以前写程序的时候,写的都是cpp文件,然后就直接使用g++编译,没出错,也就没有在意过这些问题,今天有一个c文件,但里面包含后STL中的vector语法,想着使用gcc来编译,结果报错了,报错是:找不到vector. 什么是gcc / g++ 首先说明:g