466-468.html

来自「linux-unix130.linux.and.unix.ebooks130 l」· HTML 代码 · 共 91 行

HTML
91
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Programming in C</TITLE>

<SCRIPT>
<!--
function displayWindow(url, width, height) {
        var Win = window.open(url,"displayWindow",'width=' + width +
',height=' + height + ',resizable=1,scrollbars=yes');
}
//-->
</SCRIPT>
</HEAD>

 -->




<!--ISBN=0672313723//-->

<!--TITLE=Linux Unleashed, Third Edition//-->

<!--AUTHOR=Tim Parker//-->

<!--PUBLISHER=Macmillan Computer Publishing//-->

<!--IMPRINT=Sams//-->

<!--CHAPTER=26//-->

<!--PAGES=466-468//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="463-466.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="468-472.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading6"></A><FONT COLOR="#000077">Optimization Options</FONT></H4>

<P>When you compile C code with GCC, it tries to compile the code in the least amount of time and also tries to create compiled code that is easy to debug. Making the code easy to debug means that the sequence of the compiled code is the same as the sequence of the source code, and no code gets optimized out of the compile. There are many options that you can use to tell GCC to create smaller, faster executable programs at the cost of compile time and ease of debugging. Of these options, the two that you typically use are the <TT>-O</TT> and the <TT>-O2</TT> options.</P>

<P>The <TT>-O</TT> option tells GCC to perform basic optimizations on the source code. In most cases these optimizations make the code run faster. The <TT>-O2</TT> option tells GCC to make the code as fast and small as it can. The <TT>-O2</TT> option causes the compilation speed to be slower than when using the<TT>-O</TT> option, but typically results in code that executes more quickly.</P>

<P>In addition to the <TT>-O</TT> and <TT>-O2</TT> optimization options, there are a number of lower-level options that can be used to make the code faster. These options are very specific and should be used only if you fully understand the consequences that these options will have on the compiled code. For a detailed description of these options, refer to the GCC man page by typing <TT>man gcc</TT> on the command line.</P>

<H4 ALIGN="LEFT"><A NAME="Heading7"></A><FONT COLOR="#000077">Debugging and Profiling Options</FONT></H4>

<P>GCC supports several debugging and profiling options. Of these options, the two that you are most likely to use are the <TT>-g</TT> option and the <TT>-pg</TT> option.</P>

<P>The <TT>-g</TT> option tells GCC to produce debugging information that the GNU debugger (<TT>gdb</TT>) can use to help you to debug your program. GCC provides a feature that many other C compilers do not have. With GCC you can use the <TT>-g</TT> option in conjunction with the <TT>-O</TT> option (which generates optimized code). This can be very useful if you are trying to debug code that is as close as possible to what will exist in the final product. When you are using these two options together, you should be aware that some of the code that you have written will probably be changed by GCC when it optimizes it. For more information on debugging your C programs, see the &#147;Debugging GCC Applications with <TT>gdb</TT>&#148; section in this chapter.</P>

<P>The <TT>-pg</TT> option tells GCC to add extra code to your program that will, when executed, generate profile information that can be used by the <TT>gprof</TT> program to display timing information about your program. For more information on <TT>gprof</TT>, see the &#147;<TT>gprof</TT>&#148; section in this chapter.</P>

<H3><A NAME="Heading8"></A><FONT COLOR="#000077">Debugging GCC Programs with gdb</FONT></H3>

<P>Linux includes the GNU debugging program called <TT>gdb</TT>. <TT>gdb</TT> is a very powerful debugger that can be used to debug C and C&#43;&#43; programs. It enables you to see the internal structure or the memory that is being used by a program while it is executing. Some of the functions that <TT>gdb</TT> provides for you are these:</P>

<DL>

<DD><B>&#149;</B>&nbsp;&nbsp;It enables you to monitor the value of variables that are contained in your program.

<DD><B>&#149;</B>&nbsp;&nbsp;It enables you to set breakpoints that will stop the program at a specific line of code.

<DD><B>&#149;</B>&nbsp;&nbsp;It enables you to step through the code, line by line.

</DL>

<P>You can run <TT>gdb</TT> by typing <TT>gdb</TT> on the command line and pressing Enter. If your system is configured properly, <TT>gdb</TT> will start and you will see a screen that resembles the fol-lowing:</P>

<!-- CODE SNIP //-->

<PRE>

GDB is free software and you are welcome to distribute copies of it

under certain conditions; type &#147;show copying&#148; to see the conditions.

There is absolutely no warranty for GDB; type &#147;show warranty&#148; for details.

GDB 4.12 (i486-unknown-linux), Copyright 1994 Free Software Foundation,

Inc.

(gdb)

</PRE>

<!-- END CODE SNIP //-->

<P>When you start <TT>gdb</TT>, there are a number of options that you can specify on the command line. You will probably run <TT>gdb</TT> in the following way:</P>

<!-- CODE SNIP //-->

<PRE>

gdb &lt;fname&gt;

</PRE>

<!-- END CODE SNIP //-->

<P>When you invoke <TT>gdb</TT> in this way, you are specifying the executable file that you want to debug. This tells <TT>gdb</TT> to load the executable file with the name <TT>fname</TT>. There are also ways of starting <TT>gdb</TT> that tell it to inspect a core file that was created by the executable file being examined, or to attach <TT>gdb</TT> to a currently running process. To get a listing and brief description of each of these other options, you can refer to the <TT>gdb</TT> man page or type <TT>gdb -h</TT> at the command line.</P>

<H4 ALIGN="LEFT"><A NAME="Heading9"></A><FONT COLOR="#000077">Compiling Code for Debugging</FONT></H4>

<P>To get <TT>gdb</TT> to work properly, you must compile your programs so that debugging information is generated by the compiler. The debugging information that is generated contains the types for each of the variables in your program as well as the mapping between the addresses in the executable program and the line numbers in the source code. <TT>gdb</TT> uses this information to relate the executable code to the source code.</P>

<P>To compile a program with the debugging information turned on, use the <TT>-g</TT> compiler option.</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="463-466.html">Previous</A></TD>

<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>

<TD><A HREF="468-472.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





</td>
</tr>
</table>

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?