5 Compiling for debugging
Normally, an executable file does not contain any references to the
original program source code, such as variable names or
line-numbers--the executable file is simply the sequence of machine
code instructions produced by the compiler. This is insufficient for
debugging, since there is no easy way to find the cause of an error if
the program crashes.
GCC provides the
-g debug option to store additional
debugging information in object files and executables. This debugging
information allows errors to be traced back from a specific machine
instruction to the corresponding line in the original source file. The
execution of a program compiled with
-g can also be followed in a
debugger, such as the GNU Debugger
gdb (for more information, see
"Debugging with GDB: The GNU Source-Level Debugger",
section Further reading). Using a debugger allows the values of
variables to be examined while the program is running.
The debug compilation option works by storing the names and source
code line-numbers of functions and variables in a symbol table
in the object file or executable.