Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I have recently started using Linux as a tool for programing. In my book, I have seen that the GCC is used with 2 options: -g and -o. Now, I know that -o is to set a filename, but what is the purpose of -g? I thought it might relate to debugging, but also programs compiled without the -g argument are debuggable. So what is it?

share|improve this question
add comment

3 Answers

up vote 4 down vote accepted

To quote from the manual: Produce debugging information in the operating system's native format (stabs, COFF, XCOFF, or DWARF 2). GDB can work with this debugging information. . I don't mean to be a RTFM guy but in this case, reading the manual section on -g will answer your question. As per -o, you're right.

share|improve this answer
    
Thank you so much, this answered my question. And i am sorry about not looking in the manual, I just forgot about it - Still a Linux noob :) –  arielschon12 Feb 4 '13 at 20:16
add comment

-g option enables use of extra debugging information that GDB can use. Here's example of C code:

int main() {
    int x = 1/0;    
}

let's compile it without -g option and see gdb output:

$ gcc test.c -o test
test.c: In function ‘main’:
test.c:2:11: warning: division by zero [-Wdiv-by-zero]
$ gdb -q ./test
Reading symbols from /home/mariusz/Dokumenty/Projekty/Testy/test...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/mariusz/Dokumenty/Projekty/Testy/test 

Program received signal SIGFPE, Arithmetic exception.
0x080483cb in main ()

And now with -g option:

$ gcc -g test.c -o test
test.c: In function ‘main’:
test.c:2:11: warning: division by zero [-Wdiv-by-zero]
$ gdb -q ./test
Reading symbols from /home/mariusz/Dokumenty/Projekty/Testy/test...done.
(gdb) run
Starting program: /home/mariusz/Dokumenty/Projekty/Testy/test 

Program received signal SIGFPE, Arithmetic exception.
0x080483cb in main () at test.c:2
2       int x = 1/0;    

As you can see, now there is information about line with error.

share|improve this answer
add comment

It is related to debugging:

(from man 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.

You can run a debugger on a program without debugging symbols in it, but it is like trying to find your keys in the dark. Eg, you cannot set breakpoints:

(gdb) break 6
No symbol table is loaded.

That's a big hassle.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.