当前位置: 首页 > news >正文

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

相关文章:

  • Domino的LDAP与WingFTP集成
  • 查找与排序
  • 技术or管理
  • PHP问题 —— 丢失SESSION
  • PHP Redis 安装
  • Node.js selenium-webdriver
  • Linux Process Manage
  • Linux下的三个时间属性
  • DRBD源码安装配置
  • Power Gating的设计(模块二)
  • php热身2:CRUD with Ajax
  • HIVE中的自定义函数
  • js创建数组的三个方式
  • 零基础小白也能做测试经理(高级测试工程师)?!
  • 【Django】Django Debug Toolbar调试工具配置
  • ----------
  • 【EOS】Cleos基础
  • canvas 五子棋游戏
  • Fastjson的基本使用方法大全
  • iOS 颜色设置看我就够了
  • Java方法详解
  • Just for fun——迅速写完快速排序
  • mysql 5.6 原生Online DDL解析
  • Rancher如何对接Ceph-RBD块存储
  • Sass 快速入门教程
  • Shadow DOM 内部构造及如何构建独立组件
  • Spring Cloud Feign的两种使用姿势
  • vagrant 添加本地 box 安装 laravel homestead
  • vue数据传递--我有特殊的实现技巧
  • 阿里云爬虫风险管理产品商业化,为云端流量保驾护航
  • 第13期 DApp 榜单 :来,吃我这波安利
  • 将 Measurements 和 Units 应用到物理学
  • 七牛云 DV OV EV SSL 证书上线,限时折扣低至 6.75 折!
  • 一、python与pycharm的安装
  •  一套莫尔斯电报听写、翻译系统
  • 一天一个设计模式之JS实现——适配器模式
  • 用jquery写贪吃蛇
  • 用Visual Studio开发以太坊智能合约
  • 正则与JS中的正则
  • scrapy中间件源码分析及常用中间件大全
  • !!java web学习笔记(一到五)
  • #NOIP 2014# day.1 T2 联合权值
  • $(document).ready(function(){}), $().ready(function(){})和$(function(){})三者区别
  • (3)选择元素——(17)练习(Exercises)
  • (TOJ2804)Even? Odd?
  • (分享)一个图片添加水印的小demo的页面,可自定义样式
  • (强烈推荐)移动端音视频从零到上手(上)
  • (四)汇编语言——简单程序
  • (算法)N皇后问题
  • (转)EOS中账户、钱包和密钥的关系
  • (转)JVM内存分配 -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=512m
  • *上位机的定义
  • .Net的DataSet直接与SQL2005交互
  • .net连接oracle数据库
  • .w文件怎么转成html文件,使用pandoc进行Word与Markdown文件转化