blob: 94b818fe96627d8718b58d0a7e4b9f3196eb0a1b [file] [log] [blame]
<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, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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>
<link rel="stylesheet" type="text/css" href="../cs.css">
</head>
<body>
<div class="node">
<a name="Compilation"></a>
<p>
Next:&nbsp;<a rel="next" accesskey="n" href="Starting.html#Starting">Starting</a>,
Up:&nbsp;<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 &lsquo;<samp><span class="samp">-g</span></samp>&rsquo; option when you run
the compiler.
<p>Programs that are to be shipped to your customers are compiled with
optimizations, using the &lsquo;<samp><span class="samp">-O</span></samp>&rsquo; compiler option. However, some
compilers are unable to handle the &lsquo;<samp><span class="samp">-g</span></samp>&rsquo; and &lsquo;<samp><span class="samp">-O</span></samp>&rsquo; 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 &lsquo;<samp><span class="samp">-g</span></samp>&rsquo; with or
without &lsquo;<samp><span class="samp">-O</span></samp>&rsquo;, making it possible to debug optimized code. We
recommend that you <em>always</em> use &lsquo;<samp><span class="samp">-g</span></samp>&rsquo; 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
&lsquo;<samp><span class="samp">-gg</span></samp>&rsquo;<!-- /@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, because this information is rather large.
Version 3.1 and later of <span class="sc">gcc</span>, the <span class="sc">gnu</span> C compiler,
provides macro information if you specify the options
<samp><span class="option">-gdwarf-2</span></samp> and <samp><span class="option">-g3</span></samp>; the former option requests
debugging information in the Dwarf 2 format, and the latter requests
&ldquo;extra information&rdquo;. In the future, we hope to find more compact
ways to represent macro information, so that it can be included with
<samp><span class="option">-g</span></samp> alone.
</body></html>