gcc -ggdb和gcc -g的区别是什么?

时间:2021-06-06 02:12:26

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.

当我使用gcc来编译C程序时,我通常使用-g来获取一些调试信息到elf文件中,这样gdb就可以在需要的时候帮助我。

However, I noticed that some programs use -ggdb, since it's supposed to make the debug info more gdb friendly.

但是,我注意到一些程序使用-ggdb,因为它应该使调试信息更加友好。

How do they differ and which is recommended to use?

它们有什么不同之处,推荐使用哪一种?


Note: A link to the options for Debugging Your Program or GCC, http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging-Options

注意:链接到调试您的程序或GCC的选项,http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#调试选项。

5 个解决方案

#1


28  

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.

可能没有区别——取决于操作系统的本机格式,以及您希望调试信息的可移植性。参见GCC手动调试选项。

#2


36  

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

-g和-ggdb差不多有一些细微的差别,我在这里读到:

-g produces debugging information in the OS¹s native format (stabs, COFF, XCOFF, or DWARF 2).

-g在操作系统的本机格式(stabs, COFF, XCOFF,或DWARF 2)中生成调试信息。

-ggdb produces debugging information specifically intended for gdb.

-ggdb生成专门用于gdb的调试信息。

-ggdb3 produces extra debugging information, for example: including macro definitions. -ggdb by itself without specifying the level defaults to

-ggdb3生成额外的调试信息,例如:包括宏定义。-ggdb本身没有指定级别默认值。

-ggdb2 (i.e., gdb for level 2).

-ggdb2(即。,gdb为二级。

#3


9  

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

我至少有一个示例,其中-ggdb比我们使用的另一个调试选项更适合我:

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+

注意:这只发生在x86-64个框中,并在与-ggdb编译时消失。但是新版本的调试器甚至可以使用-gstabs+。

#4


5  

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.

一件事是“-g”是可移植的(例如,在非gnu平台上执行的makefile中)。最近我在AIX机器上有一个关于-g vs -ggdb的可移植性问题,这就是我提出这个问题的原因。

No idea on what -ggdb adds in usability, though.

不过,不知道-ggdb增加了可用性。

#5


3  

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

这是官方的解释:http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#调试选项。

Only few hard facts, but interesting anyway.

只有少数确凿的事实,但无论如何还是有趣的。

#1


28  

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.

可能没有区别——取决于操作系统的本机格式,以及您希望调试信息的可移植性。参见GCC手动调试选项。

#2


36  

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

-g和-ggdb差不多有一些细微的差别,我在这里读到:

-g produces debugging information in the OS¹s native format (stabs, COFF, XCOFF, or DWARF 2).

-g在操作系统的本机格式(stabs, COFF, XCOFF,或DWARF 2)中生成调试信息。

-ggdb produces debugging information specifically intended for gdb.

-ggdb生成专门用于gdb的调试信息。

-ggdb3 produces extra debugging information, for example: including macro definitions. -ggdb by itself without specifying the level defaults to

-ggdb3生成额外的调试信息,例如:包括宏定义。-ggdb本身没有指定级别默认值。

-ggdb2 (i.e., gdb for level 2).

-ggdb2(即。,gdb为二级。

#3


9  

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

我至少有一个示例,其中-ggdb比我们使用的另一个调试选项更适合我:

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+

注意:这只发生在x86-64个框中,并在与-ggdb编译时消失。但是新版本的调试器甚至可以使用-gstabs+。

#4


5  

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.

一件事是“-g”是可移植的(例如,在非gnu平台上执行的makefile中)。最近我在AIX机器上有一个关于-g vs -ggdb的可移植性问题,这就是我提出这个问题的原因。

No idea on what -ggdb adds in usability, though.

不过,不知道-ggdb增加了可用性。

#5


3  

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

这是官方的解释:http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#调试选项。

Only few hard facts, but interesting anyway.

只有少数确凿的事实,但无论如何还是有趣的。