⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch27.htm

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTM
📖 第 1 页 / 共 4 页
字号:


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

 -->




 



<UL>



	<LI><A HREF="#Heading1">- 27 -</A>



	<UL>



		<LI><A HREF="#Heading2">Programming in C</A>



		<UL>



			<LI><A HREF="#Heading3">NOTE</A>



			<LI><A HREF="#Heading4">What Is C?</A>



			<LI><A HREF="#Heading5">The GNU C Compiler</A>



			<UL>



				<LI><A HREF="#Heading6">Invoking GCC</A>



				<LI><A HREF="#Heading7">GCC Options</A>



			</UL>



			<LI><A HREF="#Heading8">NOTE</A>



			<UL>



				<LI><A HREF="#Heading9">Optimization Options</A>



				<LI><A HREF="#Heading10">Debugging and Profiling Options</A>



			</UL>



			<LI><A HREF="#Heading11">Debugging GCC Programs with gdb</A>



			<UL>



				<LI><A HREF="#Heading12">Compiling Code for Debugging</A>



				<LI><A HREF="#Heading13">gdb Basic Commands</A>



				<LI><A HREF="#Heading14">Sample gdb Session</A>



			</UL>



			<LI><A HREF="#Heading15">NOTE</A>



			<LI><A HREF="#Heading16">TIP</A>



			<LI><A HREF="#Heading17">NOTE</A>



			<LI><A HREF="#Heading18">Additional C Programming Tools</A>



			<UL>



				<LI><A HREF="#Heading19">xxgdb</A>



				<UL>



					<LI><A HREF="#Heading20">FIGURE 27.1.</A>



					<LI><A HREF="#Heading21">FIGURE 27.2.</A>



					<LI><A HREF="#Heading22">FIGURE 27.3.</A>



					<LI><A HREF="#Heading23">FIGURE 27.4.</A>



				</UL>



				<LI><A HREF="#Heading24">calls</A>



			</UL>



			<LI><A HREF="#Heading25">NOTE</A>



			<LI><A HREF="#Heading26">WARNING</A>



			<UL>



				<LI><A HREF="#Heading27">cproto</A>



			</UL>



			<LI><A HREF="#Heading28">NOTE</A>



			<LI><A HREF="#Heading29">WARNING</A>



			<UL>



				<LI><A HREF="#Heading30">indent</A>



				<LI><A HREF="#Heading31">gprof</A>



			</UL>



			<LI><A HREF="#Heading32">TIP</A>



			<UL>



				<LI><A HREF="#Heading33">f2c and p2c</A>



			</UL>



			<LI><A HREF="#Heading34">NOTE</A>



			<LI><A HREF="#Heading35">Summary</A>



			<LI><A HREF="#Heading36">TIP</A>



		</UL>



	</UL>



</UL>







<P>



<HR SIZE="4">







<H2 ALIGN="CENTER"><A NAME="Heading1<FONT COLOR="#000077">- 27 -</FONT></H2>



<H2 ALIGN="CENTER"><A NAME="Heading2<FONT COLOR="#000077">Programming in C</FONT></H2>



<P><I>by Rick McMullin</I></P>



<P>IN THIS CHAPTER</P>







<UL>



	<LI>What Is C?



	<P>



	<LI>The GNU C Compiler



	<P>



	<LI>Debugging GCC Programs with gdb



	<P>



	<LI>Additional C Programming Tools 501



</UL>







<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:







<UL>



	<LI>What C is



	<P>



	<LI>The GNU C compiler



	<P>



	<LI>Debugging GCC applications with <TT>gdb</TT>



</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.







<DL>



	<DT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading3<FONT COLOR="#000077"><B>NOTE: </B></FONT>Pretty print programs



	are programs that automatically reformat code so that it has consistent indenting.



	



<HR>







</DL>







<H3 ALIGN="CENTER"><A NAME="Heading4<FONT COLOR="#000077">What Is C?</FONT></H3>



<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.</P>



<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:







<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.



	<P>



	<LI>Executable programs written in C are fast.



	<P>



	<LI>C is the system language with all versions of UNIX.



</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, &quot;Programming in C++.&quot;</P>



<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 this book's companion



CD-ROM.



<H3 ALIGN="CENTER"><A NAME="Heading5<FONT COLOR="#000077">The GNU C Compiler</FONT></H3>



<P>The GNU C Compiler (GCC) that is packaged with the Slackware 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 many of the



commonly used GCC compiler options.



<H4 ALIGN="CENTER"><A NAME="Heading6<FONT COLOR="#000077">Invoking GCC</FONT></H4>



<P>The GCC compiler is invoked by passing it a number of options and a number of



filenames. The basic syntax for invoking <TT>gcc</TT> is this:</P>



<PRE><FONT COLOR="#0066FF">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.



<H4 ALIGN="CENTER"><A NAME="Heading7<FONT COLOR="#000077">GCC Options</FONT></H4>



<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:</P>



<PRE><FONT COLOR="#0066FF">gcc -p -g test.c



gcc -pg test.c



</FONT></PRE>



<P>The first command tells GCC to compile <TT>test.c</TT> with profile information



for the <TT>prof</TT> command and also to store debugging information within the



executable. The second command just tells GCC to compile <TT>test.c</TT> with profile



information for the <TT>gprof</TT> command.</P>



<P>When you compile a program using <TT>gcc</TT> without any command-line options,



it will create an executable file (assuming that the compile was successful) and



call it <TT>a.out</TT>. For example, the following command would create a file named



<TT>a.out</TT> in the current directory.</P>



<PRE><FONT COLOR="#0066FF">gcc test.c



</FONT></PRE>



<P>To specify a name other than <TT>a.out</TT> for the executable file, you can 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>, you would type the following command.</P>



<PRE><FONT COLOR="#0066FF">gcc -o count count.c



</FONT></PRE>







<DL>



	<DT><FONT COLOR="#0066FF"></FONT></DT>



</DL>











<DL>



	<DD>



<HR>



<A NAME="Heading8<FONT COLOR="#000077"><B>NOTE:</B> </FONT>When you are using



	the <TT>-o</TT> option, the executable filename must occur directly after the <TT>-o</TT>



	on the command line. 



<HR>







</DL>







<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 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 <TT>.o</TT> extension



by default.</P>



<P>The <TT>-S</TT> 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 <TT>.s</TT> extension by default. The <TT>-E</TT> 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.



<H4 ALIGN="CENTER"><A NAME="Heading9<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 will 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. These optimizations will in most cases 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



will cause the compilation speed to be slower than it is when using the<TT> -O</TT>



option, but will typically result 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 GCC manual page by typing <TT>man gcc</TT> on the



command line.



<H4 ALIGN="CENTER"><A NAME="Heading10<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,



refer to the &quot;Debugging GCC Programs with <TT>gdb</TT>&quot; 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 additional information



on <TT>gprof</TT>, refer to the &quot;<TT>gprof</TT>&quot; section in this chapter.



<H3 ALIGN="CENTER"><A NAME="Heading11<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++ 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:







<UL>



	<LI>It enables you to monitor the value of variables that are contained in your program.



	<P>



	<LI>It enables you to set breakpoints that will stop the program at a specific line



	of code.



	<P>



	<LI>It enables you to step through the code, line by line.



</UL>







<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> should start and you will



⌨️ 快捷键说明

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