[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
4.1 Compiling for Debugging
In order to debug a program effectively, you need to generate debugging information when you compile it. This debugging information is stored in the object file; it describes the data type of each variable or function and the correspondence between source line numbers and addresses in the executable code.
To request debugging information, specify the ‘-g’ option when you run the compiler.
Programs that are to be shipped to your customers are compiled with optimizations, using the ‘-O’ compiler option. However, many compilers are unable to handle the ‘-g’ and ‘-O’ options together. Using those compilers, you cannot generate optimized executables containing debugging information.
No value for NGCC, the GNU C/C++ compiler, supports ‘-g’ with or without ‘-O’, making it possible to debug optimized code. We recommend that you always use ‘-g’ whenever you compile a program. You may think your program is correct, but there is no sense in pushing your luck.
When you debug a program compiled with ‘-g -O’, remember that the optimizer is rearranging your code; the debugger shows you what is really there. Do not be too surprised when the execution path does not exactly match your source file! An extreme example: if you define a variable, but never use it, No value for GDBN never sees that variable—because the compiler optimizes it out of existence.
Some things do not work as well with ‘-g -O’ as with just ‘-g’, particularly on machines with instruction scheduling. If in doubt, recompile with ‘-g’ alone, and if this fixes the problem, please report it to us as a bug (including a test case!). See section Program Variables, for more information about debugging optimized code.
Older versions of the GNU C compiler permitted a variant option ‘-gg’ for debugging information. No value for GDBN no longer supports this format; if your GNU C compiler has this option, do not use it.
No value for GDBN knows about preprocessor macros and can show you their expansion (see section C Preprocessor Macros). Most compilers do not include information about preprocessor macros in the debugging information if you specify the ‘-g’ flag alone, because this information is rather large. Version 3.1 and later of No value for NGCC, the GNU C compiler, provides macro information if you specify the options ‘-gdwarf-2’ and ‘-g3’; the former option requests debugging information in the Dwarf 2 format, and the latter requests “extra information”. In the future, we hope to find more compact ways to represent macro information, so that it can be included with ‘-g’ alone.
[ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |