### 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, some compilers are unable to handle the ‘`-g`’ and ‘`-O`’ options together. Using those compilers, you cannot generate optimized executables containing debugging information.

GCC, 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. For more information, see Debugging Optimized Code.

Older versions of the GNU C compiler permitted a variant option ‘`-gg`’ for debugging information. GDB no longer supports this format; if your GNU C compiler has this option, do not use it.

GDB knows about preprocessor macros and can show you their expansion (see C Preprocessor Macros). Most compilers do not include information about preprocessor macros in the debugging information if you specify the `-g` flag alone. Version 3.1 and later of GCC, the GNU C compiler, provides macro information if you are using the DWARF debugging format, and specify the option `-g3`.

See Options for Debugging Your Program or GCC in Using the GNU Compiler Collection (GCC), for more information on GCC options affecting debug information.

You will have the best debugging experience if you use the latest version of the DWARF debugging format that your compiler supports. DWARF is currently the most expressive and best supported debugging format in GDB.