/usr/share/doc/make-doc/make.html/Introduction.html is in make-doc 4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This file documents the GNU make utility, which determines
automatically which pieces of a large program need to be recompiled,
and issues the commands to recompile them.
This is Edition 0.73, last updated 15 January 2016,
of The GNU Make Manual, for GNU make version 4.1.
Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995,
1996, 1997, 1998, 1999, 2000, 2002, 2003, 2004, 2005, 2006, 2007,
2008, 2009, 2010, 2011, 2012, 2013, 2014 Free Software Foundation, Inc.
Permission is granted to copy, distribute and/or modify this document
under the terms of the GNU Free Documentation License, Version 1.3 or
any later version published by the Free Software Foundation; with no
Invariant Sections, with the Front-Cover Texts being "A GNU Manual,"
and with the Back-Cover Texts as in (a) below. A copy of the
license is included in the section entitled "GNU Free Documentation
License."
(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
modify this GNU manual. Buying copies from the FSF supports it in
developing GNU and promoting software freedom." -->
<!-- Created by GNU Texinfo 6.0, http://www.gnu.org/software/texinfo/ -->
<head>
<title>GNU make: Introduction</title>
<meta name="description" content="GNU make: Introduction">
<meta name="keywords" content="GNU make: Introduction">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="makeinfo">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link href="index.html#Top" rel="start" title="Top">
<link href="Concept-Index.html#Concept-Index" rel="index" title="Concept Index">
<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
<link href="index.html#Top" rel="up" title="Top">
<link href="Makefiles.html#Makefiles" rel="next" title="Makefiles">
<link href="Overview.html#Bugs" rel="prev" title="Bugs">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nocodebreak {white-space: nowrap}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: serif; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
-->
</style>
</head>
<body lang="en">
<a name="Introduction"></a>
<div class="header">
<p>
Next: <a href="Makefiles.html#Makefiles" accesskey="n" rel="next">Makefiles</a>, Previous: <a href="Overview.html#Overview" accesskey="p" rel="prev">Overview</a>, Up: <a href="index.html#Top" accesskey="u" rel="up">Top</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="An-Introduction-to-Makefiles"></a>
<h2 class="chapter">2 An Introduction to Makefiles</h2>
<p>You need a file called a <em>makefile</em> to tell <code>make</code> what to do.
Most often, the makefile tells <code>make</code> how to compile and link a
program.
<a name="index-makefile"></a>
</p>
<p>In this chapter, we will discuss a simple makefile that describes how to
compile and link a text editor which consists of eight C source files
and three header files. The makefile can also tell <code>make</code> how to
run miscellaneous commands when explicitly asked (for example, to remove
certain files as a clean-up operation). To see a more complex example
of a makefile, see <a href="Complex-Makefile.html#Complex-Makefile">Complex Makefile</a>.
</p>
<p>When <code>make</code> recompiles the editor, each changed C source file
must be recompiled. If a header file has changed, each C source file
that includes the header file must be recompiled to be safe. Each
compilation 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 linked
together to produce the new executable editor.
<a name="index-recompilation"></a>
<a name="index-editor"></a>
</p>
<table class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">• <a href="#Rule-Introduction" accesskey="1">Rule Introduction</a>:</td><td> </td><td align="left" valign="top">What a rule looks like.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Simple-Makefile" accesskey="2">Simple Makefile</a>:</td><td> </td><td align="left" valign="top">A simple makefile.
</td></tr>
<tr><td align="left" valign="top">• <a href="#How-Make-Works" accesskey="3">How Make Works</a>:</td><td> </td><td align="left" valign="top">How <code>make</code> processes this makefile.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Variables-Simplify" accesskey="4">Variables Simplify</a>:</td><td> </td><td align="left" valign="top">Variables make makefiles simpler.
</td></tr>
<tr><td align="left" valign="top">• <a href="#make-Deduces" accesskey="5">make Deduces</a>:</td><td> </td><td align="left" valign="top">Letting <code>make</code> deduce the recipes.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Combine-By-Prerequisite" accesskey="6">Combine By Prerequisite</a>:</td><td> </td><td align="left" valign="top">Another style of makefile.
</td></tr>
<tr><td align="left" valign="top">• <a href="#Cleanup" accesskey="7">Cleanup</a>:</td><td> </td><td align="left" valign="top">Rules for cleaning the directory.
</td></tr>
</table>
<hr>
<a name="Rule-Introduction"></a>
<div class="header">
<p>
Next: <a href="#Simple-Makefile" accesskey="n" rel="next">Simple Makefile</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="What-a-Rule-Looks-Like"></a>
<h3 class="section">2.1 What a Rule Looks Like</h3>
<a name="index-rule_002c-introduction-to"></a>
<a name="index-makefile-rule-parts"></a>
<a name="index-parts-of-makefile-rule"></a>
<p>A simple makefile consists of “rules” with the following shape:
</p>
<a name="index-targets_002c-introduction-to"></a>
<a name="index-prerequisites_002c-introduction-to"></a>
<a name="index-recipes_002c-introduction-to"></a>
<div class="example">
<pre class="example"><var>target</var> … : <var>prerequisites</var> …
<var>recipe</var>
…
…
</pre></div>
<p>A <em>target</em> is usually the name of a file that is generated by a
program; examples of targets are executable or object files. A target
can also be the name of an action to carry out, such as ‘<samp>clean</samp>’
(see <a href="Rules.html#Phony-Targets">Phony Targets</a>).
</p>
<p>A <em>prerequisite</em> is a file that is used as input to create the
target. A target often depends on several files.
</p>
<a name="index-tabs-in-rules"></a>
<p>A <em>recipe</em> is an action that <code>make</code> carries out. A recipe
may have more than one command, either on the same line or each on its
own line. <strong>Please note:</strong> you need to put a tab character at
the beginning of every recipe line! This is an obscurity that catches
the unwary. If you prefer to prefix your recipes with a character
other than tab, you can set the <code>.RECIPEPREFIX</code> variable to an
alternate character (see <a href="Using-Variables.html#Special-Variables">Special Variables</a>).
</p>
<p>Usually a recipe is in a rule with prerequisites and serves to create a
target file if any of the prerequisites change. However, the rule that
specifies a recipe for the target need not have prerequisites. For
example, the rule containing the delete command associated with the
target ‘<samp>clean</samp>’ does not have prerequisites.
</p>
<p>A <em>rule</em>, then, explains how and when to remake certain files
which are the targets of the particular rule. <code>make</code> carries out
the recipe on the prerequisites to create or update the target. A
rule can also explain how and when to carry out an action.
See <a href="Rules.html#Rules">Writing Rules</a>.
</p>
<p>A makefile may contain other text besides rules, but a simple makefile
need only contain rules. Rules may look somewhat more complicated
than shown in this template, but all fit the pattern more or less.
</p>
<hr>
<a name="Simple-Makefile"></a>
<div class="header">
<p>
Next: <a href="#How-Make-Works" accesskey="n" rel="next">How Make Works</a>, Previous: <a href="#Rule-Introduction" accesskey="p" rel="prev">Rule Introduction</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="A-Simple-Makefile"></a>
<h3 class="section">2.2 A Simple Makefile</h3>
<a name="index-simple-makefile"></a>
<a name="index-makefile_002c-simple"></a>
<p>Here is a straightforward makefile that describes the way an
executable file called <code>edit</code> depends on eight object files
which, in turn, depend on eight C source and three header files.
</p>
<p>In this example, all the C files include <samp>defs.h</samp>, but only those
defining editing commands include <samp>command.h</samp>, and only low
level files that change the editor buffer include <samp>buffer.h</samp>.
</p>
<div class="example">
<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></div>
<p>We split each long line into two lines using backslash/newline; this is
like using one long line, but is easier to read. See <a href="Makefiles.html#Splitting-Lines">Splitting Long Lines</a>.
<a name="index-continuation-lines"></a>
<a name="index-_005c-_0028backslash_0029_002c-for-continuation-lines"></a>
<a name="index-backslash-_0028_005c_0029_002c-for-continuation-lines"></a>
<a name="index-quoting-newline_002c-in-makefile"></a>
<a name="index-newline_002c-quoting_002c-in-makefile"></a>
</p>
<p>To use this makefile to create the executable file called <samp>edit</samp>,
type:
</p>
<div class="example">
<pre class="example">make
</pre></div>
<p>To use this makefile to delete the executable file and all the object
files from the directory, type:
</p>
<div class="example">
<pre class="example">make clean
</pre></div>
<p>In the example makefile, the targets include the executable file
‘<samp>edit</samp>’, and the object files ‘<samp>main.o</samp>’ and ‘<samp>kbd.o</samp>’. The
prerequisites are files such as ‘<samp>main.c</samp>’ and ‘<samp>defs.h</samp>’.
In fact, each ‘<samp>.o</samp>’ file is both a target and a prerequisite.
Recipes include ‘<samp>cc <span class="nolinebreak">-c</span> main.c</samp>’<!-- /@w --> and ‘<samp>cc <span class="nolinebreak">-c</span> kbd.c</samp>’<!-- /@w -->.
</p>
<p>When a target is a file, it needs to be recompiled or relinked if any
of its prerequisites change. In addition, any prerequisites that are
themselves automatically generated should be updated first. In this
example, <samp>edit</samp> depends on each of the eight object files; the
object file <samp>main.o</samp> depends on the source file <samp>main.c</samp> and
on the header file <samp>defs.h</samp>.
</p>
<p>A recipe may follow each line that contains a target and
prerequisites. These recipes say how to update the target file. A
tab character (or whatever character is specified by the
<code>.RECIPEPREFIX</code> variable; see <a href="Using-Variables.html#Special-Variables">Special Variables</a>) must come at
the beginning of every line in the recipe to distinguish recipes from
other lines in the makefile. (Bear in mind that <code>make</code> does not
know anything about how the recipes work. It is up to you to supply
recipes that will update the target file properly. All <code>make</code>
does is execute the recipe you have specified when the target file
needs to be updated.)
<a name="index-recipe"></a>
</p>
<p>The target ‘<samp>clean</samp>’ is not a file, but merely the name of an
action. Since you normally do not want to carry out the actions in
this rule, ‘<samp>clean</samp>’ is not a prerequisite of any other rule.
Consequently, <code>make</code> never does anything with it unless you tell
it specifically. Note that this rule not only is not a prerequisite,
it also does not have any prerequisites, so the only purpose of the
rule is to run the specified recipe. Targets that do not refer to
files but are just actions are called <em>phony targets</em>.
See <a href="Rules.html#Phony-Targets">Phony Targets</a>, for information about this kind of target.
See <a href="Recipes.html#Errors">Errors in Recipes</a>, to see how to cause <code>make</code>
to ignore errors from <code>rm</code> or any other command.
<a name="index-clean-target"></a>
<a name="index-rm-_0028shell-command_0029"></a>
</p>
<hr>
<a name="How-Make-Works"></a>
<div class="header">
<p>
Next: <a href="#Variables-Simplify" accesskey="n" rel="next">Variables Simplify</a>, Previous: <a href="#Simple-Makefile" accesskey="p" rel="prev">Simple Makefile</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="How-make-Processes-a-Makefile"></a>
<h3 class="section">2.3 How <code>make</code> Processes a Makefile</h3>
<a name="index-processing-a-makefile"></a>
<a name="index-makefile_002c-how-make-processes"></a>
<p>By default, <code>make</code> starts with the first target (not targets whose
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. You can override this behavior using the command line
(see <a href="Running.html#Goals">Arguments to Specify the Goals</a>) or with the
<code>.DEFAULT_GOAL</code> special variable (see <a href="Using-Variables.html#Special-Variables">Other Special Variables</a>).
<a name="index-default-goal"></a>
<a name="index-goal_002c-default"></a>
<a name="index-goal"></a>
</p>
<p>In the simple example of the previous section, the default goal is to
update the executable program <samp>edit</samp>; therefore, we put that rule
first.
</p>
<p>Thus, when you give the command:
</p>
<div class="example">
<pre class="example">make
</pre></div>
<p><code>make</code> reads the makefile in the current directory and begins by
processing the first rule. In the example, this rule is for relinking
<samp>edit</samp>; but before <code>make</code> can fully process this rule, it
must process the rules for the files that <samp>edit</samp> depends on,
which in this case are the object files. Each of these files is
processed according to its own rule. These rules say to update each
‘<samp>.o</samp>’ file by compiling its source file. The recompilation must
be done if the source file, or any of the header files named as
prerequisites, is more recent than the object file, or if the object
file does not exist.
</p>
<p>The other rules are processed because their targets appear as
prerequisites of the goal. If some other rule is not depended on by the
goal (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>
<p>Before recompiling an object file, <code>make</code> considers updating its
prerequisites, the source file and header files. This makefile does not
specify anything to be done for them—the ‘<samp>.c</samp>’ and ‘<samp>.h</samp>’ files
are not the targets of any rules—so <code>make</code> does nothing for these
files. 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> decides
whether to relink <samp>edit</samp>. This must be done if the file
<samp>edit</samp> does not exist, or if any of the object files are newer than
it. If an object file was just recompiled, it is now newer than
<samp>edit</samp>, so <samp>edit</samp> is relinked.
<a name="index-relinking"></a>
</p>
<p>Thus, if we change the file <samp>insert.c</samp> and run <code>make</code>,
<code>make</code> will compile that file to update <samp>insert.o</samp>, and then
link <samp>edit</samp>. If we change the file <samp>command.h</samp> and run
<code>make</code>, <code>make</code> will recompile the object files <samp>kbd.o</samp>,
<samp>command.o</samp> and <samp>files.o</samp> and then link the file <samp>edit</samp>.
</p>
<hr>
<a name="Variables-Simplify"></a>
<div class="header">
<p>
Next: <a href="#make-Deduces" accesskey="n" rel="next">make Deduces</a>, Previous: <a href="#How-Make-Works" accesskey="p" rel="prev">How Make Works</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Variables-Make-Makefiles-Simpler"></a>
<h3 class="section">2.4 Variables Make Makefiles Simpler</h3>
<a name="index-variables"></a>
<a name="index-simplifying-with-variables"></a>
<p>In our example, we had to list all the object files twice in the rule for
<samp>edit</samp> (repeated here):
</p>
<div class="example">
<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
</pre></div>
<a name="index-objects"></a>
<p>Such duplication is error-prone; if a new object file is added to the
system, we might add it to one list and forget the other. We can eliminate
the risk and simplify the makefile by using a variable. <em>Variables</em>
allow a text string to be defined once and substituted in multiple places
later (see <a href="Using-Variables.html#Using-Variables">How to Use Variables</a>).
</p>
<a name="index-OBJECTS"></a>
<a name="index-objs"></a>
<a name="index-OBJS"></a>
<a name="index-obj"></a>
<a name="index-OBJ"></a>
<p>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 would
define such a variable <code>objects</code> with a line like this in the
makefile:
</p>
<div class="example">
<pre class="example">objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
</pre></div>
<p>Then, each place we want to put a list of the object file names, we can
substitute the variable’s value by writing ‘<samp>$(objects)</samp>’
(see <a href="Using-Variables.html#Using-Variables">How to Use Variables</a>).
</p>
<p>Here is how the complete simple makefile looks when you use a variable
for the object files:
</p>
<div class="example">
<pre class="example">objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
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 $(objects)
</pre></div>
<hr>
<a name="make-Deduces"></a>
<div class="header">
<p>
Next: <a href="#Combine-By-Prerequisite" accesskey="n" rel="next">Combine By Prerequisite</a>, Previous: <a href="#Variables-Simplify" accesskey="p" rel="prev">Variables Simplify</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Letting-make-Deduce-the-Recipes"></a>
<h3 class="section">2.5 Letting <code>make</code> Deduce the Recipes</h3>
<a name="index-deducing-recipes-_0028implicit-rules_0029"></a>
<a name="index-implicit-rule_002c-introduction-to"></a>
<a name="index-rule_002c-implicit_002c-introduction-to"></a>
<p>It is not necessary to spell out the recipes for compiling the individual
C 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 correspondingly
named ‘<samp>.c</samp>’ file using a ‘<samp>cc -c</samp>’ command. For example, it will
use the recipe ‘<samp>cc -c main.c -o main.o</samp>’ to compile <samp>main.c</samp> into
<samp>main.o</samp>. We can therefore omit the recipes from the rules for the
object files. See <a href="Implicit-Rules.html#Implicit-Rules">Using Implicit Rules</a>.
</p>
<p>When a ‘<samp>.c</samp>’ file is used automatically in this way, it is also
automatically added to the list of prerequisites. We can therefore omit
the ‘<samp>.c</samp>’ files from the prerequisites, provided we omit the recipe.
</p>
<p>Here is the entire example, with both of these changes, and a variable
<code>objects</code> as suggested above:
</p>
<div class="example">
<pre class="example">objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
main.o : defs.h
kbd.o : defs.h command.h
command.o : defs.h command.h
display.o : defs.h buffer.h
insert.o : defs.h buffer.h
search.o : defs.h buffer.h
files.o : defs.h buffer.h command.h
utils.o : defs.h
.PHONY : clean
clean :
rm edit $(objects)
</pre></div>
<p>This is how we would write the makefile in actual practice. (The
complications associated with ‘<samp>clean</samp>’ are described elsewhere.
See <a href="Rules.html#Phony-Targets">Phony Targets</a>, and <a href="Recipes.html#Errors">Errors in Recipes</a>.)
</p>
<p>Because implicit rules are so convenient, they are important. You
will see them used frequently.
</p>
<hr>
<a name="Combine-By-Prerequisite"></a>
<div class="header">
<p>
Next: <a href="#Cleanup" accesskey="n" rel="next">Cleanup</a>, Previous: <a href="#make-Deduces" accesskey="p" rel="prev">make Deduces</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Another-Style-of-Makefile"></a>
<h3 class="section">2.6 Another Style of Makefile</h3>
<a name="index-combining-rules-by-prerequisite"></a>
<p>When the objects of a makefile are created only by implicit rules, an
alternative style of makefile is possible. In this style of makefile,
you group entries by their prerequisites instead of by their targets.
Here is what one looks like:
</p>
<div class="example">
<pre class="example">objects = main.o kbd.o command.o display.o \
insert.o search.o files.o utils.o
edit : $(objects)
cc -o edit $(objects)
$(objects) : defs.h
kbd.o command.o files.o : command.h
display.o insert.o search.o files.o : buffer.h
</pre></div>
<p>Here <samp>defs.h</samp> is given as a prerequisite of all the object files;
<samp>command.h</samp> and <samp>buffer.h</samp> are prerequisites of the specific
object files listed for them.
</p>
<p>Whether this is better is a matter of taste: it is more compact, but some
people dislike it because they find it clearer to put all the information
about each target in one place.
</p>
<hr>
<a name="Cleanup"></a>
<div class="header">
<p>
Previous: <a href="#Combine-By-Prerequisite" accesskey="p" rel="prev">Combine By Prerequisite</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Rules-for-Cleaning-the-Directory"></a>
<h3 class="section">2.7 Rules for Cleaning the Directory</h3>
<a name="index-cleaning-up"></a>
<a name="index-removing_002c-to-clean-up"></a>
<p>Compiling a program is not the only thing you might want to write rules
for. Makefiles commonly tell how to do a few other things besides
compiling a program: for example, how to delete all the object files
and executables so that the directory is ‘<samp>clean</samp>’.
</p>
<a name="index-clean-target-1"></a>
<p>Here is how we
could write a <code>make</code> rule for cleaning our example editor:
</p>
<div class="example">
<pre class="example">clean:
rm edit $(objects)
</pre></div>
<p>In practice, we might want to write the rule in a somewhat more
complicated manner to handle unanticipated situations. We would do this:
</p>
<div class="example">
<pre class="example">.PHONY : clean
clean :
-rm edit $(objects)
</pre></div>
<p>This prevents <code>make</code> from getting confused by an actual file
called <samp>clean</samp> and causes it to continue in spite of errors from
<code>rm</code>. (See <a href="Rules.html#Phony-Targets">Phony Targets</a>, and <a href="Recipes.html#Errors">Errors in
Recipes</a>.)
</p>
<p>A rule such as this should not be placed at the beginning of the
makefile, because we do not want it to run by default! Thus, in the
example makefile, we want the rule for <code>edit</code>, which recompiles
the editor, to remain the default goal.
</p>
<p>Since <code>clean</code> is not a prerequisite of <code>edit</code>, this rule will not
run at all if we give the command ‘<samp>make</samp>’ with no arguments. In
order to make the rule run, we have to type ‘<samp>make clean</samp>’.
See <a href="Running.html#Running">How to Run <code>make</code></a>.
</p>
<hr>
<div class="header">
<p>
Previous: <a href="#Combine-By-Prerequisite" accesskey="p" rel="prev">Combine By Prerequisite</a>, Up: <a href="#Introduction" accesskey="u" rel="up">Introduction</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Concept-Index.html#Concept-Index" title="Index" rel="index">Index</a>]</p>
</div>
</body>
</html>
|