What is the difference between gcc -ggdb and gcc -g

前端 未结 5 1413
天命终不由人
天命终不由人 2020-12-04 09:15

When I use gcc to compile C programs I usually use -g to get some debug information into the elf file so that gdb can help me if needed.

However, I not

相关标签:
5条回答
  • 2020-12-04 10:01

    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.

    0 讨论(0)
  • 2020-12-04 10:06

    -g and -ggdb are similar with some slight differences, I read this here:

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

    0 讨论(0)
  • 2020-12-04 10:06

    I have atleast one example where -ggdb worked better for me than another debug option which we were using :

    amitkar@lohgad:~> cat > main.c
    #include <stdio.h>
    
    int main(int argc, char **argv)
    {
            printf("Args :%d\n", argc);
            for ( ;argc > 0;)
                    printf("%s\n", argv[--argc]);
    
            return 0;
    }
    amitkar@lohgad:~> gcc -gstabs+ main.c -o main
    
    amitkar@lohgad:~> file main
    main: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), for GNU/Linux 2.6.4, dynamically linked (uses shared libs), not stripped
    amitkar@lohgad:~> /usr/bin/gdb ./main
    GNU gdb 6.6.50.20070726-cvs
    Copyright (C) 2007 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "x86_64-suse-linux"...
    Using host libthread_db library "/lib64/libthread_db.so.1".
    (gdb) break main
    Breakpoint 1 at 0x400577: file main.c, line 5.
    (gdb) run
    Starting program: /home/amitkar/main
    
    Breakpoint 1, main (argc=Cannot access memory at address 0x8000df37d57c
    ) at main.c:5
    5               printf("Args :%d\n", argc);
    (gdb) print argc
    Cannot access memory at address 0x8000df37d57c
    (gdb)
    

    Note: This happens only on x86-64 boxes and goes away when compiled with -ggdb. But newer versions of the debugger do work even with -gstabs+

    0 讨论(0)
  • 2020-12-04 10:09

    This is the official explanation: http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging-Options

    Only few hard facts, but interesting anyway.

    0 讨论(0)
  • 2020-12-04 10:10

    It is possible that there is no difference - depends on the OS native format and how portable you want the debugging info to be. See GCC manual Debugging Options.

    0 讨论(0)
提交回复
热议问题