923-926.html

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

HTML
97
字号
<HTML>

<HEAD>

<TITLE>Linux Unleashed, Third Edition:Working with the Kernel</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=57//-->

<!--PAGES=923-926//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="920-923.html">Previous</A></TD>

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

<TD><A HREF="../ch58/927-929.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<P>When you compile a program using <TT>gcc</TT> without any command line options, it creates an executable file (assuming that the compile was successful) and calls it <TT>a.out</TT>. To specify a name other than <TT>a.out</TT> for the executable file, use the <TT>-o</TT> compiler option. For example, to compile a C program file named <TT>count.c</TT> into an executable file named <TT>count,</TT> use the following command:</P>

<BLOCKQUOTE>

<P><FONT SIZE="-1"><HR><B>Note:&nbsp;&nbsp;</B><BR>When you are using the -o option, the executable file name must occur directly after the -o on the command line. Don&#146;t put other options between the output name and the -o signal.<HR></FONT>

</BLOCKQUOTE>

<!-- CODE SNIP //-->

<PRE>

gcc -o count count.c

</PRE>

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

<P>There are also compiler options that allow you to specify how far you want the compile to proceed. The <TT>-c</TT> option tells <TT>gcc</TT> to compile the code into object code and to skip the assembly and linking stages of the compile. This option is used quite often because it makes the compilation of multi-file C programs faster and easier to manage. Object code files that are created by <TT>gcc</TT>have a <TT>.o</TT> extension by default.</P>

<P>The <TT>-S</TT> compiler option tells <TT>gcc</TT> to stop the compile after it has generated the assembler files for the C code. Assembler files that are generated by <TT>gcc</TT> have an <TT>.s</TT> extension by default. The <TT>-E</TT> option instructs the compiler to only perform the preprocessing compiler stage on the input files. When this option is used, the output from the preprocessor is sent to the standard output rather than being stored in a file.</P>

<P>When you compile C code with <TT>gcc</TT>, 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 <TT>gcc</TT> to create smaller, faster executable programs at the cost of compile time and ease of debugging. Of these options the two that you will typically use are the <TT>-O</TT> and the <TT>-O2</TT> options.</P>

<P>The <TT>-O</TT> option tells <TT>gcc</TT> to perform basic optimizations on the source code. In most cases, these optimizations make the code run faster. The <TT>-O2</TT> option tells <TT>gcc</TT> to make the code as fast and small as it can. The <TT>-O2</TT> option causes the compilation speed to be slower than it is when using the -O 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 only be used if you fully understand the consequences that using these options will have on the compiled code. For a detailed description of these options, refer to the <TT>gcc</TT> man page.</P>

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

<P>The <TT>gcc</TT> compiler 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. The <TT>gcc</TT> program provides a feature that many other C compilers do not. With <TT>gcc</TT> 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 be aware that some of the code that you have written will probably be changed by <TT>gcc</TT> when it optimizes it.</P>

<P>The <TT>-pg</TT> option tells <TT>gcc</TT> 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.</P>

<H4 ALIGN="LEFT"><A NAME="Heading8"></A><FONT COLOR="#000077">Debugging gcc Programs with gdb</FONT></H4>

<P>Linux includes the GNU debugging program called <TT>gdb</TT>. The <TT>gdb</TT> debugger is a very powerful debugger that can be used to debug C and C&#43;&#43; programs. It allows 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 are</P>

<DL>

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

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

<DD><B>&#149;</B>&nbsp;&nbsp;allows you to step through the code line by line

</DL>

<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> most often with this command:</P>

<!-- CODE SNIP //-->

<PRE>

gdb filename

</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. There are also ways of starting <TT>gdb</TT> that tell it to inspect a core file 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>

<P>To get <TT>gdb</TT> to work properly you must compile your programs so that debugging information will be 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. The <TT>gdb</TT> debugger uses this information to relate the executable code to the source code. To compile a program with the debugging information turned on use the <TT>-g</TT> compiler option.</P>

<H3><A NAME="Heading9"></A><FONT COLOR="#000077">Summary</FONT></H3>

<P>Recompiling kernel source code and adding new features to the kernel proceeds smoothly as long as you know what you are doing. Don&#146;t let the process scare you, but always keep boot disks on hand. Follow instructions wherever available as most new software has special requirements for linking into the kernel or replacing existing systems. From here, there are some other subjects you may want to check out. For example, to learn about

</P>

<DL>

<DD>Using the C compiler that comes with most Linux distributions, see Chapter 26, &#147;Programming in C.&#148;

<DD>Ensuring that your system with the kernel is configured properly, see Part VI, starting with Chapter 32, &#147;System Administration Basics.&#148;

<DD>Using the source code control system to avoid too many files of slightly different contents littering your hard drive, see Chapter 56, &#147;Source Code Control.&#148;

</DL>

<P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="920-923.html">Previous</A></TD>

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

<TD><A HREF="../ch58/927-929.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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