gcc -g option flag

来源:互联网 发布:数据库关系 0 1 编辑:程序博客网 时间:2024/06/05 08:41

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

    option description -g0 no debug information -g1 minimal debug information -g2 default debug information -g3 maximal debug information

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

原创粉丝点击