📄 make.html
字号:
</UL><A NAME="IDX55"></A><A NAME="IDX56"></A><LI><SAMP>`#'</SAMP> in a line of a makefile starts a <EM>comment</EM>. It and the rest ofthe line are ignored, except that a trailing backslash not escaped byanother backslash will continue the comment across multiple lines.Comments may appear on any of the lines in the makefile, except within a<CODE>define</CODE> directive, and perhaps within commands (where the shelldecides what is a comment). A line containing just a comment (withperhaps spaces before it) is effectively blank, and is ignored.</UL><H2><A NAME="SEC14" HREF="make_toc.html#TOC14">What Name to Give Your Makefile</A></H2><P><A NAME="IDX57"></A><A NAME="IDX58"></A><A NAME="IDX59"></A><A NAME="IDX60"></A></P><P>By default, when <CODE>make</CODE> looks for the makefile, it tries thefollowing names, in order: <TT>`GNUmakefile'</TT>, <TT>`makefile'</TT>and <TT>`Makefile'</TT>.<A NAME="IDX61"></A><A NAME="IDX62"></A><A NAME="IDX63"></A></P><P><A NAME="IDX64"></A>In most cases you should call your makefile either <TT>`makefile'</TT> or<TT>`Makefile'</TT>. (We recommend <TT>`Makefile'</TT> because it appearsprominently near the beginning of a directory listing, right near otherimportant files such as <TT>`README'</TT>.) The first name checked,<TT>`GNUmakefile'</TT>, is not recommended for most makefiles. You shoulduse this name if you have a makefile that is specific to GNU<CODE>make</CODE>, and will not be understood by other versions of<CODE>make</CODE>. Other <CODE>make</CODE> programs look for <TT>`makefile'</TT> and<TT>`Makefile'</TT>, but not <TT>`GNUmakefile'</TT>.</P><P>If <CODE>make</CODE> finds none of these names, it does not use any makefile.Then you must specify a goal with a command argument, and <CODE>make</CODE>will attempt to figure out how to remake it using only its built-inimplicit rules. See section <A HREF="make.html#SEC86">Using Implicit Rules</A>.</P><P><A NAME="IDX65"></A><A NAME="IDX66"></A><A NAME="IDX67"></A>If you want to use a nonstandard name for your makefile, you can specifythe makefile name with the <SAMP>`-f'</SAMP> or <SAMP>`--file'</SAMP> option. Thearguments <SAMP>`-f <VAR>name</VAR>'</SAMP> or <SAMP>`--file=<VAR>name</VAR>'</SAMP> tell<CODE>make</CODE> to read the file <VAR>name</VAR> as the makefile. If you usemore than one <SAMP>`-f'</SAMP> or <SAMP>`--file'</SAMP> option, you can specify severalmakefiles. All the makefiles are effectively concatenated in the orderspecified. The default makefile names <TT>`GNUmakefile'</TT>,<TT>`makefile'</TT> and <TT>`Makefile'</TT> are not checked automatically if youspecify <SAMP>`-f'</SAMP> or <SAMP>`--file'</SAMP>.<A NAME="IDX68"></A><A NAME="IDX69"></A><A NAME="IDX70"></A><A NAME="IDX71"></A></P><H2><A NAME="SEC15" HREF="make_toc.html#TOC15">Including Other Makefiles</A></H2><P><A NAME="IDX72"></A><A NAME="IDX73"></A></P><P><A NAME="IDX74"></A>The <CODE>include</CODE> directive tells <CODE>make</CODE> to suspend reading thecurrent makefile and read one or more other makefiles before continuing.The directive is a line in the makefile that looks like this:</P><PRE>include <VAR>filenames</VAR>...</PRE><P><VAR>filenames</VAR> can contain shell file name patterns.<A NAME="IDX75"></A><A NAME="IDX76"></A><A NAME="IDX77"></A></P><P>Extra spaces are allowed and ignored at the beginning of the line, buta tab is not allowed. (If the line begins with a tab, it will beconsidered a command line.) Whitespace is required between<CODE>include</CODE> and the file names, and between file names; extrawhitespace is ignored there and at the end of the directive. Acomment starting with <SAMP>`#'</SAMP> is allowed at the end of the line. Ifthe file names contain any variable or function references, they areexpanded. See section <A HREF="make.html#SEC55">How to Use Variables</A>.</P><P>For example, if you have three <TT>`.mk'</TT> files, <TT>`a.mk'</TT>, <TT>`b.mk'</TT>, and <TT>`c.mk'</TT>, and <CODE>$(bar)</CODE> expands to<CODE>bish bash</CODE>, then the following expression</P><PRE>include foo *.mk $(bar)</PRE><P>is equivalent to</P><PRE>include foo a.mk b.mk c.mk bish bash</PRE><P>When <CODE>make</CODE> processes an <CODE>include</CODE> directive, it suspendsreading of the containing makefile and reads from each listed file inturn. When that is finished, <CODE>make</CODE> resumes reading themakefile in which the directive appears.</P><P>One occasion for using <CODE>include</CODE> directives is when several programs,handled by individual makefiles in various directories, need to use acommon set of variable definitions (see section <A HREF="make.html#SEC62">Setting Variables</A>) or pattern rules(see section <A HREF="make.html#SEC91">Defining and Redefining Pattern Rules</A>).</P><P>Another such occasion is when you want to generate dependencies fromsource files automatically; the dependencies can be put in a file thatis included by the main makefile. This practice is generally cleanerthan that of somehow appending the dependencies to the end of the mainmakefile as has been traditionally done with other versions of<CODE>make</CODE>. See section <A HREF="make.html#SEC41">Generating Dependencies Automatically</A>.<A NAME="IDX78"></A><A NAME="IDX79"></A><A NAME="IDX80"></A></P><P><A NAME="IDX81"></A><A NAME="IDX82"></A><A NAME="IDX83"></A><A NAME="IDX84"></A><A NAME="IDX85"></A>If any of the specified filenames does not start with a slash, and the file is notfound in the current directory, several other directories are searched.First, any directories you have specified with the <SAMP>`-I'</SAMP> orwith the <SAMP>`--include-dir'</SAMP> option are searched(see section <A HREF="make.html#SEC85">Summary of Options</A>).Then the following directories (if they exist)are searched, in this order:</P><UL><LI><TT>`<VAR>prefix</VAR>/include'</TT> (normally <TT>`/usr/local/include'</TT>)<LI><TT>`/usr/gnu/include'</TT>,<LI><TT>`/usr/local/include'</TT>, <TT>`/usr/include'</TT>.</UL><P>If an included makefile cannot be found in any of these directories, awarning message is generated, but it is not an immediately fatal error;processing of the makefile containing the <CODE>include</CODE> continues.Once it has finished reading makefiles, <CODE>make</CODE> will try to remakeany that are out of date or don't exist.See section <A HREF="make.html#SEC17">How Makefiles Are Remade</A>.Only after it has tried to find a way to remake a makefile and failed,will <CODE>make</CODE> diagnose the missing makefile as a fatal error.</P><P>If you want <CODE>make</CODE> to simply ignore a makefile which does not existand cannot be remade, with no error message, use the <CODE>-include</CODE>directive instead of <CODE>include</CODE>, like this:</P><PRE>-include <VAR>filenames</VAR>...</PRE><P>This is acts like <CODE>include</CODE> in every way except that there is noerror (not even a warning) if any of the <VAR>filenames</VAR> do not exist.</P><H2><A NAME="SEC16" HREF="make_toc.html#TOC16">The Variable <CODE>MAKEFILES</CODE></A></H2><P><A NAME="IDX86"></A><A NAME="IDX87"></A></P><P><A NAME="IDX88"></A>If the environment variable <CODE>MAKEFILES</CODE> is defined, <CODE>make</CODE>considers its value as a list of names (separated by whitespace) ofadditional makefiles to be read before the others. This works much likethe <CODE>include</CODE> directive: various directories are searched for thosefiles (see section <A HREF="make.html#SEC15">Including Other Makefiles</A>). In addition, thedefault goal is never taken from one of these makefiles and it is not anerror if the files listed in <CODE>MAKEFILES</CODE> are not found.</P><P><A NAME="IDX89"></A>The main use of <CODE>MAKEFILES</CODE> is in communication between recursiveinvocations of <CODE>make</CODE> (see section <A HREF="make.html#SEC48">Recursive Use of <CODE>make</CODE></A>). It usually is not desirable to set the environmentvariable before a top-level invocation of <CODE>make</CODE>, because it isusually better not to mess with a makefile from outside. However, ifyou are running <CODE>make</CODE> without a specific makefile, a makefile in<CODE>MAKEFILES</CODE> can do useful things to help the built-in implicitrules work better, such as defining search paths (see section <A HREF="make.html#SEC25">Searching Directories for Dependencies</A>).</P><P>Some users are tempted to set <CODE>MAKEFILES</CODE> in the environmentautomatically on login, and program makefiles to expect this to be done.This is a very bad idea, because such makefiles will fail to work if run byanyone else. It is much better to write explicit <CODE>include</CODE> directivesin the makefiles. See section <A HREF="make.html#SEC15">Including Other Makefiles</A>.</P><H2><A NAME="SEC17" HREF="make_toc.html#TOC17">How Makefiles Are Remade</A></H2><P><A NAME="IDX90"></A><A NAME="IDX91"></A><A NAME="IDX92"></A>Sometimes makefiles can be remade from other files, such as RCS or SCCSfiles. If a makefile can be remade from other files, you probably want<CODE>make</CODE> to get an up-to-date version of the makefile to read in.</P><P>To this end, after reading in all makefiles, <CODE>make</CODE> will considereach as a goal target and attempt to update it. If a makefile has arule which says how to update it (found either in that very makefile orin another one) or if an implicit rule applies to it (see section <A HREF="make.html#SEC86">Using Implicit Rules</A>), it will be updated if necessary. Afterall makefiles have been checked, if any have actually been changed,<CODE>make</CODE> starts with a clean slate and reads all the makefiles overagain. (It will also attempt to update each of them over again, butnormally this will not change them again, since they are already up todate.)</P><P>If the makefiles specify a double-colon rule to remake a file withcommands but no dependencies, that file will always be remade(see section <A HREF="make.html#SEC40">Double-Colon Rules</A>). In the case of makefiles, a makefile that has adouble-colon rule with commands but no dependencies will be remade everytime <CODE>make</CODE> is run, and then again after <CODE>make</CODE> starts overand reads the makefiles in again. This would cause an infinite loop:<CODE>make</CODE> would constantly remake the makefile, and never do anythingelse. So, to avoid this, <CODE>make</CODE> will <STRONG>not</STRONG> attempt toremake makefiles which are specified as double-colon targets but have nodependencies.</P><P>If you do not specify any makefiles to be read with <SAMP>`-f'</SAMP> or<SAMP>`--file'</SAMP> options, <CODE>make</CODE> will try the default makefile names;see section <A HREF="make.html#SEC14">What Name to Give Your Makefile</A>. Unlikemakefiles explicitly requested with <SAMP>`-f'</SAMP> or <SAMP>`--file'</SAMP> options,<CODE>make</CODE> is not certain that these makefiles should exist. However,if a default makefile does not exist but can be created by running<CODE>make</CODE> rules, you probably want the rules to be run so that themakefile can be used.</P><P>Therefore, if none of the default makefiles exists, <CODE>make</CODE> will tryto make each of them in the same order in which they are searched for(see section <A HREF="make.html#SEC14">What Name to Give Your Makefile</A>)until it succeeds in making one, or it runs out of names to try. Notethat it is not an error if <CODE>make</CODE> cannot find or make any makefile;a makefile is not always necessary.</P><P>When you use the <SAMP>`-t'</SAMP> or <SAMP>`--touch'</SAMP> option(see section <A HREF="make.html#SEC81">Instead of Executing the Commands</A>),you would not want to use an out-of-date makefile to decide whichtargets to touch. So the <SAMP>`-t'</SAMP> option has no effect on updatingmakefiles; they are really updated even if <SAMP>`-t'</SAMP> is specified.Likewise, <SAMP>`-q'</SAMP> (or <SAMP>`--question'</SAMP>) and <SAMP>`-n'</SAMP> (or<SAMP>`--just-print'</SAMP>) do not prevent updating of makefiles, because anout-of-date makefile would result in the wrong output for other targets.Thus, <SAMP>`make -f mfile -n foo'</SAMP> will update <TT>`mfile'</TT>, read it in,and then print the commands to update <TT>`foo'</TT> and its dependencieswithout running them. The commands printed for <TT>`foo'</TT> will be thosespecified in the updated contents of <TT>`mfile'</TT>.</P><P>However, on occasion you might actually wish to prevent updating of eventhe makefiles. You can do this by specifying the makefiles as goals inthe command line as well as specifying them as makefiles. When themakefile name is specified explicitly as a goal, the options <SAMP>`-t'</SAMP>and so on do apply to them.</P><P>Thus, <SAMP>`make -f mfile -n mfile foo'</SAMP> would read the makefile<TT>`mfile'</TT>, print the commands needed to update it without actuallyrunning them, and then print the commands needed to update <TT>`foo'</TT>without running them. The commands for <TT>`foo'</TT> will be thosespecified by the existing contents of <TT>`mfile'</TT>.</P><H2><A NAME="SEC18" HREF="make_toc.html#TOC18">Overriding Part of Another Makefile</A></H2><P><A NAME="IDX93"></A><A NAME="IDX94"></A>Sometimes it is useful to have a makefile that is mostly just likeanother makefile. You can often use the <SAMP>`include'</SAMP> directive toinclude one in the other, and add more targets or variable definitions.However, if the two makefiles give different commands for the sametarget, <CODE>make</CODE> will not let you just do this. But there is another way.</P><P><A NAME="IDX95"></A>In the containing makefile (the one that wants to include the other),you can use a match-anything pattern rule to say that to remake anytarget that cannot be made from the information in the containingmakefile, <CODE>make</CODE> should look in another makefile.See section <A HREF="make.html#SEC91">Defining and Redefining Pattern Rules</A>, for more information on pattern rules.</P><P>For example, if you have a makefile called <TT>`Makefile'</TT> that says howto make the target <SAMP>`foo'</SAMP> (and other targets), you can write amakefile called <TT>`GNUmakefile'</TT> that contains:</P><PRE>foo: frobnicate > foo%: force @$(MAKE) -f Makefile $@force: ;</PRE><P>If you say <SAMP>`make foo'</SAMP>, <CODE>make</CODE> will find <TT>`GNUmakefile'</TT>,read it, and see that to make <TT>`foo'</TT>, it needs to run the command<SAMP>`frobnicate > foo'</SAMP>. If you say <SAMP>`make bar'</SAMP>, <CODE>make</CODE> willfind no way to make <TT>`bar'</TT> in <TT>`GNUmakefile'</TT>, so it will use thecommands from the pattern rule: <SAMP>`make -f Makefile bar'</SAMP>. If<TT>`Makefile'</TT> provides a rule for updating <TT>`bar'</TT>, <CODE>make</CODE>will apply the rule. And likewise for any other target that<TT>`GNUmakefile'</TT> does not say how to make.</P><P>The way this works is that the pattern rule has a pattern of just<SAMP>`%'</SAMP>, so it matches any target whatever. The rule specifies adependency <TT>`force'</TT>, to guarantee that the commands will be run evenif the target file already exists. We give <TT>`force'</TT> target emptycommands to prevent <CODE>make</CODE> from searching for an implicit rule tobuild it--otherwise it would apply the same match-anything rule to<TT>`force'</TT> itself and create a dependency loop!</P><H1><A NAME="SEC19" HREF="make_toc.html#TOC19">Writing Rules</A></H1><P><A NAME="IDX96"></A><A NAME="IDX97"></A><A NAME="IDX98"></A><A NAME="IDX99"></A>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -