rhl27.htm
来自「linux的初学电子书」· HTM 代码 · 共 2,014 行 · 第 1/4 页
HTM
2,014 行
<HTML>
<HEAD>
<TITLE>Red Hat Linux Unleashed rhl27.htm </TITLE>
<LINK REL="ToC" HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm">
<LINK REL="Index" HREF="htindex.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/htindex.htm">
<LINK REL="Next" HREF="rhl28.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl28.htm">
<LINK REL="Previous" HREF="rhl26.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl26.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#800080">
<A NAME="I0"></A>
<H2>Red Hat Linux Unleashed rhl27.htm</H2>
<P ALIGN=LEFT>
<A HREF="rhl26.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl26.htm" TARGET="_self"><IMG SRC="purprev.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purprev.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Previous Page"></A>
<A HREF="index-1.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/index.htm" TARGET="_self"><IMG SRC="purtoc.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purtoc.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="TOC"></A>
<A HREF="rhl28.htm" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/rhl28.htm" TARGET="_self"><IMG SRC="purnext.gif" tppabs="http://202.113.16.101/%7eeb%7e/Red%20Hat%20Linux%20Unleashed/purnext.gif" WIDTH = 32 HEIGHT = 32 BORDER = 0 ALT="Next Page"></A>
<HR ALIGN=CENTER>
<P>
<UL>
<UL>
<UL>
<LI>
<A HREF="#E68E221" >What Is C?</A>
<LI>
<A HREF="#E68E222" >The GNU C Compiler</A>
<UL>
<LI>
<A HREF="#E69E332" >Invoking GCC</A>
<LI>
<A HREF="#E69E333" >GCC Options</A>
<LI>
<A HREF="#E69E334" >Optimization Options</A>
<LI>
<A HREF="#E69E335" >Debugging and Profiling Options</A></UL>
<LI>
<A HREF="#E68E223" >Debugging GCC Programs with gdb</A>
<UL>
<LI>
<A HREF="#E69E336" >Compiling Code for Debugging</A>
<LI>
<A HREF="#E69E337" >gdb Basic Commands</A>
<LI>
<A HREF="#E69E338" >Sample gdb Session</A></UL>
<LI>
<A HREF="#E68E224" >Additional C Programming Tools</A>
<UL>
<LI>
<A HREF="#E69E339" >xxgdb</A>
<LI>
<A HREF="#E69E340" >calls</A>
<LI>
<A HREF="#E69E341" >cproto</A>
<LI>
<A HREF="#E69E342" >indent</A>
<LI>
<A HREF="#E69E343" >gprof</A>
<LI>
<A HREF="#E69E344" >f2c and p2c</A></UL>
<LI>
<A HREF="#E68E225" >Summary</A></UL></UL></UL>
<HR ALIGN=CENTER>
<A NAME="E66E27"></A>
<H1 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>27</B></FONT></CENTER></H1>
<BR>
<A NAME="E67E27"></A>
<H2 ALIGN=CENTER>
<CENTER>
<FONT SIZE=6 COLOR="#FF0000"><B>Programming in C</B></FONT></CENTER></H2>
<BR>
<P>Linux is distributed with a wide range of software-development tools. Many of these tools support the development of C and C++ applications. This chapter describes the tools that can be used to develop and debug C applications under Linux. It is not
intended to be a tutorial on the C programming language, but rather to describe how to use the C compiler and some of the other C programming tools that are included with Linux. In this chapter you will learn about the following:
<BR>
<UL>
<LI>What C is
<BR>
<BR>
<LI>The GNU C compiler
<BR>
<BR>
<LI>Debugging GCC applications with gdb
<BR>
<BR>
</UL>
<P>You also will look at some of the useful C tools that are included with the Linux distribution. These tools include pretty print programs, additional debugging tools, and automatic function prototypers.
<BR>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>Pretty print programs are programs that automatically reformat code so that it has consistent indenting.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<BR>
<A NAME="E68E221"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>What Is C?</B></FONT></CENTER></H3>
<BR>
<P>C is a general-purpose programming language that has been around since the early days of the UNIX operating system. It was originally created by Dennis Ritchie at Bell Laboratories to aid in the development of UNIX. The first versions of UNIX were
written using assembly language and a language called B. C was developed to overcome some of the shortcomings of B. Since that time, C has become one of the most widely used computer languages in the world.
<BR>
<P>Why did C gain so much support in the programming world? Some of the reasons that C is so commonly used include the following:
<BR>
<UL>
<LI>It is a very portable language. Almost any computer that you can think of has at least one C compiler available for it, and the language syntax and function libraries are standardized across platforms. This is a very attractive feature for developers.
<BR>
<BR>
<LI>Executable programs written in C are fast.
<BR>
<BR>
<LI>C is the system language with all versions of UNIX.
<BR>
<BR>
</UL>
<P>C has evolved quite a bit over the last 20 years. In the late 1980s, the American National Standards Institute published a standard for the C language known as ANSI C. This further helped to secure C's future by making it even more consistent between
platforms. The 1980s also saw an object-oriented extension to C called C++. C++ will be described in the next chapter, "Programming in C++."
<BR>
<P>The C compiler that is available for Linux is the GNU C compiler, abbreviated GCC. This compiler was created under the Free Software Foundation's programming license and is therefore freely distributable. You will find it on the book's companion CD-ROM.
<BR>
<BR>
<A NAME="E68E222"></A>
<H3 ALIGN=CENTER>
<CENTER>
<FONT SIZE=5 COLOR="#FF0000"><B>The GNU C Compiler</B></FONT></CENTER></H3>
<BR>
<P>The GNU C Compiler (GCC) that is packaged with the Red Hat Linux distribution is a fully functional, ANSI C compatible compiler. If you are familiar with a C compiler on a different operating system or hardware platform, you will be able to learn GCC
very quickly. This section describes how to invoke GCC and introduces some of the commonly used GCC compiler options.
<BR>
<BR>
<A NAME="E69E332"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>Invoking GCC</B></FONT></CENTER></H4>
<BR>
<P>The GCC compiler is invoked by passing it a number of options and a number of filenames. The basic syntax for invoking gcc is this:
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gcc [options] [filenames]</FONT></PRE>
<P>The operations specified by the command-line options will be performed on each of the files that are specified on the command line. The next section describes the options that you will use most often.
<BR>
<BR>
<A NAME="E69E333"></A>
<H4 ALIGN=CENTER>
<CENTER>
<FONT SIZE=4 COLOR="#FF0000"><B>GCC Options</B></FONT></CENTER></H4>
<BR>
<P>There are more than 100 compiler options that can be passed to GCC. You will probably never use many of these options, but you will use some of them on a regular basis. Many of the GCC options consist of more than one character. For this reason you must
specify each option with its own hyphen, and you cannot group options after a single hyphen as you can with most Linux commands. For example, the following two commands are not the same:
<BR>
<PRE>
<FONT COLOR="#000080">gcc -p -g test.c
gcc -pg test.c</FONT></PRE>
<P>The first command tells GCC to compile test.c with profile information for the prof command and also to store debugging information within the executable. The second command just tells GCC to compile test.c with profile information for the gprof
command.
<BR>
<P>When you compile a program using gcc without any command-line options, it will create an executable file (assuming that the compile was successful) and call it a.out. For example, the following command would create a file named a.out in the current
directory.
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gcc test.c</FONT></PRE>
<P>To specify a name other than a.out for the executable file, you can use the -o compiler option. For example, to compile a C program file named count.c into an executable file named count, you would type the following command.
<BR>
<BR>
<PRE>
<FONT COLOR="#000080">gcc -o count count.c</FONT></PRE>
<BLOCKQUOTE>
<BLOCKQUOTE>
<HR ALIGN=CENTER>
<BR>
<NOTE>When you are using the -o option, the executable filename must occur directly after the -o on the command line.</NOTE>
<BR>
<HR ALIGN=CENTER>
</BLOCKQUOTE></BLOCKQUOTE>
<P>There are also compiler options that allow you to specify how far you want the compile to proceed. The -c option tells GCC 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 multifile C programs faster and easier to manage. Object code files that are created by GCC have a .o extension by default.
<BR>
<P>The -s compiler option tells GCC to stop the compile after it has generated the assembler files for the C code. Assembler files that are generated by GCC have a .s extension by default. The -E option instructs the compiler to perform only 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.
<BR>
<P>The following file extensions are assumed to be used when using the language compilers, including gcc:
<BR>
<TABLE BORDERCOLOR=#000040 BORDER=1 CELLSPACING=2 WIDTH="100%" CELLPADDING=2 >
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Extension
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Type of File</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.a
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Archive file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.c
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
C program file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.C, .cc, or .cxx
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
C++ program file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.h
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
A preprocessor (include) file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.i
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
An already preprocessed C file only needing compiling and assembling</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.ii
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
An already preprocessed C++ file only needing compiling and assembling</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.m
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Objective-C program file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.o
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Compiled object file</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.s
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Assembler source that had been preprocessed</FONT>
<TR>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
.S
</FONT>
<TD VALIGN=top BGCOLOR=#80FFFF ><FONT COLOR=#000080>
Assembler source which requires preprocessing</FONT>
</TABLE><BR>
<A NAME="E69E334"></A>
<H4 ALIGN=CENTER>
<CENTER>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?