| <html lang="en"> |
| <head> |
| <title>Compilation - Debugging with GDB</title> |
| <meta http-equiv="Content-Type" content="text/html"> |
| <meta name="description" content="Debugging with GDB"> |
| <meta name="generator" content="makeinfo 4.13"> |
| <link title="Top" rel="start" href="index.html#Top"> |
| <link rel="up" href="Running.html#Running" title="Running"> |
| <link rel="next" href="Starting.html#Starting" title="Starting"> |
| <link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> |
| <!-- |
| Copyright (C) 1988-2019 Free Software Foundation, Inc. |
| |
| Permission is granted to copy, distribute and/or modify this document |
| under the terms of the GNU Free Documentation License, Version 1.3 or |
| any later version published by the Free Software Foundation; with the |
| Invariant Sections being ``Free Software'' and ``Free Software Needs |
| Free Documentation'', with the Front-Cover Texts being ``A GNU Manual,'' |
| and with the Back-Cover Texts as in (a) below. |
| |
| (a) The FSF's Back-Cover Text is: ``You are free to copy and modify |
| this GNU Manual. Buying copies from GNU Press supports the FSF in |
| developing GNU and promoting software freedom.'' |
| --> |
| <meta http-equiv="Content-Style-Type" content="text/css"> |
| <style type="text/css"><!-- |
| pre.display { font-family:inherit } |
| pre.format { font-family:inherit } |
| pre.smalldisplay { font-family:inherit; font-size:smaller } |
| pre.smallformat { font-family:inherit; font-size:smaller } |
| pre.smallexample { font-size:smaller } |
| pre.smalllisp { font-size:smaller } |
| span.sc { font-variant:small-caps } |
| span.roman { font-family:serif; font-weight:normal; } |
| span.sansserif { font-family:sans-serif; font-weight:normal; } |
| --></style> |
| </head> |
| <body> |
| <div class="node"> |
| <a name="Compilation"></a> |
| <p> |
| Next: <a rel="next" accesskey="n" href="Starting.html#Starting">Starting</a>, |
| Up: <a rel="up" accesskey="u" href="Running.html#Running">Running</a> |
| <hr> |
| </div> |
| |
| <h3 class="section">4.1 Compiling for Debugging</h3> |
| |
| <p>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. |
| |
| <p>To request debugging information, specify the ‘<samp><span class="samp">-g</span></samp>’ option when you run |
| the compiler. |
| |
| <p>Programs that are to be shipped to your customers are compiled with |
| optimizations, using the ‘<samp><span class="samp">-O</span></samp>’ compiler option. However, some |
| compilers are unable to handle the ‘<samp><span class="samp">-g</span></samp>’ and ‘<samp><span class="samp">-O</span></samp>’ options |
| together. Using those compilers, you cannot generate optimized |
| executables containing debugging information. |
| |
| <p><span class="sc">gcc</span>, the <span class="sc">gnu</span> C/C<tt>++</tt> compiler, supports ‘<samp><span class="samp">-g</span></samp>’ with or |
| without ‘<samp><span class="samp">-O</span></samp>’, making it possible to debug optimized code. We |
| recommend that you <em>always</em> use ‘<samp><span class="samp">-g</span></samp>’ 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 <a href="Optimized-Code.html#Optimized-Code">Optimized Code</a>. |
| |
| <p>Older versions of the <span class="sc">gnu</span> C compiler permitted a variant option |
| ‘<samp><span class="samp">-gg</span></samp>’<!-- /@w --> for debugging information. <span class="sc">gdb</span> no longer supports this |
| format; if your <span class="sc">gnu</span> C compiler has this option, do not use it. |
| |
| <p><span class="sc">gdb</span> knows about preprocessor macros and can show you their |
| expansion (see <a href="Macros.html#Macros">Macros</a>). Most compilers do not include information |
| about preprocessor macros in the debugging information if you specify |
| the <samp><span class="option">-g</span></samp> flag alone. Version 3.1 and later of <span class="sc">gcc</span>, |
| the <span class="sc">gnu</span> C compiler, provides macro information if you are using |
| the DWARF debugging format, and specify the option <samp><span class="option">-g3</span></samp>. |
| |
| <p>See <a href="../gcc/Debugging-Options.html#Debugging-Options">Options for Debugging Your Program or GCC</a>, for more |
| information on <span class="sc">gcc</span> options affecting debug information. |
| |
| <p>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 <span class="sc">gdb</span>. |
| |
| </body></html> |
| |