📄 457-460.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=457-460//-->
<!--UNASSIGNED1//-->
<!--UNASSIGNED2//-->
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="455-457.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="460-464.html">Next</A></TD>
</TR>
</TABLE>
</CENTER>
<P><BR></P>
<P>To create a target in the <B>Makefile</B>, begin with a new line and name the target—what you want to build—then place a colon (<B>:</B>) and a tab, and then list the files the target depends on. Starting on the next line, begin with a tab, then place the UNIX command used to build the target. You can have multiple commands, each of which should go on its own line, and every command line must start with a tab.</P>
<P>In the abstract, the <B>Makefile</B> rules look like the following:</P>
<DL>
<DD><I>what_to_build: what_it_depends_on</I>
<DD><I>command1_to_build_it</I>
<DD><I>command2_to_build_it</I>
<DD><I>command3_to_build_it</I>
<DD>…
<DD><I>lastcommand_to_build_it</I>
</DL>
<P>In the abstract, this looks confusing. Here’s a more concrete example, using the <B>chap10</B> program we provided earlier.</P>
<P>The target we want to build is the <B>chap10</B> program. The <B>chap10</B> program (the target) depends on the object module <B>chap10.o</B>. Once we have the object module <B>chap10.o</B>, then the command line to create the <B>chap10</B> program is:</P>
<!-- CODE SNIP //-->
<PRE>
chap10: chap10.o
cc -o chap10 chap10.o
</PRE>
<!-- END CODE SNIP //-->
<P>This <B>make</B> rule states that if <B>chap10.o</B> has a more recent date, then execute the <B>cc</B> command to build the <B>chap10</B> program from the object module <B>chap10.o</B>.</P>
<P>This is just part of the task; we still have to compile <B>chap10.c</B> to create the object module <B>chap10.o</B>. That is, the file <B>chap10.o</B>, is said to depend on the file <B>chap10.c</B>. You build <B>chap10.o</B> from <B>chap10.c</B>. To do this, we use another <B>make</B> rule.</P>
<P>This time, the object module <B>chap10.o</B> depends on the text file <B>chap10.c</B>. The command to build the object module is:</P>
<!-- CODE SNIP //-->
<PRE>
chap10.o: chap10.c
cc -c chap10.c
</PRE>
<!-- END CODE SNIP //-->
<P>With this <B>make</B> rule, if you edit <B>chap10.c</B>, you’ll make the file <B>chap10.c</B> have a more recent date/time than the object module <B>chap10.o</B>. This causes <B>make</B> to trigger the <B>cc</B> command to compile <B>chap10.c</B> into <B>chap10.o</B>.</P>
<P>You’ve discovered the secret to <B>make</B>’s rules. Everything depends on the date/time of the files, a very simple—but clever—idea. The idea is that if the text of the program <I>.c</I> file is modified, you better rebuild the program with <B>cc</B>. Because most users are impatient, if the <I>.c</I> file hasn’t been changed, there’s simply no reason (at least in our example) to rebuild the program with <B>cc</B>.</P>
<P><FONT SIZE="+1"><B>A Make Example</B></FONT></P>
<P>To try <B>make</B>, enter the following text into a file named <B>Makefile</B>:</P>
<!-- CODE //-->
<PRE>
#
# Test Makefile
#
# The program chap10 depends on chap10.o.
chap10: chap10.o
cc -o chap10 chap10.o
# The object module chap10.o depends on chap10.c.
chap10.o: chap10.c
cc -c chap10.c
</PRE>
<!-- END CODE //-->
<P>This <B>Makefile</B> should be in the same directory as your sample C program file, <B>chap10.c</B>. To use <B>make</B>, we need to tell it what to make, that is, what target we want to build. In our case, we want <B>make</B> to build the program <B>chap10</B>. The following command will build this program:</P>
<!-- CODE SNIP //-->
<PRE>
$ make chap10
cc -c chap10.c
cc -o chap10 chap10.o
</PRE>
<!-- END CODE SNIP //-->
<P>We should now have the <B>chap10</B> program ready to run. If we try <B>make</B> again, it—being very lazy—tells us there’s no new work to do:</P>
<!-- CODE SNIP //-->
<PRE>
$ make chap10
chap10 is up to date.
</PRE>
<!-- END CODE SNIP //-->
<P>Why? Because the <B>chap10</B> program was built, and nothing has changed. Now, edit the <B>chap10.c</B> file again or use the <B>touch</B> command to bump up the date/time associated with the file:</P>
<!-- CODE SNIP //-->
<PRE>
$ touch chap10.c
</PRE>
<!-- END CODE SNIP //-->
<P>When you call <B>make</B> again, it knows it now needs to rebuild the <B>chap10</B> program, because presumably the <B>chap10.c</B> file has changed since the last time <B>chap10.c</B> was compiled with <B>cc</B>. Because <B>touch</B> only updates the date/time associated with the file and doesn’t change the internals of the file in any way, we’ve just fooled <B>make</B>. <B>make</B> doesn’t bother checking if a file is different; it merely checks the time the file was last written to, blindly assuming that no one would ever write to a file without modifying its contents. Normally, though, you don’t want to fool <B>make</B>; use its simple rules to make your life easier.</P>
<P><B>Make</B> supports a number of useful command-line parameters, as shown in Table 10.4.</P>
<TABLE WIDTH="100%"><CAPTION><B>Table 10.4</B> Make Command-Line Parameters
<TR>
<TH WIDTH="30%" ALIGN="LEFT">Parameter
<TH WIDTH="70%" ALIGN="LEFT">Meaning
<TR>
<TH COLSPAN="2"><HR>
<TR>
<TD WIDTH="30%"><B>-f <I>makefile</I></B>
<TD WIDTH="70%">Uses the named file instead of <B><I>Makefile</I></B> for the rules
<TR>
<TD VALIGN="TOP"><B>-n</B>
<TD>Runs in no-execute mode—only prints the commands, doesn’t execute them
<TR>
<TD VALIGN="TOP"><B>-s</B>
<TD>Runs in silent mode; doesn’t print any commands <B>make</B> executes
<TR>
<TD COLSPAN="2"><HR>
</TABLE>
<P><BR></P>
<CENTER>
<TABLE BORDER>
<TR>
<TD><A HREF="455-457.html">Previous</A></TD>
<TD><A HREF="../ewtoc.html">Table of Contents</A></TD>
<TD><A HREF="460-464.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 + -