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

📄 make.html

📁 vxworks相关论文
💻 HTML
📖 第 1 页 / 共 5 页
字号:
is to run the specified commands.  Targets that do not refer to filesbut are just actions are called <EM>phony targets</EM>.  See section <A HREF="make.html#SEC31">Phony Targets</A>, for information about this kind of target.  See section <A HREF="make.html#SEC46">Errors in Commands</A>, to see how to cause <CODE>make</CODE> to ignore errorsfrom <CODE>rm</CODE> or any other command.<A NAME="IDX25"></A><A NAME="IDX26"></A></P><H2><A NAME="SEC7" HREF="make_toc.html#TOC7">How <CODE>make</CODE> Processes a Makefile</A></H2><P><A NAME="IDX27"></A><A NAME="IDX28"></A></P><P>By default, <CODE>make</CODE> starts with the first rule (not counting ruleswhose target names start with <SAMP>`.'</SAMP>).  This is called the<EM>default goal</EM>.  (<EM>Goals</EM> are the targets that <CODE>make</CODE>strives ultimately to update.  See section <A HREF="make.html#SEC80">Arguments to Specify the Goals</A>.)<A NAME="IDX29"></A><A NAME="IDX30"></A><A NAME="IDX31"></A></P><P>In the simple example of the previous section, the default goal is toupdate the executable program <TT>`edit'</TT>; therefore, we put that rulefirst.Thus, when you give the command:</P><PRE>make</PRE><P><CODE>make</CODE> reads the makefile in the current directory and begins byprocessing the first rule.  In the example, this rule is for relinking<TT>`edit'</TT>; but before <CODE>make</CODE> can fully process this rule, itmust process the rules for the files that <TT>`edit'</TT> depends on,which in this case are the object files.  Each of these files isprocessed according to its own rule.  These rules say to update each<SAMP>`.o'</SAMP> file by compiling its source file.  The recompilation mustbe done if the source file, or any of the header files named asdependencies, is more recent than the object file, or if the objectfile does not exist.</P><P>The other rules are processed because their targets appear asdependencies of the goal.  If some other rule is not depended on by thegoal (or anything it depends on, etc.), that rule is not processed,unless you tell <CODE>make</CODE> to do so (with a command such as<CODE>make clean</CODE>).</P><P>Before recompiling an object file, <CODE>make</CODE> considers updating itsdependencies, the source file and header files.  This makefile does notspecify anything to be done for them--the <SAMP>`.c'</SAMP> and <SAMP>`.h'</SAMP> filesare not the targets of any rules--so <CODE>make</CODE> does nothing for thesefiles.  But <CODE>make</CODE> would update automatically generated C programs,such as those made by Bison or Yacc, by their own rules at this time.</P><P>After recompiling whichever object files need it, <CODE>make</CODE> decideswhether to relink <TT>`edit'</TT>.  This must be done if the file<TT>`edit'</TT> does not exist, or if any of the object files are newer thanit.  If an object file was just recompiled, it is now newer than<TT>`edit'</TT>, so <TT>`edit'</TT> is relinked.<A NAME="IDX32"></A></P><P>Thus, if we change the file <TT>`insert.c'</TT> and run <CODE>make</CODE>,<CODE>make</CODE> will compile that file to update <TT>`insert.o'</TT>, and thenlink <TT>`edit'</TT>.  If we change the file <TT>`command.h'</TT> and run<CODE>make</CODE>, <CODE>make</CODE> will recompile the object files <TT>`kbd.o'</TT>,<TT>`command.o'</TT> and <TT>`files.o'</TT> and then link the file <TT>`edit'</TT>.</P><H2><A NAME="SEC8" HREF="make_toc.html#TOC8">Variables Make Makefiles Simpler</A></H2><P><A NAME="IDX33"></A><A NAME="IDX34"></A></P><P>In our example, we had to list all the object files twice in the rule for<TT>`edit'</TT> (repeated here):</P><PRE>edit : main.o kbd.o command.o display.o \              insert.o search.o files.o utils.o        cc -o edit main.o kbd.o command.o display.o \                   insert.o search.o files.o utils.o</PRE><P><A NAME="IDX35"></A>Such duplication is error-prone; if a new object file is added to thesystem, we might add it to one list and forget the other.  We can eliminatethe risk and simplify the makefile by using a variable.  <EM>Variables</EM>allow a text string to be defined once and substituted in multiple placeslater (see section <A HREF="make.html#SEC55">How to Use Variables</A>).</P><P><A NAME="IDX36"></A><A NAME="IDX37"></A><A NAME="IDX38"></A><A NAME="IDX39"></A><A NAME="IDX40"></A>It is standard practice for every makefile to have a variable named<CODE>objects</CODE>, <CODE>OBJECTS</CODE>, <CODE>objs</CODE>, <CODE>OBJS</CODE>, <CODE>obj</CODE>,or <CODE>OBJ</CODE> which is a list of all object file names.  We woulddefine such a variable <CODE>objects</CODE> with a line like this in themakefile:</P><PRE>objects = main.o kbd.o command.o display.o \          insert.o search.o files.o utils.o</PRE><P>Then, each place we want to put a list of the object file names, we cansubstitute the variable's value by writing <SAMP>`$(objects)'</SAMP>(see section <A HREF="make.html#SEC55">How to Use Variables</A>).  </P><P>Here is how the complete simple makefile looks when you use a variablefor the object files:</P><PRE>objects = main.o kbd.o command.o display.o \          insert.o search.o files.o utils.oedit : $(objects)        cc -o edit $(objects)main.o : main.c defs.h        cc -c main.ckbd.o : kbd.c defs.h command.h        cc -c kbd.ccommand.o : command.c defs.h command.h        cc -c command.cdisplay.o : display.c defs.h buffer.h        cc -c display.cinsert.o : insert.c defs.h buffer.h        cc -c insert.csearch.o : search.c defs.h buffer.h        cc -c search.cfiles.o : files.c defs.h buffer.h command.h        cc -c files.cutils.o : utils.c defs.h        cc -c utils.cclean :        rm edit $(objects)</PRE><H2><A NAME="SEC9" HREF="make_toc.html#TOC9">Letting <CODE>make</CODE> Deduce the Commands</A></H2><P><A NAME="IDX41"></A><A NAME="IDX42"></A><A NAME="IDX43"></A></P><P>It is not necessary to spell out the commands for compiling the individualC source files, because <CODE>make</CODE> can figure them out: it has an<EM>implicit rule</EM> for updating a <SAMP>`.o'</SAMP> file from a correspondinglynamed <SAMP>`.c'</SAMP> file using a <SAMP>`cc -c'</SAMP> command.  For example, it willuse the command <SAMP>`cc -c main.c -o main.o'</SAMP> to compile <TT>`main.c'</TT> into<TT>`main.o'</TT>.  We can therefore omit the commands from the rules for theobject files.  See section <A HREF="make.html#SEC86">Using Implicit Rules</A>.</P><P>When a <SAMP>`.c'</SAMP> file is used automatically in this way, it is alsoautomatically added to the list of dependencies.  We can therefore omitthe <SAMP>`.c'</SAMP> files from the dependencies, provided we omit the commands.</P><P>Here is the entire example, with both of these changes, and a variable<CODE>objects</CODE> as suggested above:</P><PRE>objects = main.o kbd.o command.o display.o \          insert.o search.o files.o utils.oedit : $(objects)        cc -o edit $(objects)main.o : defs.hkbd.o : defs.h command.hcommand.o : defs.h command.hdisplay.o : defs.h buffer.hinsert.o : defs.h buffer.hsearch.o : defs.h buffer.hfiles.o : defs.h buffer.h command.hutils.o : defs.h.PHONY : cleanclean :        -rm edit $(objects)</PRE><P>This is how we would write the makefile in actual practice.  (Thecomplications associated with <SAMP>`clean'</SAMP> are described elsewhere.See section <A HREF="make.html#SEC31">Phony Targets</A>, and section <A HREF="make.html#SEC46">Errors in Commands</A>.)</P><P>Because implicit rules are so convenient, they are important.  Youwill see them used frequently.</P><H2><A NAME="SEC10" HREF="make_toc.html#TOC10">Another Style of Makefile</A></H2><P><A NAME="IDX44"></A></P><P>When the objects of a makefile are created only by implicit rules, analternative style of makefile is possible.  In this style of makefile,you group entries by their dependencies instead of by their targets.Here is what one looks like:</P><PRE>objects = main.o kbd.o command.o display.o \          insert.o search.o files.o utils.oedit : $(objects)        cc -o edit $(objects)$(objects) : defs.hkbd.o command.o files.o : command.hdisplay.o insert.o search.o files.o : buffer.h</PRE><P>Here <TT>`defs.h'</TT> is given as a dependency of all the object files;<TT>`command.h'</TT> and <TT>`buffer.h'</TT> are dependencies of the specificobject files listed for them.</P><P>Whether this is better is a matter of taste: it is more compact, but somepeople dislike it because they find it clearer to put all the informationabout each target in one place.</P><H2><A NAME="SEC11" HREF="make_toc.html#TOC11">Rules for Cleaning the Directory</A></H2><P><A NAME="IDX45"></A><A NAME="IDX46"></A></P><P>Compiling a program is not the only thing you might want to write rulesfor.  Makefiles commonly tell how to do a few other things besidescompiling a program: for example, how to delete all the object filesand executables so that the directory is <SAMP>`clean'</SAMP>.</P><P><A NAME="IDX47"></A>Here is how wecould write a <CODE>make</CODE> rule for cleaning our example editor:</P><PRE>clean:        rm edit $(objects)</PRE><P>In practice, we might want to write the rule in a somewhat morecomplicated manner to handle unanticipated situations.  We would do this:</P><PRE>.PHONY : cleanclean :        -rm edit $(objects)</PRE><P>This prevents <CODE>make</CODE> from getting confused by an actual filecalled <TT>`clean'</TT> and causes it to continue in spite of errors from<CODE>rm</CODE>.  (See section <A HREF="make.html#SEC31">Phony Targets</A>, and section <A HREF="make.html#SEC46">Errors in Commands</A>.)</P><P>A rule such as this should not be placed at the beginning of themakefile, because we do not want it to run by default!  Thus, in theexample makefile, we want the rule for <CODE>edit</CODE>, which recompilesthe editor, to remain the default goal.</P><P>Since <CODE>clean</CODE> is not a dependency of <CODE>edit</CODE>, this rule will notrun at all if we give the command <SAMP>`make'</SAMP> with no arguments.  Inorder to make the rule run, we have to type <SAMP>`make clean'</SAMP>.See section <A HREF="make.html#SEC78">How to Run <CODE>make</CODE></A>.</P><H1><A NAME="SEC12" HREF="make_toc.html#TOC12">Writing Makefiles</A></H1><P><A NAME="IDX48"></A>The information that tells <CODE>make</CODE> how to recompile a system comes fromreading a data base called the <EM>makefile</EM>.</P><H2><A NAME="SEC13" HREF="make_toc.html#TOC13">What Makefiles Contain</A></H2><P>Makefiles contain five kinds of things: <EM>explicit rules</EM>,<EM>implicit rules</EM>, <EM>variable definitions</EM>, <EM>directives</EM>,and <EM>comments</EM>.  Rules, variables, and directives are described atlength in later chapters.</P><UL><LI><A NAME="IDX49"></A> <A NAME="IDX50"></A> An <EM>explicit rule</EM> says when and how to remake one or more files,called the rule's targets.  It lists the other files that the targets<EM>depend on</EM>, and may also give commands to use to create or updatethe targets.  See section <A HREF="make.html#SEC19">Writing Rules</A>.<A NAME="IDX51"></A><A NAME="IDX52"></A><LI>An <EM>implicit rule</EM> says when and how to remake a class of filesbased on their names.  It describes how a target may depend on a filewith a name similar to the target and gives commands to create orupdate such a target.  See section <A HREF="make.html#SEC86">Using Implicit Rules</A>.<A NAME="IDX53"></A><LI>A <EM>variable definition</EM> is a line that specifies a text stringvalue for a variable that can be substituted into the text later.  Thesimple makefile example shows a variable definition for <CODE>objects</CODE>as a list of all object files (see section <A HREF="make.html#SEC8">Variables Make Makefiles Simpler</A>).<A NAME="IDX54"></A><LI>A <EM>directive</EM> is a command for <CODE>make</CODE> to do something special whilereading the makefile.  These include:<UL><LI>Reading another makefile (see section <A HREF="make.html#SEC15">Including Other Makefiles</A>).<LI>Deciding (based on the values of variables) whether to use orignore a part of the makefile (see section <A HREF="make.html#SEC67">Conditional Parts of Makefiles</A>).<LI>Defining a variable from a verbatim string containing multiple lines(see section <A HREF="make.html#SEC65">Defining Variables Verbatim</A>).

⌨️ 快捷键说明

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