3 <title>Debugging with GDB
</title>
4 <meta http-equiv=
"Content-Type" content=
"text/html">
5 <meta name=
"description" content=
"Debugging with GDB">
6 <meta name=
"generator" content=
"makeinfo 4.3">
7 <link href=
"http://www.gnu.org/software/texinfo/" rel=
"generator-home">
12 Node:
<a name=
"Compilation">Compilation
</a>,
13 Next:
<a rel=
"next" accesskey=
"n" href=
"Starting.html#Starting">Starting
</a>,
14 Up:
<a rel=
"up" accesskey=
"u" href=
"Running.html#Running">Running
</a>
18 <h3 class=
"section">Compiling for debugging
</h3>
20 <p>In order to debug a program effectively, you need to generate
21 debugging information when you compile it. This debugging information
22 is stored in the object file; it describes the data type of each
23 variable or function and the correspondence between source line numbers
24 and addresses in the executable code.
26 <p>To request debugging information, specify the
<code>-g
</code> option when you run
29 <p>Many C compilers are unable to handle the
<code>-g
</code> and
<code>-O
</code>
30 options together. Using those compilers, you cannot generate optimized
31 executables containing debugging information.
33 GCC, the
<small>GNU
</small> C compiler, supports
<code>-g
</code> with or
34 without
<code>-O
</code>, making it possible to debug optimized code. We
35 recommend that you
<em>always
</em> use
<code>-g
</code> whenever you compile a
36 program. You may think your program is correct, but there is no sense
39 <p>When you debug a program compiled with
<code>-g -O
</code>, remember that the
40 optimizer is rearranging your code; the debugger shows you what is
41 really there. Do not be too surprised when the execution path does not
42 exactly match your source file! An extreme example: if you define a
43 variable, but never use it, GDB never sees that
44 variable--because the compiler optimizes it out of existence.
46 <p>Some things do not work as well with
<code>-g -O
</code> as with just
47 <code>-g
</code>, particularly on machines with instruction scheduling. If in
48 doubt, recompile with
<code>-g
</code> alone, and if this fixes the problem,
49 please report it to us as a bug (including a test case!).
51 <p>Older versions of the
<small>GNU
</small> C compiler permitted a variant option
52 <code>-gg
</code> for debugging information. GDB no longer supports this
53 format; if your
<small>GNU
</small> C compiler has this option, do not use it.