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

📄 444-447.html

📁 linux-unix130.linux.and.unix.ebooks130 linux and unix ebookslinuxLearning Linux - Collection of 12 E
💻 HTML
字号:
<HTML>

<HEAD>

<TITLE>Linux Configuration and Installation:Programming in Linux</TITLE>

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

 -->




<!--ISBN=1558285660//-->

<!--TITLE=Linux Configuration and Installation//-->

<!--AUTHOR=Patrick Volkerding//-->

<!--AUTHOR=Kevin Reichard//-->

<!--AUTHOR=Eric Foster//-->

<!--PUBLISHER=IDG Books Worldwide, Inc.//-->

<!--IMPRINT=M & T Books//-->

<!--CHAPTER=10//-->

<!--PAGES=444-447//-->

<!--UNASSIGNED1//-->

<!--UNASSIGNED2//-->



<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="439-444.html">Previous</A></TD>

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

<TD><A HREF="447-449.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>

<P><BR></P>

<H4 ALIGN="LEFT"><A NAME="Heading5"></A><FONT COLOR="#000077">The Cc Command</FONT></H4>

<P>The <B>cc</B> command executes the C compiler, which can compile and link C programs into executable commands. To test your Linux C compiler, we&#146;ll use the following short program:</P>

<!-- CODE //-->

<PRE>

     /*

      * Example C program for Chapter 10,

      * Linux Configuration and Installation.

      */

     #include &lt;stdio.h&gt;



     int main(int argc, char** argv)



     &#123;

         /* This is a comment. */

         printf("Linux is my favorite O.S.\n");



         return 0;

     &#125;



     /* chap10.c */

</PRE>

<!-- END CODE //-->

<P>Enter the preceding code into a text file named <B>chap10.c</B>, using your favorite Linux text editor.</P>

<P>It&#146;s a good idea to always name C program files with a <I>.c</I> extension. This isn&#146;t required, but following conventions like this makes Linux easier to use.</P>

<P>After you type in this short program, you can do the following simple steps to create a working executable program from this C file.</P>

<P>The program you typed in was simply a text file. There&#146;s nothing in it to make it an executable command. To do so, we need to compile and link the program. Both steps are accomplished by the following <B>cc</B> command:</P>

<!-- CODE SNIP //-->

<PRE>

     $ cc -o chap10 chap10.c

</PRE>

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

<P>This command runs the C compiler, <B>cc</B>. The <I>-o</I> option tells <B>cc</B> to build a program named <B>chap10</B> (the default name without the <I>-o</I> option is the awkward <B>a.out</B>). The <I>chap10.c</I> part of the command tells <B>cc</B> to compile the file named <B>chap10.c</B>. The <B>cc</B> command both compiled and linked the program.</P>

<P>You should now have an executable program named <B>chap10</B>. You can execute this program by typing <B>chap10</B> at the command line. When you do, you&#146;ll see the following output:</P>

<!-- CODE SNIP //-->

<PRE>

     $ chap10

     Linux is my favorite O.S.

</PRE>

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

<P>Now you&#146;re a real C programmer, ready for a lucrative new career.

</P>

<P><FONT SIZE="+1"><B>Compiling the Long Way</B></FONT></P>

<P>When we used the <B>cc</B> command, <B>cc</B> first compiled the program into an object module. Then <B>cc</B> linked the object module to create an executable program, the file named <B>chap10</B>. This is very important if you need to compile more than one file into your program. Most C programs require a number of <B>.c</B> files, all of which must be compiled and linked to form one program. One of the main reasons for separating C programs into multiple files is sanity: reading a 1MB program in one file is ludicrous. And yes, C programs get to this size, and even much bigger than 1 megabyte. Some C programs we&#146;ve worked on include more than a million lines of C code. You need to know how to compile multiple <I>.c</I> files into one executable command because the vast majority of Linux freeware comes in this fashion.</P>

<P>To use the long method of compiling and linking, we split the tasks into two steps. First, you compile all the <I>.c</I> files you require. Then you link the resulting <I>.o</I> files (we&#146;ll get into this later) into your executable program. Because we have a very small C program typed in already (you did type it in, didn&#146;t you?), we&#146;ll start with that.</P>

<P>Compile <B>chap10.c</B> into an object module, an <I>.o</I> file, with the following command:</P>

<!-- CODE SNIP //-->

<PRE>

     $ cc -c chap10.c

</PRE>

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

<P>If you are successful, you should see a file named <B>chap10.o</B> in your directory. The <I>.o</I> file is called the <I>object file</I> (or <I>object module</I>); it contains unlinked machine code.</P>

<P>The next step is to link the object files (there&#146;s usually more than one) into an executable file. To do this, we again use the <I>-o</I> option to <B>cc</B>, but this time we pass a <I>.o</I> file at the end of the command line, rather than the <I>.c</I> file we used earlier:</P>

<!-- CODE SNIP //-->

<PRE>

     $ cc -o chap10 chap10.o

</PRE>

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

<P>This command links the file <B>chap10.o</B> into the executable program <B>chap10</B>. You can place more than one object filename on the command line, as in the following example:</P>

<!-- CODE SNIP //-->

<PRE>

     $ cc -o chap10 chap10_a.o chap10_b.o chap10_c.o

</PRE>

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

<P>Normally you&#146;ll want to pick more descriptive filenames than the ones we&#146;ve used.

</P><P><BR></P>

<CENTER>

<TABLE BORDER>

<TR>

<TD><A HREF="439-444.html">Previous</A></TD>

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

<TD><A HREF="447-449.html">Next</A></TD>

</TR>

</TABLE>

</CENTER>





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

<!-- begin footer information -->





</body></html>

⌨️ 快捷键说明

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