📄 make.html
字号:
<pre class="example"> bug-make@gnu.org</pre> <p class="noindent">or use our Web-based project management tool, at:<pre class="example"> http://savannah.gnu.org/projects/make/</pre> <p class="noindent">In addition to the information above, please be careful to include theversion number of <code>make</code> you are using. You can get thisinformation with the command `<samp><span class="samp">make --version</span></samp>'. Be sure also toinclude the type of machine and operating system you are using. Oneway to obtain this information is by looking at the final lines ofoutput from the command `<samp><span class="samp">make --help</span></samp>'.<div class="node"><p><hr><a name="Introduction"></a>Next: <a rel="next" accesskey="n" href="#Makefiles">Makefiles</a>,Previous: <a rel="previous" accesskey="p" href="#Overview">Overview</a>,Up: <a rel="up" accesskey="u" href="#Top">Top</a></div><!-- node-name, next, previous, up --><h2 class="chapter">2 An Introduction to Makefiles</h2><p>You need a file called a <dfn>makefile</dfn> to tell <code>make</code> what to do. Most often, the makefile tells <code>make</code> how to compile and link aprogram. <a name="index-makefile-7"></a>In this chapter, we will discuss a simple makefile that describes how tocompile and link a text editor which consists of eight C source filesand three header files. The makefile can also tell <code>make</code> how torun miscellaneous commands when explicitly asked (for example, to removecertain files as a clean-up operation). To see a more complex exampleof a makefile, see <a href="#Complex-Makefile">Complex Makefile</a>. <p>When <code>make</code> recompiles the editor, each changed C source filemust be recompiled. If a header file has changed, each C source filethat includes the header file must be recompiled to be safe. Eachcompilation produces an object file corresponding to the source file. Finally, if any source file has been recompiled, all the object files,whether newly made or saved from previous compilations, must be linkedtogether to produce the new executable editor. <a name="index-recompilation-8"></a><a name="index-editor-9"></a><ul class="menu"><li><a accesskey="1" href="#Rule-Introduction">Rule Introduction</a>: What a rule looks like. <li><a accesskey="2" href="#Simple-Makefile">Simple Makefile</a>: A Simple Makefile<li><a accesskey="3" href="#How-Make-Works">How Make Works</a>: How <code>make</code> Processes This Makefile<li><a accesskey="4" href="#Variables-Simplify">Variables Simplify</a>: Variables Make Makefiles Simpler<li><a accesskey="5" href="#make-Deduces">make Deduces</a>: Letting <code>make</code> Deduce the Commands<li><a accesskey="6" href="#Combine-By-Prerequisite">Combine By Prerequisite</a>: Another Style of Makefile<li><a accesskey="7" href="#Cleanup">Cleanup</a>: Rules for Cleaning the Directory</ul><div class="node"><p><hr><a name="Rule-Introduction"></a>Next: <a rel="next" accesskey="n" href="#Simple-Makefile">Simple Makefile</a>,Previous: <a rel="previous" accesskey="p" href="#Introduction">Introduction</a>,Up: <a rel="up" accesskey="u" href="#Introduction">Introduction</a></div><!-- node-name, next, previous, up --><h3 class="section">2.1 What a Rule Looks Like</h3><p><a name="index-rule_002c-introduction-to-10"></a><a name="index-makefile-rule-parts-11"></a><a name="index-parts-of-makefile-rule-12"></a>A simple makefile consists of “rules” with the following shape: <p><a name="index-targets_002c-introduction-to-13"></a><a name="index-prerequisites_002c-introduction-to-14"></a><a name="index-commands_002c-introduction-to-15"></a><pre class="example"> <var>target</var> ... : <var>prerequisites</var> ... <var>command</var> ... ...</pre> <p>A <dfn>target</dfn> is usually the name of a file that is generated by aprogram; examples of targets are executable or object files. A targetcan also be the name of an action to carry out, such as `<samp><span class="samp">clean</span></samp>'(see <a href="#Phony-Targets">Phony Targets</a>). <p>A <dfn>prerequisite</dfn> is a file that is used as input to create thetarget. A target often depends on several files. <p><a name="index-tabs-in-rules-16"></a>A <dfn>command</dfn> is an action that <code>make</code> carries out. A rule may have more than one command, each on its own line. <strong>Please note:</strong> you need to put a tab character at the beginning ofevery command line! This is an obscurity that catches the unwary. <p>Usually a command is in a rule with prerequisites and serves to create atarget file if any of the prerequisites change. However, the rule thatspecifies commands for the target need not have prerequisites. Forexample, the rule containing the delete command associated with thetarget `<samp><span class="samp">clean</span></samp>' does not have prerequisites. <p>A <dfn>rule</dfn>, then, explains how and when to remake certain fileswhich are the targets of the particular rule. <code>make</code> carries outthe commands on the prerequisites to create or update the target. Arule can also explain how and when to carry out an action. See <a href="#Rules">Writing Rules</a>. <p>A makefile may contain other text besides rules, but a simple makefileneed only contain rules. Rules may look somewhat more complicatedthan shown in this template, but all fit the pattern more or less.<div class="node"><p><hr><a name="Simple-Makefile"></a>Next: <a rel="next" accesskey="n" href="#How-Make-Works">How Make Works</a>,Previous: <a rel="previous" accesskey="p" href="#Rule-Introduction">Rule Introduction</a>,Up: <a rel="up" accesskey="u" href="#Introduction">Introduction</a></div><h3 class="section">2.2 A Simple Makefile</h3><p><a name="index-simple-makefile-17"></a><a name="index-makefile_002c-simple-18"></a>Here is a straightforward makefile that describes the way anexecutable file called <code>edit</code> depends on eight object fileswhich, in turn, depend on eight C source and three header files. <p>In this example, all the C files include <samp><span class="file">defs.h</span></samp>, but only thosedefining editing commands include <samp><span class="file">command.h</span></samp>, and only lowlevel files that change the editor buffer include <samp><span class="file">buffer.h</span></samp>.<pre class="example"> 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 main.o : main.c defs.h cc -c main.c kbd.o : kbd.c defs.h command.h cc -c kbd.c command.o : command.c defs.h command.h cc -c command.c display.o : display.c defs.h buffer.h cc -c display.c insert.o : insert.c defs.h buffer.h cc -c insert.c search.o : search.c defs.h buffer.h cc -c search.c files.o : files.c defs.h buffer.h command.h cc -c files.c utils.o : utils.c defs.h cc -c utils.c clean : rm edit main.o kbd.o command.o display.o \ insert.o search.o files.o utils.o</pre> <p class="noindent">We split each long line into two lines using backslash-newline; this islike using one long line, but is easier to read. <a name="index-continuation-lines-19"></a><a name="index-g_t_0040code_007b_005c_007d-_0028backslash_0029_002c-for-continuation-lines-20"></a><a name="index-backslash-_0028_0040code_007b_005c_007d_0029_002c-for-continuation-lines-21"></a><a name="index-quoting-newline_002c-in-makefile-22"></a><a name="index-newline_002c-quoting_002c-in-makefile-23"></a>To use this makefile to create the executable file called <samp><span class="file">edit</span></samp>,type:<pre class="example"> make</pre> <p>To use this makefile to delete the executable file and all the objectfiles from the directory, type:<pre class="example"> make clean</pre> <p>In the example makefile, the targets include the executable file`<samp><span class="samp">edit</span></samp>', and the object files `<samp><span class="samp">main.o</span></samp>' and `<samp><span class="samp">kbd.o</span></samp>'. Theprerequisites are files such as `<samp><span class="samp">main.c</span></samp>' and `<samp><span class="samp">defs.h</span></samp>'. In fact, each `<samp><span class="samp">.o</span></samp>' file is both a target and a prerequisite. Commands include `<samp><span class="samp">cc -c main.c</span></samp>'<!-- /@w --> and `<samp><span class="samp">cc -c kbd.c</span></samp>'<!-- /@w -->. <p>When a target is a file, it needs to be recompiled or relinked if anyof its prerequisites change. In addition, any prerequisites that arethemselves automatically generated should be updated first. In thisexample, <samp><span class="file">edit</span></samp> depends on each of the eight object files; theobject file <samp><span class="file">main.o</span></samp> depends on the source file <samp><span class="file">main.c</span></samp> andon the header file <samp><span class="file">defs.h</span></samp>. <p>A shell command follows each line that contains a target andprerequisites. These shell commands say how to update the target file. A tab character must come at the beginning of every command line todistinguish command lines from other lines in the makefile. (Bear inmind that <code>make</code> does not know anything about how the commandswork. It is up to you to supply commands that will update the targetfile properly. All <code>make</code> does is execute the commands in the ruleyou have specified when the target file needs to be updated.) <a name="index-shell-command-24"></a>The target `<samp><span class="samp">clean</span></samp>' is not a file, but merely the name of anaction. Since younormallydo not want to carry out the actions in this rule, `<samp><span class="samp">clean</span></samp>' is not a prerequisite of any other rule. Consequently, <code>make</code> never does anything with it unless you tellit specifically. Note that this rule not only is not a prerequisite, italso does not have any prerequisites, so the only purpose of the ruleis to run the specified commands. Targets that do not refer to filesbut are just actions are called <dfn>phony targets</dfn>. See <a href="#Phony-Targets">Phony Targets</a>, for information about this kind of target. See <a href="#Errors">Errors in Commands</a>, to see how to cause <code>make</code> to ignore errorsfrom <code>rm</code> or any other command. <a name="index-g_t_0040code_007bclean_007d-target-25"></a><a name="index-g_t_0040code_007brm_007d-_0028shell-command_0029-26"></a><div class="node"><p><hr><a name="How-Make-Works"></a>Next: <a rel="next" accesskey="n" href="#Variables-Simplify">Variables Simplify</a>,Previous: <a rel="previous" accesskey="p" href="#Simple-Makefile">Simple Makefile</a>,Up: <a rel="up" accesskey="u" href="#Introduction">Introduction</a></div><!-- node-name, next, previous, up --><h3 class="section">2.3 How <code>make</code> Processes a Makefile</h3><p><a name="index-processing-a-makefile-27"></a><a name="index-makefile_002c-how-_0040code_007bmake_007d-processes-28"></a>By default, <code>make</code> starts with the first target (not targets whosenames start with `<samp><span class="samp">.</span></samp>'). This is called the <dfn>default goal</dfn>. (<dfn>Goals</dfn> are the targets that <code>make</code> strives ultimately toupdate. You can override this behavior using the command line(see <a href="#Goals">Arguments to Specify the Goals</a>) or with the<code>.DEFAULT_GOAL</code> special variable (see <a href="#Special-Variables">Other Special Variables</a>). <a name="index-default-goal-29"></a><a name="index-goal_002c-default-30"></a><a name="index-goal-31"></a>In the simple example of the previous section, the default goal is toupdate the executable program <samp><span class="file">edit</span></samp>; therefore, we put that rulefirst. <p>Thus, when you give the command:<pre class="example"> make</pre> <p class="noindent"><code>make</code> reads the makefile in the current directory and begins byprocessing the first rule. In the example, this rule is for relinking<samp><span class="file">edit</span></samp>; but before <code>make</code> can fully process this rule, itmust process the rules for the files that <samp><span class="file">edit</span></samp> 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><span class="samp">.o</span></samp>' file by compiling its source file. The recompilation mustbe done if the source file, or any of the header files named asprerequisites, is more recent than the object file, or if the objectfile does not exist. <p>The other rules are processed because their targets appear asprerequisites 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><!-- /@w -->). <p>Before recompiling an object file, <code>make</code> considers updating its
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -