1 This is make.info, produced by makeinfo version 4.2 from make.texi.
3 INFO-DIR-SECTION GNU Packages
5 * Make: (make). Remake files automatically.
8 This file documents the GNU Make utility, which determines
9 automatically which pieces of a large program need to be recompiled,
10 and issues the commands to recompile them.
12 This is Edition 0.60, last updated 08 July 2002, of `The GNU Make
13 Manual', for `make', Version 3.80.
15 Copyright 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996,
16 1997, 1998, 1999, 2000, 2002 Free Software Foundation, Inc.
18 Permission is granted to copy, distribute and/or modify this document
19 under the terms of the GNU Free Documentation License, Version 1.1 or
20 any later version published by the Free Software Foundation; with no
21 Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
22 Texts. A copy of the license is included in the section entitled "GNU
23 Free Documentation License".
26 File: make.info, Node: Remaking Makefiles, Next: Overriding Makefiles, Prev: Special Variables, Up: Makefiles
28 How Makefiles Are Remade
29 ========================
31 Sometimes makefiles can be remade from other files, such as RCS or
32 SCCS files. If a makefile can be remade from other files, you probably
33 want `make' to get an up-to-date version of the makefile to read in.
35 To this end, after reading in all makefiles, `make' will consider
36 each as a goal target and attempt to update it. If a makefile has a
37 rule which says how to update it (found either in that very makefile or
38 in another one) or if an implicit rule applies to it (*note Using
39 Implicit Rules: Implicit Rules.), it will be updated if necessary.
40 After all makefiles have been checked, if any have actually been
41 changed, `make' starts with a clean slate and reads all the makefiles
42 over again. (It will also attempt to update each of them over again,
43 but normally this will not change them again, since they are already up
46 If you know that one or more of your makefiles cannot be remade and
47 you want to keep `make' from performing an implicit rule search on
48 them, perhaps for efficiency reasons, you can use any normal method of
49 preventing implicit rule lookup to do so. For example, you can write an
50 explicit rule with the makefile as the target, and an empty command
51 string (*note Using Empty Commands: Empty Commands.).
53 If the makefiles specify a double-colon rule to remake a file with
54 commands but no prerequisites, that file will always be remade (*note
55 Double-Colon::). In the case of makefiles, a makefile that has a
56 double-colon rule with commands but no prerequisites will be remade
57 every time `make' is run, and then again after `make' starts over and
58 reads the makefiles in again. This would cause an infinite loop:
59 `make' would constantly remake the makefile, and never do anything
60 else. So, to avoid this, `make' will *not* attempt to remake makefiles
61 which are specified as targets of a double-colon rule with commands but
64 If you do not specify any makefiles to be read with `-f' or `--file'
65 options, `make' will try the default makefile names; *note What Name to
66 Give Your Makefile: Makefile Names.. Unlike makefiles explicitly
67 requested with `-f' or `--file' options, `make' is not certain that
68 these makefiles should exist. However, if a default makefile does not
69 exist but can be created by running `make' rules, you probably want the
70 rules to be run so that the makefile can be used.
72 Therefore, if none of the default makefiles exists, `make' will try
73 to make each of them in the same order in which they are searched for
74 (*note What Name to Give Your Makefile: Makefile Names.) until it
75 succeeds in making one, or it runs out of names to try. Note that it
76 is not an error if `make' cannot find or make any makefile; a makefile
77 is not always necessary.
79 When you use the `-t' or `--touch' option (*note Instead of
80 Executing the Commands: Instead of Execution.), you would not want to
81 use an out-of-date makefile to decide which targets to touch. So the
82 `-t' option has no effect on updating makefiles; they are really
83 updated even if `-t' is specified. Likewise, `-q' (or `--question')
84 and `-n' (or `--just-print') do not prevent updating of makefiles,
85 because an out-of-date makefile would result in the wrong output for
86 other targets. Thus, `make -f mfile -n foo' will update `mfile', read
87 it in, and then print the commands to update `foo' and its prerequisites
88 without running them. The commands printed for `foo' will be those
89 specified in the updated contents of `mfile'.
91 However, on occasion you might actually wish to prevent updating of
92 even the makefiles. You can do this by specifying the makefiles as
93 goals in the command line as well as specifying them as makefiles.
94 When the makefile name is specified explicitly as a goal, the options
95 `-t' and so on do apply to them.
97 Thus, `make -f mfile -n mfile foo' would read the makefile `mfile',
98 print the commands needed to update it without actually running them,
99 and then print the commands needed to update `foo' without running
100 them. The commands for `foo' will be those specified by the existing
104 File: make.info, Node: Overriding Makefiles, Next: Reading Makefiles, Prev: Remaking Makefiles, Up: Makefiles
106 Overriding Part of Another Makefile
107 ===================================
109 Sometimes it is useful to have a makefile that is mostly just like
110 another makefile. You can often use the `include' directive to include
111 one in the other, and add more targets or variable definitions.
112 However, if the two makefiles give different commands for the same
113 target, `make' will not let you just do this. But there is another way.
115 In the containing makefile (the one that wants to include the other),
116 you can use a match-anything pattern rule to say that to remake any
117 target that cannot be made from the information in the containing
118 makefile, `make' should look in another makefile. *Note Pattern
119 Rules::, for more information on pattern rules.
121 For example, if you have a makefile called `Makefile' that says how
122 to make the target `foo' (and other targets), you can write a makefile
123 called `GNUmakefile' that contains:
129 @$(MAKE) -f Makefile $@
132 If you say `make foo', `make' will find `GNUmakefile', read it, and
133 see that to make `foo', it needs to run the command `frobnicate > foo'.
134 If you say `make bar', `make' will find no way to make `bar' in
135 `GNUmakefile', so it will use the commands from the pattern rule: `make
136 -f Makefile bar'. If `Makefile' provides a rule for updating `bar',
137 `make' will apply the rule. And likewise for any other target that
138 `GNUmakefile' does not say how to make.
140 The way this works is that the pattern rule has a pattern of just
141 `%', so it matches any target whatever. The rule specifies a
142 prerequisite `force', to guarantee that the commands will be run even
143 if the target file already exists. We give `force' target empty
144 commands to prevent `make' from searching for an implicit rule to build
145 it--otherwise it would apply the same match-anything rule to `force'
146 itself and create a prerequisite loop!
149 File: make.info, Node: Reading Makefiles, Prev: Overriding Makefiles, Up: Makefiles
151 How `make' Reads a Makefile
152 ===========================
154 GNU `make' does its work in two distinct phases. During the first
155 phase it reads all the makefiles, included makefiles, etc. and
156 internalizes all the variables and their values, implicit and explicit
157 rules, and constructs a dependency graph of all the targets and their
158 prerequisites. During the second phase, `make' uses these internal
159 structures to determine what targets will need to be rebuilt and to
160 invoke the rules necessary to do so.
162 It's important to understand this two-phase approach because it has a
163 direct impact on how variable and function expansion happens; this is
164 often a source of some confusion when writing makefiles. Here we will
165 present a summary of the phases in which expansion happens for different
166 constructs within the makefile. We say that expansion is "immediate"
167 if it happens during the first phase: in this case `make' will expand
168 any variables or functions in that section of a construct as the
169 makefile is parsed. We say that expansion is "deferred" if expansion
170 is not performed immediately. Expansion of deferred construct is not
171 performed until either the construct appears later in an immediate
172 context, or until the second phase.
174 You may not be familiar with some of these constructs yet. You can
175 reference this section as you become familiar with them, in later
181 Variable definitions are parsed as follows:
184 IMMEDIATE ?= DEFERRED
185 IMMEDIATE := IMMEDIATE
186 IMMEDIATE += DEFERRED or IMMEDIATE
192 For the append operator, `+=', the right-hand side is considered
193 immediate if the variable was previously set as a simple variable
194 (`:='), and deferred otherwise.
196 Conditional Statements
197 ----------------------
199 All instances of conditional syntax are parsed immediately, in their
200 entirety; this includes the `ifdef', `ifeq', `ifndef', and `ifneq'
206 A rule is always expanded the same way, regardless of the form:
208 IMMEDIATE : IMMEDIATE ; DEFERRED
211 That is, the target and prerequisite sections are expanded
212 immediately, and the commands used to construct the target are always
213 deferred. This general rule is true for explicit rules, pattern rules,
214 suffix rules, static pattern rules, and simple prerequisite definitions.
217 File: make.info, Node: Rules, Next: Commands, Prev: Makefiles, Up: Top
222 A "rule" appears in the makefile and says when and how to remake
223 certain files, called the rule's "targets" (most often only one per
224 rule). It lists the other files that are the "prerequisites" of the
225 target, and "commands" to use to create or update the target.
227 The order of rules is not significant, except for determining the
228 "default goal": the target for `make' to consider, if you do not
229 otherwise specify one. The default goal is the target of the first
230 rule in the first makefile. If the first rule has multiple targets,
231 only the first target is taken as the default. There are two
232 exceptions: a target starting with a period is not a default unless it
233 contains one or more slashes, `/', as well; and, a target that defines
234 a pattern rule has no effect on the default goal. (*Note Defining and
235 Redefining Pattern Rules: Pattern Rules.)
237 Therefore, we usually write the makefile so that the first rule is
238 the one for compiling the entire program or all the programs described
239 by the makefile (often with a target called `all'). *Note Arguments to
240 Specify the Goals: Goals.
244 * Rule Example:: An example explained.
245 * Rule Syntax:: General syntax explained.
246 * Prerequisite Types:: There are two types of prerequisites.
247 * Wildcards:: Using wildcard characters such as `*'.
248 * Directory Search:: Searching other directories for source files.
249 * Phony Targets:: Using a target that is not a real file's name.
250 * Force Targets:: You can use a target without commands
251 or prerequisites to mark other
253 * Empty Targets:: When only the date matters and the
255 * Special Targets:: Targets with special built-in meanings.
256 * Multiple Targets:: When to make use of several targets in a rule.
257 * Multiple Rules:: How to use several rules with the same target.
258 * Static Pattern:: Static pattern rules apply to multiple targets
259 and can vary the prerequisites according to
261 * Double-Colon:: How to use a special kind of rule to allow
262 several independent rules for one target.
263 * Automatic Prerequisites:: How to automatically generate rules giving
264 prerequisites from source files themselves.
267 File: make.info, Node: Rule Example, Next: Rule Syntax, Prev: Rules, Up: Rules
272 Here is an example of a rule:
274 foo.o : foo.c defs.h # module for twiddling the frobs
277 Its target is `foo.o' and its prerequisites are `foo.c' and
278 `defs.h'. It has one command, which is `cc -c -g foo.c'. The command
279 line starts with a tab to identify it as a command.
281 This rule says two things:
283 * How to decide whether `foo.o' is out of date: it is out of date if
284 it does not exist, or if either `foo.c' or `defs.h' is more recent
287 * How to update the file `foo.o': by running `cc' as stated. The
288 command does not explicitly mention `defs.h', but we presume that
289 `foo.c' includes it, and that that is why `defs.h' was added to
293 File: make.info, Node: Rule Syntax, Next: Prerequisite Types, Prev: Rule Example, Up: Rules
298 In general, a rule looks like this:
300 TARGETS : PREREQUISITES
306 TARGETS : PREREQUISITES ; COMMAND
310 The TARGETS are file names, separated by spaces. Wildcard
311 characters may be used (*note Using Wildcard Characters in File Names:
312 Wildcards.) and a name of the form `A(M)' represents member M in
313 archive file A (*note Archive Members as Targets: Archive Members.).
314 Usually there is only one target per rule, but occasionally there is a
315 reason to have more (*note Multiple Targets in a Rule: Multiple
318 The COMMAND lines start with a tab character. The first command may
319 appear on the line after the prerequisites, with a tab character, or may
320 appear on the same line, with a semicolon. Either way, the effect is
321 the same. *Note Writing the Commands in Rules: Commands.
323 Because dollar signs are used to start variable references, if you
324 really want a dollar sign in a rule you must write two of them, `$$'
325 (*note How to Use Variables: Using Variables.). You may split a long
326 line by inserting a backslash followed by a newline, but this is not
327 required, as `make' places no limit on the length of a line in a
330 A rule tells `make' two things: when the targets are out of date,
331 and how to update them when necessary.
333 The criterion for being out of date is specified in terms of the
334 PREREQUISITES, which consist of file names separated by spaces.
335 (Wildcards and archive members (*note Archives::) are allowed here too.)
336 A target is out of date if it does not exist or if it is older than any
337 of the prerequisites (by comparison of last-modification times). The
338 idea is that the contents of the target file are computed based on
339 information in the prerequisites, so if any of the prerequisites
340 changes, the contents of the existing target file are no longer
343 How to update is specified by COMMANDS. These are lines to be
344 executed by the shell (normally `sh'), but with some extra features
345 (*note Writing the Commands in Rules: Commands.).
348 File: make.info, Node: Prerequisite Types, Next: Wildcards, Prev: Rule Syntax, Up: Rules
350 Types of Prerequisites
351 ======================
353 There are actually two different types of prerequisites understood by
354 GNU `make': normal prerequisites such as described in the previous
355 section, and "order-only" prerequisites. A normal prerequisite
356 actually makes two statements: first, it imposes an order of execution
357 of build commands: any commands necessary to build any of a target's
358 prerequisites will be fully executed before any commands necessary to
359 build the target. Second, it imposes a dependency relationship: if any
360 prerequisite is newer than the target, then the target is considered
361 out-of-date and must be rebuilt.
363 Normally, this is exactly what you want: if a target's prerequisite
364 is updated, then the target should also be updated.
366 Occasionally, however, you have a situation where you want to impose
367 a specific ordering on the rules to be invoked _without_ forcing the
368 target to be updated if one of those rules is executed. In that case,
369 you want to define "order-only" prerequisites. Order-only
370 prerequisites can be specified by placing a pipe symbol (`|') in the
371 prerequisites list: any prerequisites to the left of the pipe symbol
372 are normal; any prerequisites to the right are order-only:
374 TARGETS : NORMAL-PREREQUISITES | ORDER-ONLY-PREREQUISITES
376 The normal prerequisites section may of course be empty. Also, you
377 may still declare multiple lines of prerequisites for the same target:
378 they are appended appropriately. Note that if you declare the same
379 file to be both a normal and an order-only prerequisite, the normal
380 prerequisite takes precedence (since they are a strict superset of the
381 behavior of an order-only prerequisite).
384 File: make.info, Node: Wildcards, Next: Directory Search, Prev: Prerequisite Types, Up: Rules
386 Using Wildcard Characters in File Names
387 =======================================
389 A single file name can specify many files using "wildcard
390 characters". The wildcard characters in `make' are `*', `?' and
391 `[...]', the same as in the Bourne shell. For example, `*.c' specifies
392 a list of all the files (in the working directory) whose names end in
395 The character `~' at the beginning of a file name also has special
396 significance. If alone, or followed by a slash, it represents your home
397 directory. For example `~/bin' expands to `/home/you/bin'. If the `~'
398 is followed by a word, the string represents the home directory of the
399 user named by that word. For example `~john/bin' expands to
400 `/home/john/bin'. On systems which don't have a home directory for
401 each user (such as MS-DOS or MS-Windows), this functionality can be
402 simulated by setting the environment variable HOME.
404 Wildcard expansion happens automatically in targets, in
405 prerequisites, and in commands (where the shell does the expansion).
406 In other contexts, wildcard expansion happens only if you request it
407 explicitly with the `wildcard' function.
409 The special significance of a wildcard character can be turned off by
410 preceding it with a backslash. Thus, `foo\*bar' would refer to a
411 specific file whose name consists of `foo', an asterisk, and `bar'.
415 * Wildcard Examples:: Several examples
416 * Wildcard Pitfall:: Problems to avoid.
417 * Wildcard Function:: How to cause wildcard expansion where
418 it does not normally take place.
421 File: make.info, Node: Wildcard Examples, Next: Wildcard Pitfall, Prev: Wildcards, Up: Wildcards
426 Wildcards can be used in the commands of a rule, where they are
427 expanded by the shell. For example, here is a rule to delete all the
433 Wildcards are also useful in the prerequisites of a rule. With the
434 following rule in the makefile, `make print' will print all the `.c'
435 files that have changed since the last time you printed them:
441 This rule uses `print' as an empty target file; see *Note Empty Target
442 Files to Record Events: Empty Targets. (The automatic variable `$?' is
443 used to print only those files that have changed; see *Note Automatic
444 Variables: Automatic.)
446 Wildcard expansion does not happen when you define a variable.
447 Thus, if you write this:
451 then the value of the variable `objects' is the actual string `*.o'.
452 However, if you use the value of `objects' in a target, prerequisite or
453 command, wildcard expansion will take place at that time. To set
454 `objects' to the expansion, instead use:
456 objects := $(wildcard *.o)
458 *Note Wildcard Function::.
461 File: make.info, Node: Wildcard Pitfall, Next: Wildcard Function, Prev: Wildcard Examples, Up: Wildcards
463 Pitfalls of Using Wildcards
464 ---------------------------
466 Now here is an example of a naive way of using wildcard expansion,
467 that does not do what you would intend. Suppose you would like to say
468 that the executable file `foo' is made from all the object files in the
469 directory, and you write this:
474 cc -o foo $(CFLAGS) $(objects)
476 The value of `objects' is the actual string `*.o'. Wildcard expansion
477 happens in the rule for `foo', so that each _existing_ `.o' file
478 becomes a prerequisite of `foo' and will be recompiled if necessary.
480 But what if you delete all the `.o' files? When a wildcard matches
481 no files, it is left as it is, so then `foo' will depend on the
482 oddly-named file `*.o'. Since no such file is likely to exist, `make'
483 will give you an error saying it cannot figure out how to make `*.o'.
484 This is not what you want!
486 Actually it is possible to obtain the desired result with wildcard
487 expansion, but you need more sophisticated techniques, including the
488 `wildcard' function and string substitution. *Note The Function
489 `wildcard': Wildcard Function.
491 Microsoft operating systems (MS-DOS and MS-Windows) use backslashes
492 to separate directories in pathnames, like so:
496 This is equivalent to the Unix-style `c:/foo/bar/baz.c' (the `c:'
497 part is the so-called drive letter). When `make' runs on these
498 systems, it supports backslashes as well as the Unix-style forward
499 slashes in pathnames. However, this support does _not_ include the
500 wildcard expansion, where backslash is a quote character. Therefore,
501 you _must_ use Unix-style slashes in these cases.
504 File: make.info, Node: Wildcard Function, Prev: Wildcard Pitfall, Up: Wildcards
506 The Function `wildcard'
507 -----------------------
509 Wildcard expansion happens automatically in rules. But wildcard
510 expansion does not normally take place when a variable is set, or
511 inside the arguments of a function. If you want to do wildcard
512 expansion in such places, you need to use the `wildcard' function, like
515 $(wildcard PATTERN...)
517 This string, used anywhere in a makefile, is replaced by a
518 space-separated list of names of existing files that match one of the
519 given file name patterns. If no existing file name matches a pattern,
520 then that pattern is omitted from the output of the `wildcard'
521 function. Note that this is different from how unmatched wildcards
522 behave in rules, where they are used verbatim rather than ignored
523 (*note Wildcard Pitfall::).
525 One use of the `wildcard' function is to get a list of all the C
526 source files in a directory, like this:
530 We can change the list of C source files into a list of object files
531 by replacing the `.c' suffix with `.o' in the result, like this:
533 $(patsubst %.c,%.o,$(wildcard *.c))
535 (Here we have used another function, `patsubst'. *Note Functions for
536 String Substitution and Analysis: Text Functions.)
538 Thus, a makefile to compile all C source files in the directory and
539 then link them together could be written as follows:
541 objects := $(patsubst %.c,%.o,$(wildcard *.c))
546 (This takes advantage of the implicit rule for compiling C programs, so
547 there is no need to write explicit rules for compiling the files.
548 *Note The Two Flavors of Variables: Flavors, for an explanation of
549 `:=', which is a variant of `='.)
552 File: make.info, Node: Directory Search, Next: Phony Targets, Prev: Wildcards, Up: Rules
554 Searching Directories for Prerequisites
555 =======================================
557 For large systems, it is often desirable to put sources in a separate
558 directory from the binaries. The "directory search" features of `make'
559 facilitate this by searching several directories automatically to find
560 a prerequisite. When you redistribute the files among directories, you
561 do not need to change the individual rules, just the search paths.
565 * General Search:: Specifying a search path that applies
566 to every prerequisite.
567 * Selective Search:: Specifying a search path
568 for a specified class of names.
569 * Search Algorithm:: When and how search paths are applied.
570 * Commands/Search:: How to write shell commands that work together
572 * Implicit/Search:: How search paths affect implicit rules.
573 * Libraries/Search:: Directory search for link libraries.
576 File: make.info, Node: General Search, Next: Selective Search, Prev: Directory Search, Up: Directory Search
578 `VPATH': Search Path for All Prerequisites
579 ------------------------------------------
581 The value of the `make' variable `VPATH' specifies a list of
582 directories that `make' should search. Most often, the directories are
583 expected to contain prerequisite files that are not in the current
584 directory; however, `VPATH' specifies a search list that `make' applies
585 for all files, including files which are targets of rules.
587 Thus, if a file that is listed as a target or prerequisite does not
588 exist in the current directory, `make' searches the directories listed
589 in `VPATH' for a file with that name. If a file is found in one of
590 them, that file may become the prerequisite (see below). Rules may then
591 specify the names of files in the prerequisite list as if they all
592 existed in the current directory. *Note Writing Shell Commands with
593 Directory Search: Commands/Search.
595 In the `VPATH' variable, directory names are separated by colons or
596 blanks. The order in which directories are listed is the order followed
597 by `make' in its search. (On MS-DOS and MS-Windows, semi-colons are
598 used as separators of directory names in `VPATH', since the colon can
599 be used in the pathname itself, after the drive letter.)
603 VPATH = src:../headers
605 specifies a path containing two directories, `src' and `../headers',
606 which `make' searches in that order.
608 With this value of `VPATH', the following rule,
612 is interpreted as if it were written like this:
616 assuming the file `foo.c' does not exist in the current directory but
617 is found in the directory `src'.
620 File: make.info, Node: Selective Search, Next: Search Algorithm, Prev: General Search, Up: Directory Search
622 The `vpath' Directive
623 ---------------------
625 Similar to the `VPATH' variable, but more selective, is the `vpath'
626 directive (note lower case), which allows you to specify a search path
627 for a particular class of file names: those that match a particular
628 pattern. Thus you can supply certain search directories for one class
629 of file names and other directories (or none) for other file names.
631 There are three forms of the `vpath' directive:
633 `vpath PATTERN DIRECTORIES'
634 Specify the search path DIRECTORIES for file names that match
637 The search path, DIRECTORIES, is a list of directories to be
638 searched, separated by colons (semi-colons on MS-DOS and
639 MS-Windows) or blanks, just like the search path used in the
643 Clear out the search path associated with PATTERN.
646 Clear all search paths previously specified with `vpath'
649 A `vpath' pattern is a string containing a `%' character. The
650 string must match the file name of a prerequisite that is being searched
651 for, the `%' character matching any sequence of zero or more characters
652 (as in pattern rules; *note Defining and Redefining Pattern Rules:
653 Pattern Rules.). For example, `%.h' matches files that end in `.h'.
654 (If there is no `%', the pattern must match the prerequisite exactly,
655 which is not useful very often.)
657 `%' characters in a `vpath' directive's pattern can be quoted with
658 preceding backslashes (`\'). Backslashes that would otherwise quote
659 `%' characters can be quoted with more backslashes. Backslashes that
660 quote `%' characters or other backslashes are removed from the pattern
661 before it is compared to file names. Backslashes that are not in
662 danger of quoting `%' characters go unmolested.
664 When a prerequisite fails to exist in the current directory, if the
665 PATTERN in a `vpath' directive matches the name of the prerequisite
666 file, then the DIRECTORIES in that directive are searched just like
667 (and before) the directories in the `VPATH' variable.
673 tells `make' to look for any prerequisite whose name ends in `.h' in
674 the directory `../headers' if the file is not found in the current
677 If several `vpath' patterns match the prerequisite file's name, then
678 `make' processes each matching `vpath' directive one by one, searching
679 all the directories mentioned in each directive. `make' handles
680 multiple `vpath' directives in the order in which they appear in the
681 makefile; multiple directives with the same pattern are independent of
690 will look for a file ending in `.c' in `foo', then `blish', then `bar',
696 will look for a file ending in `.c' in `foo', then `bar', then `blish'.
699 File: make.info, Node: Search Algorithm, Next: Commands/Search, Prev: Selective Search, Up: Directory Search
701 How Directory Searches are Performed
702 ------------------------------------
704 When a prerequisite is found through directory search, regardless of
705 type (general or selective), the pathname located may not be the one
706 that `make' actually provides you in the prerequisite list. Sometimes
707 the path discovered through directory search is thrown away.
709 The algorithm `make' uses to decide whether to keep or abandon a
710 path found via directory search is as follows:
712 1. If a target file does not exist at the path specified in the
713 makefile, directory search is performed.
715 2. If the directory search is successful, that path is kept and this
716 file is tentatively stored as the target.
718 3. All prerequisites of this target are examined using this same
721 4. After processing the prerequisites, the target may or may not need
724 a. If the target does _not_ need to be rebuilt, the path to the
725 file found during directory search is used for any
726 prerequisite lists which contain this target. In short, if
727 `make' doesn't need to rebuild the target then you use the
728 path found via directory search.
730 b. If the target _does_ need to be rebuilt (is out-of-date), the
731 pathname found during directory search is _thrown away_, and
732 the target is rebuilt using the file name specified in the
733 makefile. In short, if `make' must rebuild, then the target
734 is rebuilt locally, not in the directory found via directory
737 This algorithm may seem complex, but in practice it is quite often
738 exactly what you want.
740 Other versions of `make' use a simpler algorithm: if the file does
741 not exist, and it is found via directory search, then that pathname is
742 always used whether or not the target needs to be built. Thus, if the
743 target is rebuilt it is created at the pathname discovered during
746 If, in fact, this is the behavior you want for some or all of your
747 directories, you can use the `GPATH' variable to indicate this to
750 `GPATH' has the same syntax and format as `VPATH' (that is, a space-
751 or colon-delimited list of pathnames). If an out-of-date target is
752 found by directory search in a directory that also appears in `GPATH',
753 then that pathname is not thrown away. The target is rebuilt using the
757 File: make.info, Node: Commands/Search, Next: Implicit/Search, Prev: Search Algorithm, Up: Directory Search
759 Writing Shell Commands with Directory Search
760 --------------------------------------------
762 When a prerequisite is found in another directory through directory
763 search, this cannot change the commands of the rule; they will execute
764 as written. Therefore, you must write the commands with care so that
765 they will look for the prerequisite in the directory where `make' finds
768 This is done with the "automatic variables" such as `$^' (*note
769 Automatic Variables: Automatic.). For instance, the value of `$^' is a
770 list of all the prerequisites of the rule, including the names of the
771 directories in which they were found, and the value of `$@' is the
775 cc -c $(CFLAGS) $^ -o $@
777 (The variable `CFLAGS' exists so you can specify flags for C
778 compilation by implicit rules; we use it here for consistency so it will
779 affect all C compilations uniformly; *note Variables Used by Implicit
780 Rules: Implicit Variables..)
782 Often the prerequisites include header files as well, which you do
783 not want to mention in the commands. The automatic variable `$<' is
784 just the first prerequisite:
786 VPATH = src:../headers
787 foo.o : foo.c defs.h hack.h
788 cc -c $(CFLAGS) $< -o $@
791 File: make.info, Node: Implicit/Search, Next: Libraries/Search, Prev: Commands/Search, Up: Directory Search
793 Directory Search and Implicit Rules
794 -----------------------------------
796 The search through the directories specified in `VPATH' or with
797 `vpath' also happens during consideration of implicit rules (*note
798 Using Implicit Rules: Implicit Rules.).
800 For example, when a file `foo.o' has no explicit rule, `make'
801 considers implicit rules, such as the built-in rule to compile `foo.c'
802 if that file exists. If such a file is lacking in the current
803 directory, the appropriate directories are searched for it. If `foo.c'
804 exists (or is mentioned in the makefile) in any of the directories, the
805 implicit rule for C compilation is applied.
807 The commands of implicit rules normally use automatic variables as a
808 matter of necessity; consequently they will use the file names found by
809 directory search with no extra effort.
812 File: make.info, Node: Libraries/Search, Prev: Implicit/Search, Up: Directory Search
814 Directory Search for Link Libraries
815 -----------------------------------
817 Directory search applies in a special way to libraries used with the
818 linker. This special feature comes into play when you write a
819 prerequisite whose name is of the form `-lNAME'. (You can tell
820 something strange is going on here because the prerequisite is normally
821 the name of a file, and the _file name_ of a library generally looks
822 like `libNAME.a', not like `-lNAME'.)
824 When a prerequisite's name has the form `-lNAME', `make' handles it
825 specially by searching for the file `libNAME.so' in the current
826 directory, in directories specified by matching `vpath' search paths
827 and the `VPATH' search path, and then in the directories `/lib',
828 `/usr/lib', and `PREFIX/lib' (normally `/usr/local/lib', but
829 MS-DOS/MS-Windows versions of `make' behave as if PREFIX is defined to
830 be the root of the DJGPP installation tree).
832 If that file is not found, then the file `libNAME.a' is searched
833 for, in the same directories as above.
835 For example, if there is a `/usr/lib/libcurses.a' library on your
836 system (and no `/usr/lib/libcurses.so' file), then
841 would cause the command `cc foo.c /usr/lib/libcurses.a -o foo' to be
842 executed when `foo' is older than `foo.c' or than
843 `/usr/lib/libcurses.a'.
845 Although the default set of files to be searched for is `libNAME.so'
846 and `libNAME.a', this is customizable via the `.LIBPATTERNS' variable.
847 Each word in the value of this variable is a pattern string. When a
848 prerequisite like `-lNAME' is seen, `make' will replace the percent in
849 each pattern in the list with NAME and perform the above directory
850 searches using that library filename. If no library is found, the next
851 word in the list will be used.
853 The default value for `.LIBPATTERNS' is "`lib%.so lib%.a'", which
854 provides the default behavior described above.
856 You can turn off link library expansion completely by setting this
857 variable to an empty value.
860 File: make.info, Node: Phony Targets, Next: Force Targets, Prev: Directory Search, Up: Rules
865 A phony target is one that is not really the name of a file. It is
866 just a name for some commands to be executed when you make an explicit
867 request. There are two reasons to use a phony target: to avoid a
868 conflict with a file of the same name, and to improve performance.
870 If you write a rule whose commands will not create the target file,
871 the commands will be executed every time the target comes up for
872 remaking. Here is an example:
877 Because the `rm' command does not create a file named `clean', probably
878 no such file will ever exist. Therefore, the `rm' command will be
879 executed every time you say `make clean'.
881 The phony target will cease to work if anything ever does create a
882 file named `clean' in this directory. Since it has no prerequisites,
883 the file `clean' would inevitably be considered up to date, and its
884 commands would not be executed. To avoid this problem, you can
885 explicitly declare the target to be phony, using the special target
886 `.PHONY' (*note Special Built-in Target Names: Special Targets.) as
891 Once this is done, `make clean' will run the commands regardless of
892 whether there is a file named `clean'.
894 Since it knows that phony targets do not name actual files that
895 could be remade from other files, `make' skips the implicit rule search
896 for phony targets (*note Implicit Rules::). This is why declaring a
897 target phony is good for performance, even if you are not worried about
898 the actual file existing.
900 Thus, you first write the line that states that `clean' is a phony
901 target, then you write the rule, like this:
907 Another example of the usefulness of phony targets is in conjunction
908 with recursive invocations of `make' (for more information, see *Note
909 Recursive Use of `make': Recursion). In this case the makefile will
910 often contain a variable which lists a number of subdirectories to be
911 built. One way to handle this is with one rule whose command is a
912 shell loop over the subdirectories, like this:
914 SUBDIRS = foo bar baz
917 for dir in $(SUBDIRS); do \
921 There are a few problems with this method, however. First, any error
922 detected in a submake is not noted by this rule, so it will continue to
923 build the rest of the directories even when one fails. This can be
924 overcome by adding shell commands to note the error and exit, but then
925 it will do so even if `make' is invoked with the `-k' option, which is
926 unfortunate. Second, and perhaps more importantly, you cannot take
927 advantage of the parallel build capabilities of make using this method,
928 since there is only one rule.
930 By declaring the subdirectories as phony targets (you must do this as
931 the subdirectory obviously always exists; otherwise it won't be built)
932 you can remove these problems:
934 SUBDIRS = foo bar baz
936 .PHONY: subdirs $(SUBDIRS)
945 Here we've also declared that the `foo' subdirectory cannot be built
946 until after the `baz' subdirectory is complete; this kind of
947 relationship declaration is particularly important when attempting
950 A phony target should not be a prerequisite of a real target file;
951 if it is, its commands are run every time `make' goes to update that
952 file. As long as a phony target is never a prerequisite of a real
953 target, the phony target commands will be executed only when the phony
954 target is a specified goal (*note Arguments to Specify the Goals:
957 Phony targets can have prerequisites. When one directory contains
958 multiple programs, it is most convenient to describe all of the
959 programs in one makefile `./Makefile'. Since the target remade by
960 default will be the first one in the makefile, it is common to make
961 this a phony target named `all' and give it, as prerequisites, all the
962 individual programs. For example:
964 all : prog1 prog2 prog3
967 prog1 : prog1.o utils.o
968 cc -o prog1 prog1.o utils.o
973 prog3 : prog3.o sort.o utils.o
974 cc -o prog3 prog3.o sort.o utils.o
976 Now you can say just `make' to remake all three programs, or specify as
977 arguments the ones to remake (as in `make prog1 prog3').
979 When one phony target is a prerequisite of another, it serves as a
980 subroutine of the other. For example, here `make cleanall' will delete
981 the object files, the difference files, and the file `program':
983 .PHONY: cleanall cleanobj cleandiff
985 cleanall : cleanobj cleandiff
995 File: make.info, Node: Force Targets, Next: Empty Targets, Prev: Phony Targets, Up: Rules
997 Rules without Commands or Prerequisites
998 =======================================
1000 If a rule has no prerequisites or commands, and the target of the
1001 rule is a nonexistent file, then `make' imagines this target to have
1002 been updated whenever its rule is run. This implies that all targets
1003 depending on this one will always have their commands run.
1005 An example will illustrate this:
1011 Here the target `FORCE' satisfies the special conditions, so the
1012 target `clean' that depends on it is forced to run its commands. There
1013 is nothing special about the name `FORCE', but that is one name
1014 commonly used this way.
1016 As you can see, using `FORCE' this way has the same results as using
1019 Using `.PHONY' is more explicit and more efficient. However, other
1020 versions of `make' do not support `.PHONY'; thus `FORCE' appears in
1021 many makefiles. *Note Phony Targets::.
1024 File: make.info, Node: Empty Targets, Next: Special Targets, Prev: Force Targets, Up: Rules
1026 Empty Target Files to Record Events
1027 ===================================
1029 The "empty target" is a variant of the phony target; it is used to
1030 hold commands for an action that you request explicitly from time to
1031 time. Unlike a phony target, this target file can really exist; but
1032 the file's contents do not matter, and usually are empty.
1034 The purpose of the empty target file is to record, with its
1035 last-modification time, when the rule's commands were last executed. It
1036 does so because one of the commands is a `touch' command to update the
1039 The empty target file should have some prerequisites (otherwise it
1040 doesn't make sense). When you ask to remake the empty target, the
1041 commands are executed if any prerequisite is more recent than the
1042 target; in other words, if a prerequisite has changed since the last
1043 time you remade the target. Here is an example:
1049 With this rule, `make print' will execute the `lpr' command if either
1050 source file has changed since the last `make print'. The automatic
1051 variable `$?' is used to print only those files that have changed
1052 (*note Automatic Variables: Automatic.).
1055 File: make.info, Node: Special Targets, Next: Multiple Targets, Prev: Empty Targets, Up: Rules
1057 Special Built-in Target Names
1058 =============================
1060 Certain names have special meanings if they appear as targets.
1063 The prerequisites of the special target `.PHONY' are considered to
1064 be phony targets. When it is time to consider such a target,
1065 `make' will run its commands unconditionally, regardless of
1066 whether a file with that name exists or what its last-modification
1067 time is. *Note Phony Targets: Phony Targets.
1070 The prerequisites of the special target `.SUFFIXES' are the list
1071 of suffixes to be used in checking for suffix rules. *Note
1072 Old-Fashioned Suffix Rules: Suffix Rules.
1075 The commands specified for `.DEFAULT' are used for any target for
1076 which no rules are found (either explicit rules or implicit rules).
1077 *Note Last Resort::. If `.DEFAULT' commands are specified, every
1078 file mentioned as a prerequisite, but not as a target in a rule,
1079 will have these commands executed on its behalf. *Note Implicit
1080 Rule Search Algorithm: Implicit Rule Search.
1083 The targets which `.PRECIOUS' depends on are given the following
1084 special treatment: if `make' is killed or interrupted during the
1085 execution of their commands, the target is not deleted. *Note
1086 Interrupting or Killing `make': Interrupts. Also, if the target
1087 is an intermediate file, it will not be deleted after it is no
1088 longer needed, as is normally done. *Note Chains of Implicit
1089 Rules: Chained Rules. In this latter respect it overlaps with the
1090 `.SECONDARY' special target.
1092 You can also list the target pattern of an implicit rule (such as
1093 `%.o') as a prerequisite file of the special target `.PRECIOUS' to
1094 preserve intermediate files created by rules whose target patterns
1095 match that file's name.
1098 The targets which `.INTERMEDIATE' depends on are treated as
1099 intermediate files. *Note Chains of Implicit Rules: Chained Rules.
1100 `.INTERMEDIATE' with no prerequisites has no effect.
1103 The targets which `.SECONDARY' depends on are treated as
1104 intermediate files, except that they are never automatically
1105 deleted. *Note Chains of Implicit Rules: Chained Rules.
1107 `.SECONDARY' with no prerequisites causes all targets to be treated
1108 as secondary (i.e., no target is removed because it is considered
1112 If `.DELETE_ON_ERROR' is mentioned as a target anywhere in the
1113 makefile, then `make' will delete the target of a rule if it has
1114 changed and its commands exit with a nonzero exit status, just as
1115 it does when it receives a signal. *Note Errors in Commands:
1119 If you specify prerequisites for `.IGNORE', then `make' will
1120 ignore errors in execution of the commands run for those particular
1121 files. The commands for `.IGNORE' are not meaningful.
1123 If mentioned as a target with no prerequisites, `.IGNORE' says to
1124 ignore errors in execution of commands for all files. This usage
1125 of `.IGNORE' is supported only for historical compatibility. Since
1126 this affects every command in the makefile, it is not very useful;
1127 we recommend you use the more selective ways to ignore errors in
1128 specific commands. *Note Errors in Commands: Errors.
1130 `.LOW_RESOLUTION_TIME'
1131 If you specify prerequisites for `.LOW_RESOLUTION_TIME', `make'
1132 assumes that these files are created by commands that generate low
1133 resolution time stamps. The commands for `.LOW_RESOLUTION_TIME'
1136 The high resolution file time stamps of many modern hosts lessen
1137 the chance of `make' incorrectly concluding that a file is up to
1138 date. Unfortunately, these hosts provide no way to set a high
1139 resolution file time stamp, so commands like `cp -p' that
1140 explicitly set a file's time stamp must discard its subsecond
1141 part. If a file is created by such a command, you should list it
1142 as a prerequisite of `.LOW_RESOLUTION_TIME' so that `make' does
1143 not mistakenly conclude that the file is out of date. For example:
1145 .LOW_RESOLUTION_TIME: dst
1149 Since `cp -p' discards the subsecond part of `src''s time stamp,
1150 `dst' is typically slightly older than `src' even when it is up to
1151 date. The `.LOW_RESOLUTION_TIME' line causes `make' to consider
1152 `dst' to be up to date if its time stamp is at the start of the
1153 same second that `src''s time stamp is in.
1155 Due to a limitation of the archive format, archive member time
1156 stamps are always low resolution. You need not list archive
1157 members as prerequisites of `.LOW_RESOLUTION_TIME', as `make' does
1161 If you specify prerequisites for `.SILENT', then `make' will not
1162 print the commands to remake those particular files before
1163 executing them. The commands for `.SILENT' are not meaningful.
1165 If mentioned as a target with no prerequisites, `.SILENT' says not
1166 to print any commands before executing them. This usage of
1167 `.SILENT' is supported only for historical compatibility. We
1168 recommend you use the more selective ways to silence specific
1169 commands. *Note Command Echoing: Echoing. If you want to silence
1170 all commands for a particular run of `make', use the `-s' or
1171 `--silent' option (*note Options Summary::).
1173 `.EXPORT_ALL_VARIABLES'
1174 Simply by being mentioned as a target, this tells `make' to export
1175 all variables to child processes by default. *Note Communicating
1176 Variables to a Sub-`make': Variables/Recursion.
1179 If `.NOTPARALLEL' is mentioned as a target, then this invocation of
1180 `make' will be run serially, even if the `-j' option is given.
1181 Any recursively invoked `make' command will still be run in
1182 parallel (unless its makefile contains this target). Any
1183 prerequisites on this target are ignored.
1185 Any defined implicit rule suffix also counts as a special target if
1186 it appears as a target, and so does the concatenation of two suffixes,
1187 such as `.c.o'. These targets are suffix rules, an obsolete way of
1188 defining implicit rules (but a way still widely used). In principle,
1189 any target name could be special in this way if you break it in two and
1190 add both pieces to the suffix list. In practice, suffixes normally
1191 begin with `.', so these special target names also begin with `.'.
1192 *Note Old-Fashioned Suffix Rules: Suffix Rules.