2009-03-21 7 views
62

Wenn ich gcc C-Programme ich in der Regel kompilieren -g verwenden, um einige Debug-Informationen in die Elf-Datei zu erhalten so dass gdb mir helfen, wenn nötig.Was ist der Unterschied zwischen gcc -ggdb und gcc -g

Allerdings habe ich festgestellt, dass einige Programme -ggdb verwenden, da es die Debug-Informationen mehr gdb freundlich machen soll.

Wie unterscheiden sie sich und welche wird empfohlen?


Hinweis: Ein Link zu den Optionen für Ihr Programm oder GCC Debugging, http://gcc.gnu.org/onlinedocs/gcc/Debugging-Options.html#Debugging-Options

Antwort

27

Es ist möglich, dass es keinen Unterschied gibt - hängt vom nativen OS-Format ab und davon, wie mobil die Debugging-Informationen sein sollen. Siehe GCC-Handbuch Debugging Options.

5

Eine Sache ist, dass „-g“ ist tragbar (zB in Makefiles bestimmt auf nicht-GNU ausgeführt werden Plattformen). Ich hatte kürzlich ein Portabilitätsproblem in Bezug auf -g vs. -ggdb auf einer AIX-Maschine, deshalb bringe ich es zur Sprache.

Keine Ahnung, was -ggdb in der Benutzerfreundlichkeit hinzugefügt, obwohl.

9

Ich habe atleast ein Beispiel, wo -ggdb für mich als eine andere Debug-Option besser gearbeitet, die wir verwendet haben:

[email protected]:~> 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; 
} 
[email protected]:~> gcc -gstabs+ main.c -o main 

[email protected]:~> 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 
[email protected]:~> /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) 

Hinweis: Dies geschieht nur auf x86-64-Boxen und geht weg, wenn mit -ggdb kompiliert . Aber neuere Versionen des Debuggers arbeiten sogar mit -gstabs +

35

-g und -ggdb fast ähnlich sind, mit einigen leichten Unterschiede, ich lese diese here:

-g produziert im OS¹s nativen Format Debug-Informationen (ersticht, COFF, XCOFF oder ZWERG 2).

-ggdb erzeugt Debugging-Informationen speziell für gdb.

-ggdb3 erzeugt zusätzliche Debuginformationen, z. B. Makrodefinitionen. -ggdb ohne Angabe der Ebene standardmäßig

-ggdb2 (d. h. gdb für Ebene 2).