📄 ch28_13.htm
字号:
<HTML><!--Distributed by F --><HEAD><TITLE>[Chapter 28] 28.13 make Isn't Just for Programmers! </TITLE><METANAME="DC.title"CONTENT="UNIX Power Tools"><METANAME="DC.creator"CONTENT="Jerry Peek, Tim O'Reilly & Mike Loukides"><METANAME="DC.publisher"CONTENT="O'Reilly & Associates, Inc."><METANAME="DC.date"CONTENT="1998-08-04T21:44:51Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-260-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch28_01.htm"TITLE="28. Comparing Files"><LINKREL="prev"HREF="ch28_12.htm"TITLE="28.12 Comparing Two Files with comm "><LINKREL="next"HREF="ch28_14.htm"TITLE="28.14 Even More Uses for make "></HEAD><BODYBGCOLOR="#FFFFFF"TEXT="#000000"><DIVCLASS="htmlnav"><H1><IMGSRC="gifs/smbanner.gif"ALT="UNIX Power Tools"USEMAP="#srchmap"BORDER="0"></H1><MAPNAME="srchmap"><AREASHAPE="RECT"COORDS="0,0,466,58"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="467,0,514,18"HREF="jobjects/fsearch.htm"ALT="Search this book"></MAP><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_12.htm"TITLE="28.12 Comparing Two Files with comm "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 28.12 Comparing Two Files with comm "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1">Chapter 28<BR>Comparing Files</FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_14.htm"TITLE="28.14 Even More Uses for make "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.14 Even More Uses for make "BORDER="0"></A></TD></TR></TABLE> <HRALIGN="LEFT"WIDTH="515"TITLE="footer"></DIV><DIVCLASS="SECT1"><H2CLASS="sect1"><ACLASS="title"NAME="UPT-ART-5380">28.13 make Isn't Just for Programmers! </A></H2><PCLASS="para"><ACLASS="indexterm"NAME="UPT-ART-5380-IX-COMPARING-FILES-MAKE-UTILITY-FOR"></A><ACLASS="indexterm"NAME="UPT-ART-5380-IX-MAKE-PROGRAM"></A>The <EMCLASS="emphasis">make</EM> program is a UNIX facility for describing dependenciesamong a group of related files, usually ones that are part of the same project.This facility has enjoyed widespread use in software development projects.Programmers use <EMCLASS="emphasis">make</EM> to describe how to "make" a program - whatsource files need to be compiled, what libraries must be included,and which object files need to be linked.By keeping track of these relationships in a single place,individual members of a software development team canmake changes to a single module, run <EMCLASS="emphasis">make</EM>, and be assured that theprogram reflects the latest changes made by others on the team.</P><PCLASS="para">We group <EMCLASS="emphasis">make</EM> with the other commands for keeping track of differencesbetween files only by a leap of the imagination.However, although it does not compare two versions of the samesource file, it can be used to compare versions such as asource file and the formatted output.</P><PCLASS="para">Part of what makes UNIX a productive environment for text processingis discovering other uses for standard programs.The <EMCLASS="emphasis">make</EM> utility has many possible applications for a documentationproject.One such use is to maintain up-to-date copies of formatted filesthat make up a single manual and provide users with a way of obtaining aprinted copy of the entire manual without having to know whichpreprocessors or<SPANCLASS="link"><EMCLASS="emphasis">nroff</EM>/<EMCLASS="emphasis">troff</EM> (<ACLASS="linkend"HREF="ch43_13.htm"TITLE="The Text Formatters nroff, troff, ditroff, ... ">43.13</A>)</SPAN>options need to be invoked.</P><PCLASS="para">The basic operation that <EMCLASS="emphasis">make</EM> performs is to compare two sets offiles, for example, formatted and unformatted files, anddetermine if any members of one set, the unformatted files, are morerecent than their counterpart in the other set, the formatted files.This is accomplished by simply comparing the<SPANCLASS="link">last-modification date (<ACLASS="linkend"HREF="ch16_05.htm"TITLE="The Three UNIX File Times ">16.5</A>)</SPAN>("timestamp") of pairs of files.If the unformatted source file has been modified since the formattedfile was made, <EMCLASS="emphasis">make</EM> executes the specified command to "remake" theformatted file.</P><PCLASS="para"><ACLASS="indexterm"NAME="AUTOID-31633"></A>To use <EMCLASS="emphasis">make</EM>, you have to write a description file, usually named<EMCLASS="emphasis">makefile</EM> (or <EMCLASS="emphasis">Makefile</EM>), that resides in the working directory forthe project.The <EMCLASS="emphasis">makefile</EM> specifies a hierarchy of dependencies among individualfiles, called components.At the top of this hierarchy is a target.For our purposes, you can think of the target as a printed copy of abook; the components are formatted files generated by processing anunformatted file with <EMCLASS="emphasis">nroff</EM>.</P><PCLASS="para">Here's the <EMCLASS="emphasis">makefile</EM> that reflects these dependencies:</P><PCLASS="para"><TABLECLASS="screen.co"BORDER="1"><TR><THVALIGN="TOP"><PRECLASS="calloutlist"> <ACLASS="co"HREF="ch43_02.htm"TITLE="43.2 Introduction to Printing on UNIX ">lp</A> <ACLASS="co"HREF="ch43_15.htm"TITLE="43.15 From a Source File to the Printer ">tbl</A> </PRE></TH><TDVALIGN="TOP"><PRECLASS="screen">manual: ch01.fmt ch02.fmt ch03.fmt lp ch0[1-3].fmtch01.fmt: ch01 nroff -mm ch01 > ch01.fmtch02.fmt: ch02 tbl ch02 | nroff -mm > ch02.fmtch03.fmt: ch03a ch03b ch03c nroff -mm ch03[abc] > ch03.fmt</PRE></TD></TR></TABLE></P><PCLASS="para">This hierarchy is represented in<ACLASS="xref"HREF="ch28_13.htm#UPT-ART-5380-FIG-0"TITLE="What makefile Describes: Files and Commands to Make Manual">Figure 28.1</A>.</P><H4CLASS="figure"><ACLASS="title"NAME="UPT-ART-5380-FIG-0">Figure 28.1: What makefile Describes: Files and Commands to Make Manual</A></H4><IMGCLASS="graphic"SRC="figs/5380.gif"ALT="Figure 28.1"><PCLASS="para">The target is <EMCLASS="emphasis">manual</EM>, which is made up of three formatted fileswhose names appear after the colon.Each of these components has its own dependency line.For instance, <EMCLASS="emphasis">ch01.fmt</EM> is dependent upon a coded filenamed <EMCLASS="emphasis">ch01</EM>. Underneath the dependency line is the commandthat generates <EMCLASS="emphasis">ch01.fmt</EM>. Each command line must begin with a TAB.</P><PCLASS="para">When you enter the command <CODECLASS="literal">make</CODE>, the end result is that the threeformatted files are spooled to the printer.However, a sequence of operations is performed before this final action.The dependency line for each component is evaluated, determining ifthe coded file has been modified since the last time the formattedfile was made.The formatting command will be executed only if the coded file ismore recent.After all the components are made, the<SPANCLASS="link"><EMCLASS="emphasis">lp</EM> (<ACLASS="linkend"HREF="ch43_02.htm"TITLE="Introduction to Printing on UNIX ">43.2</A>)</SPAN>command is executed.</P><PCLASS="para">As an example of this process, we'll assume that all the formattedfiles are up-to-date.Then by editing the source file <EMCLASS="emphasis">ch03a</EM>, we change the modification time.When you execute the <EMCLASS="emphasis">make</EM> command, any output files dependent on<EMCLASS="emphasis">ch03a</EM> are reformatted:</P><PCLASS="para"><BLOCKQUOTECLASS="screen"><PRECLASS="screen">$ <CODECLASS="userinput"><B>make</B></CODE>nroff -mm ch03[abc] > ch03.fmtlp ch0[1-3].fmt</PRE></BLOCKQUOTE></P><PCLASS="para">Only <EMCLASS="emphasis">ch03.fmt</EM> needs to be remade.As soon as that formatting command finishes, the command underneaththe target <EMCLASS="emphasis">manual</EM> is executed, spooling the files to the printer.</P><PCLASS="para">Although this example has actually made only limited use of <EMCLASS="emphasis">make</EM>'sfacilities, we hope it suggests more ways to use <EMCLASS="emphasis">make</EM> in adocumention project.You can keep your <EMCLASS="emphasis">makefiles</EM> just this simple, or you can go on tolearn additional notation, such as internal macros and suffixes, inan effort to generalize the description file for increased usefulness.</P><DIVCLASS="sect1info"><PCLASS="SECT1INFO">- <SPANCLASS="authorinitials">TOR</SPAN> <SPANCLASS="bibliomisc">from <CITECLASS="citetitle">UNIX Text Processing</CITE>, Hayden Books, 1987</SPAN></P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><TABLEWIDTH="515"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_12.htm"TITLE="28.12 Comparing Two Files with comm "><IMGSRC="gifs/txtpreva.gif"SRC="gifs/txtpreva.gif"ALT="Previous: 28.12 Comparing Two Files with comm "BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="book"HREF="index.htm"TITLE="UNIX Power Tools"><IMGSRC="gifs/txthome.gif"SRC="gifs/txthome.gif"ALT="UNIX Power Tools"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172"><ACLASS="SECT1"HREF="ch28_14.htm"TITLE="28.14 Even More Uses for make "><IMGSRC="gifs/txtnexta.gif"SRC="gifs/txtnexta.gif"ALT="Next: 28.14 Even More Uses for make "BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="172">28.12 Comparing Two Files with comm </TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="171"><ACLASS="index"HREF="index/idx_0.htm"TITLE="Book Index"><IMGSRC="gifs/index.gif"SRC="gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="172">28.14 Even More Uses for make </TD></TR></TABLE><HRALIGN="LEFT"WIDTH="515"TITLE="footer"><IMGSRC="gifs/smnavbar.gif"SRC="gifs/smnavbar.gif"USEMAP="#map"BORDER="0"ALT="The UNIX CD Bookshelf Navigation"><MAPNAME="map"><AREASHAPE="RECT"COORDS="0,0,73,21"HREF="../index.htm"ALT="The UNIX CD Bookshelf"><AREASHAPE="RECT"COORDS="74,0,163,21"HREF="index.htm"ALT="UNIX Power Tools"><AREASHAPE="RECT"COORDS="164,0,257,21"HREF="../unixnut/index.htm"ALT="UNIX in a Nutshell"><AREASHAPE="RECT"COORDS="258,0,321,21"HREF="../vi/index.htm"ALT="Learning the vi Editor"><AREASHAPE="RECT"COORDS="322,0,378,21"HREF="../sedawk/index.htm"ALT="sed & awk"><AREASHAPE="RECT"COORDS="379,0,438,21"HREF="../ksh/index.htm"ALT="Learning the Korn Shell"><AREASHAPE="RECT"COORDS="439,0,514,21"HREF="../lrnunix/index.htm"ALT="Learning the UNIX Operating System"></MAP></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -