gcc -g option flag

6 篇文章 0 订阅

gcc -g option flag

  1. 官方解释
    Options for Debugging Your Program or GCC
    GCC has various special options that are used for debugging either your program or 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.
    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.

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

    -gsplit-dwarf
    Separate as much dwarf debugging information as possible into a separate output file with the extension .dwo. This option allows the build system to avoid linking files with debug information. To be useful, this option requires a debugger capable of reading .dwo files.

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

    我们在gcc编译时加上”-g”参数,编译器就会在产生的目标文件里加上调试信息,通过”readelf -S”等工具可以看到,目标文件(object)里多了很多”debug”相关的段。

  2. level description

    optiondescription
    -g0no debug information
    -g1minimal debug information
    -g2default debug information
    -g3maximal debug information

    • -g0
      不生成调试信息
    • -g
      选项可以利用操作系统的“原生格式(native format)”生成调试
      信息。
    • -g2
      这是默认的级别,此时产生的调试信息包括扩展的符号表、行号、局部
      或外部变量信息。
    • -g3
      包含级别2中的所有调试信息,以及源代码中定义的宏
    • -g1
      不包含局部变量和与行号有关的调试信息,因此只能够用于回溯
      跟踪和堆栈转储之用。回溯跟踪指的是监视程序在运行过程中的函数调用历
      史,堆栈转储则是一种以原始的十六进制格式保存程序执行环境的方法,两者
      都是经常用到的调试手段。
  3. tips
    调试信息在目标文件和可执行文件中占用很大空间,所以产品发布时,“须要”将这些调试信息去除掉,以减少目标文件的大小。可以使用 strip 命令去除ELF文件中的调试信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值