Make
Make
GNU Make
A Program for Directing Recompilation
GNU make Version 3.77.
May 1998
Permission is granted to make and distribute verbatim copies of this manual provided the
copyright notice and this permission notice are preserved on all copies.
Permission is granted to copy and distribute modified versions of this manual under the con-
ditions for verbatim copying, provided that the entire resulting derived work is distributed
under the terms of a permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual into another lan-
guage, under the above conditions for modified versions, except that this permission notice
may be stated in a translation approved by the Free Software Foundation.
1 Overview of make
The make utility automatically determines which pieces of a large program need to be
recompiled, and issues commands to recompile them. This manual describes GNU make,
which was implemented by Richard Stallman and Roland McGrath. GNU make conforms
to section 6.2 of IEEE Standard 1003.2-1992 (POSIX.2).
Our examples show C programs, since they are most common, but you can use make
with any programming language whose compiler can be run with a shell command. Indeed,
make is not limited to programs. You can use it to describe any task where some files must
be updated automatically from others whenever the others change.
To prepare to use make, you must write a file called the makefile that describes the
relationships among files in your program and provides commands for updating each file.
In a program, typically, the executable file is updated from object files, which are in turn
made by compiling source files.
Once a suitable makefile exists, each time you change some source files, this simple shell
command:
make
suffices to perform all necessary recompilations. The make program uses the makefile data
base and the last-modification times of the files to decide which of the files need to be
updated. For each of those files, it issues the commands recorded in the data base.
You can provide command line arguments to make to control which files should be
recompiled, or how. See Chapter 9 [How to Run make], page 73.
clear whether you should be able to do something or not, report that too; it’s a bug in the
documentation!
Before reporting a bug or trying to fix it yourself, try to isolate it to the smallest possible
makefile that reproduces the problem. Then send us the makefile and the exact results make
gave you. Also say what you expected to occur; this will help us decide whether the problem
was really in the documentation.
Once you’ve got a precise problem, please send electronic mail to:
bug-make@gnu.org
Please include the version number of make you are using. You can get this information
with the command ‘make --version’. Be sure also to include the type of machine and
operating system you are using. If possible, include the contents of the file ‘config.h’ that
is generated by the configuration process.
Chapter 2: An Introduction to Makefiles 3
2 An Introduction to Makefiles
You need a file called a makefile to tell make what to do. Most often, the makefile tells
make how to compile and link a program.
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 make 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 Appendix C [Complex Makefile], page 131.
When make 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 shell command follows each line that contains a target and dependencies. These shell
commands say how to update the target file. A tab character must come at the beginning of
every command line to distinguish commands lines from other lines in the makefile. (Bear
in mind that make does not know anything about how the commands work. It is up to you
to supply commands that will update the target file properly. All make does is execute the
commands in the rule you have specified when the target file needs to be updated.)
The target ‘clean’ 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, ‘clean’ is not a dependency of any other
rule. Consequently, make never does anything with it unless you tell it specifically. Note
that this rule not only is not a dependency, it also does not have any dependencies, so the
only purpose of the rule is to run the specified commands. Targets that do not refer to files
but are just actions are called phony targets. See Section 4.4 [Phony Targets], page 22, for
information about this kind of target. See Section 5.4 [Errors in Commands], page 36, to
see how to cause make to ignore errors from rm or any other command.
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)
Chapter 2: An Introduction to Makefiles 7
It is not necessary to spell out the commands for compiling the individual C source
files, because make can figure them out: it has an implicit rule for updating a ‘.o’ file from
a correspondingly named ‘.c’ file using a ‘cc -c’ command. For example, it will use the
command ‘cc -c main.c -o main.o’ to compile ‘main.c’ into ‘main.o’. We can therefore
omit the commands from the rules for the object files. See Chapter 10 [Using Implicit
Rules], page 83.
When a ‘.c’ file is used automatically in this way, it is also automatically added to the
list of dependencies. We can therefore omit the ‘.c’ files from the dependencies, provided
we omit the commands.
Here is the entire example, with both of these changes, and a variable objects as
suggested above:
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)
This is how we would write the makefile in actual practice. (The complications associ-
ated with ‘clean’ are described elsewhere. See Section 4.4 [Phony Targets], page 22, and
Section 5.4 [Errors in Commands], page 36.)
Because implicit rules are so convenient, they are important. You will see them used
frequently.
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 dependencies
instead of by their targets. Here is what one looks like:
8 GNU make
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
Here ‘defs.h’ is given as a dependency of all the object files; ‘command.h’ and ‘buffer.h’
are dependencies of the specific object files listed for them.
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.
3 Writing Makefiles
The information that tells make how to recompile a system comes from reading a data
base called the makefile.
specific to GNU make, and will not be understood by other versions of make. Other make
programs look for ‘makefile’ and ‘Makefile’, but not ‘GNUmakefile’.
If make finds none of these names, it does not use any makefile. Then you must specify
a goal with a command argument, and make will attempt to figure out how to remake it
using only its built-in implicit rules. See Chapter 10 [Using Implicit Rules], page 83.
If you want to use a nonstandard name for your makefile, you can specify the makefile
name with the ‘-f’ or ‘--file’ option. The arguments ‘-f name’ or ‘--file=name’ tell
make to read the file name as the makefile. If you use more than one ‘-f’ or ‘--file’ option,
you can specify several makefiles. All the makefiles are effectively concatenated in the order
specified. The default makefile names ‘GNUmakefile’, ‘makefile’ and ‘Makefile’ are not
checked automatically if you specify ‘-f’ or ‘--file’.
specified with the ‘-I’ or ‘--include-dir’ option are searched (see Section 9.7 [Sum-
mary of Options], page 78). Then the following directories (if they exist) are searched,
in this order: ‘prefix/include’ (normally ‘/usr/local/include’1 ) ‘/usr/gnu/include’,
‘/usr/local/include’, ‘/usr/include’.
If an included makefile cannot be found in any of these directories, a warning message
is generated, but it is not an immediately fatal error; processing of the makefile containing
the include continues. Once it has finished reading makefiles, make will try to remake any
that are out of date or don’t exist. See Section 3.5 [How Makefiles Are Remade], page 11.
Only after it has tried to find a way to remake a makefile and failed, will make diagnose the
missing makefile as a fatal error.
If you want make to simply ignore a makefile which does not exist and cannot be remade,
with no error message, use the -include directive instead of include, like this:
-include filenames. . .
This is acts like include in every way except that there is no error (not even a warning) if
any of the filenames do not exist. For compatibility with some other make implementations,
sinclude is another name for -include.
To this end, after reading in all makefiles, make will consider each as a goal target and
attempt to update it. If a makefile has a rule which says how to update it (found either
in that very makefile or in another one) or if an implicit rule applies to it (see Chapter 10
[Using Implicit Rules], page 83), it will be updated if necessary. After all makefiles have
been checked, if any have actually been changed, make starts with a clean slate and reads
all the makefiles over again. (It will also attempt to update each of them over again, but
normally this will not change them again, since they are already up to date.)
If the makefiles specify a double-colon rule to remake a file with commands but no
dependencies, that file will always be remade (see Section 4.11 [Double-Colon], page 29).
In the case of makefiles, a makefile that has a double-colon rule with commands but no
dependencies will be remade every time make is run, and then again after make starts over
and reads the makefiles in again. This would cause an infinite loop: make would constantly
remake the makefile, and never do anything else. So, to avoid this, make will not attempt
to remake makefiles which are specified as double-colon targets but have no dependencies.
If you do not specify any makefiles to be read with ‘-f’ or ‘--file’ options, make will
try the default makefile names; see Section 3.2 [What Name to Give Your Makefile], page 9.
Unlike makefiles explicitly requested with ‘-f’ or ‘--file’ options, make is not certain that
these makefiles should exist. However, if a default makefile does not exist but can be created
by running make rules, you probably want the rules to be run so that the makefile can be
used.
Therefore, if none of the default makefiles exists, make will try to make each of them in
the same order in which they are searched for (see Section 3.2 [What Name to Give Your
Makefile], page 9) until it succeeds in making one, or it runs out of names to try. Note
that it is not an error if make cannot find or make any makefile; a makefile is not always
necessary.
When you use the ‘-t’ or ‘--touch’ option (see Section 9.3 [Instead of Executing the
Commands], page 75), you would not want to use an out-of-date makefile to decide which
targets to touch. So the ‘-t’ option has no effect on updating makefiles; they are really up-
dated even if ‘-t’ is specified. Likewise, ‘-q’ (or ‘--question’) and ‘-n’ (or ‘--just-print’)
do not prevent updating of makefiles, because an out-of-date makefile would result in the
wrong output for other targets. Thus, ‘make -f mfile -n foo’ will update ‘mfile’, read
it in, and then print the commands to update ‘foo’ and its dependencies without running
them. The commands printed for ‘foo’ will be those specified in the updated contents of
‘mfile’.
However, on occasion you might actually wish to prevent updating of even the makefiles.
You can do this by specifying the makefiles as goals in the command line as well as specifying
them as makefiles. When the makefile name is specified explicitly as a goal, the options ‘-t’
and so on do apply to them.
Thus, ‘make -f mfile -n mfile foo’ would read the makefile ‘mfile’, print the com-
mands needed to update it without actually running them, and then print the commands
needed to update ‘foo’ without running them. The commands for ‘foo’ will be those
specified by the existing contents of ‘mfile’.
Chapter 3: Writing Makefiles 13
%: force
@$(MAKE) -f Makefile $@
force: ;
If you say ‘make foo’, make will find ‘GNUmakefile’, read it, and see that to make ‘foo’,
it needs to run the command ‘frobnicate > foo’. If you say ‘make bar’, make will find no
way to make ‘bar’ in ‘GNUmakefile’, so it will use the commands from the pattern rule:
‘make -f Makefile bar’. If ‘Makefile’ provides a rule for updating ‘bar’, make will apply
the rule. And likewise for any other target that ‘GNUmakefile’ does not say how to make.
The way this works is that the pattern rule has a pattern of just ‘%’, so it matches any
target whatever. The rule specifies a dependency ‘force’, to guarantee that the commands
will be run even if the target file already exists. We give ‘force’ target empty commands
to prevent make from searching for an implicit rule to build it—otherwise it would apply
the same match-anything rule to ‘force’ itself and create a dependency loop!
14 GNU make
Chapter 4: Writing Rules 15
4 Writing Rules
A rule appears in the makefile and says when and how to remake certain files, called the
rule’s targets (most often only one per rule). It lists the other files that are the dependencies
of the target, and commands to use to create or update the target.
The order of rules is not significant, except for determining the default goal: the target
for make to consider, if you do not otherwise specify one. The default goal is the target of
the first rule in the first makefile. If the first rule has multiple targets, only the first target
is taken as the default. There are two exceptions: a target starting with a period is not
a default unless it contains one or more slashes, ‘/’, as well; and, a target that defines a
pattern rule has no effect on the default goal. (See Section 10.5 [Defining and Redefining
Pattern Rules], page 90.)
Therefore, we usually write the makefile so that the first rule is the one for compiling
the entire program or all the programs described by the makefile (often with a target called
‘all’). See Section 9.2 [Arguments to Specify the Goals], page 73.
then the value of the variable objects is the actual string ‘*.o’. However, if you use the
value of objects in a target, dependency or command, wildcard expansion will take place
at that time. To set objects to the expansion, instead use:
objects := $(wildcard *.o)
See Section 4.2.3 [Wildcard Function], page 17.
foo : $(objects)
cc -o foo $(CFLAGS) $(objects)
The value of objects is the actual string ‘*.o’. Wildcard expansion happens in the rule for
‘foo’, so that each existing ‘.o’ file becomes a dependency of ‘foo’ and will be recompiled
if necessary.
But what if you delete all the ‘.o’ files? When a wildcard matches no files, it is left as
it is, so then ‘foo’ will depend on the oddly-named file ‘*.o’. Since no such file is likely to
exist, make will give you an error saying it cannot figure out how to make ‘*.o’. This is not
what you want!
Actually it is possible to obtain the desired result with wildcard expansion, but you need
more sophisticated techniques, including the wildcard function and string substitution.
These are described in the following section.
Microsoft operating systems (MS-DOS and MS-Windows) use backslashes to separate
directories in pathnames, like so:
c:\foo\bar\baz.c
This is equivalent to the Unix-style ‘c:/foo/bar/baz.c’ (the ‘c:’ part is the so-called
drive letter). When make runs on these systems, it supports backslashes as well as the Unix-
style forward slashes in pathnames. However, this support does not include the wildcard
expansion, where backslash is a quote character. Therefore, you must use Unix-style slashes
in these cases.
Note that this is different from how unmatched wildcards behave in rules, where they are
used verbatim rather than ignored (see Section 4.2.2 [Wildcard Pitfall], page 17).
One use of the wildcard function is to get a list of all the C source files in a directory,
like this:
$(wildcard *.c)
We can change the list of C source files into a list of object files by replacing the ‘.c’
suffix with ‘.o’ in the result, like this:
$(patsubst %.c,%.o,$(wildcard *.c))
(Here we have used another function, patsubst. See Section 8.2 [Functions for String
Substitution and Analysis], page 64.)
Thus, a makefile to compile all C source files in the directory and then link them together
could be written as follows:
objects := $(patsubst %.c,%.o,$(wildcard *.c))
foo : $(objects)
cc -o foo $(objects)
(This takes advantage of the implicit rule for compiling C programs, so there is no need to
write explicit rules for compiling the files. See Section 6.2 [The Two Flavors of Variables],
page 46, for an explanation of ‘:=’, which is a variant of ‘=’.)
VPATH = src:../headers
specifies a path containing two directories, ‘src’ and ‘../headers’, which make searches in
that order.
With this value of VPATH, the following rule,
foo.o : foo.c
is interpreted as if it were written like this:
foo.o : src/foo.c
assuming the file ‘foo.c’ does not exist in the current directory but is found in the directory
‘src’.
If, in fact, this is the behavior you want for some or all of your directories, you can use
the GPATH variable to indicate this to make.
GPATH has the same syntax and format as VPATH (that is, a space- or colon-delimited list
of pathnames). If an out-of-date target is found by directory search in a directory that also
appears in GPATH, then that pathname is not thrown away. The target is rebuilt using the
expanded path.
When a dependency’s name has the form ‘-lname’, make handles it specially by searching
for the file ‘libname.a’ in the current directory, in directories specified by matching vpath
search paths and the VPATH search path, and then in the directories ‘/lib’, ‘/usr/lib’,
and ‘prefix/lib’ (normally ‘/usr/local/lib’, but MS-DOS/MS-Windows versions of make
behave as if prefix is defined to be the root of the DJGPP installation tree).
For example,
foo : foo.c -lcurses
cc $^ -o $@
would cause the command ‘cc foo.c /usr/lib/libcurses.a -o foo’ to be executed when
‘foo’ is older than ‘foo.c’ or than ‘/usr/lib/libcurses.a’.
Phony targets can have dependencies. When one directory contains multiple programs,
it is most convenient to describe all of the programs in one makefile ‘./Makefile’. Since
the target remade by default will be the first one in the makefile, it is common to make this
a phony target named ‘all’ and give it, as dependencies, all the individual programs. For
example:
all : prog1 prog2 prog3
.PHONY : all
prog2 : prog2.o
cc -o prog2 prog2.o
cleanobj :
rm *.o
cleandiff :
rm *.diff
Using ‘.PHONY’ is more explicit and more efficient. However, other versions of make do
not support ‘.PHONY’; thus ‘FORCE’ appears in many makefiles. See Section 4.4 [Phony
Targets], page 22.
the target is not deleted. See Section 5.5 [Interrupting or Killing make], page 37.
Also, if the target is an intermediate file, it will not be deleted after it is no
longer needed, as is normally done. See Section 10.4 [Chains of Implicit Rules],
page 89.
You can also list the target pattern of an implicit rule (such as ‘%.o’) as a
dependency file of the special target .PRECIOUS to preserve intermediate files
created by rules whose target patterns match that file’s name.
.INTERMEDIATE
The targets which .INTERMEDIATE depends on are treated as intermediate files.
See Section 10.4 [Chains of Implicit Rules], page 89. .INTERMEDIATE with no
dependencies marks all file targets mentioned in the makefile as intermediate.
.SECONDARY
The targets which .SECONDARY depends on are treated as intermediate files,
except that they are never automatically deleted. See Section 10.4 [Chains of
Implicit Rules], page 89.
.SECONDARY with no dependencies marks all file targets mentioned in the make-
file as secondary.
.IGNORE
If you specify dependencies for .IGNORE, then make will ignore errors in execu-
tion of the commands run for those particular files. The commands for .IGNORE
are not meaningful.
If mentioned as a target with no dependencies, .IGNORE says to ignore errors in
execution of commands for all files. This usage of ‘.IGNORE’ is supported only
for historical compatibility. Since this affects every command in the makefile,
it is not very useful; we recommend you use the more selective ways to ignore
errors in specific commands. See Section 5.4 [Errors in Commands], page 36.
.SILENT
If you specify dependencies for .SILENT, then make will not the print commands
to remake those particular files before executing them. The commands for
.SILENT are not meaningful.
If mentioned as a target with no dependencies, .SILENT says not to print any
commands before executing them. This usage of ‘.SILENT’ is supported only
for historical compatibility. We recommend you use the more selective ways to
silence specific commands. See Section 5.1 [Command Echoing], page 33. If
you want to silence all commands for a particular run of make, use the ‘-s’ or
‘--silent’ option (see Section 9.7 [Options Summary], page 78).
.EXPORT_ALL_VARIABLES
Simply by being mentioned as a target, this tells make to export all variables
to child processes by default. See Section 5.6.2 [Communicating Variables to a
Sub-make], page 38.
Any defined implicit rule suffix also counts as a special target if it appears as a target,
and so does the concatenation of two suffixes, such as ‘.c.o’. These targets are suffix rules,
26 GNU make
an obsolete way of defining implicit rules (but a way still widely used). In principle, any
target name could be special in this way if you break it in two and add both pieces to the
suffix list. In practice, suffixes normally begin with ‘.’, so these special target names also
begin with ‘.’. See Section 10.7 [Old-Fashioned Suffix Rules], page 96.
An extra rule with just dependencies can be used to give a few extra dependencies to
many files at once. For example, one usually has a variable named objects containing a
list of all the compiler output files in the system being made. An easy way to say that all
of them must be recompiled if ‘config.h’ changes is to write the following:
objects = foo.o bar.o
foo.o : defs.h
bar.o : defs.h test.h
$(objects) : config.h
This could be inserted or taken out without changing the rules that really specify how to
make the object files, making it a convenient form to use if you wish to add the additional
dependency intermittently.
Another wrinkle is that the additional dependencies could be specified with a variable
that you set with a command argument to make (see Section 9.5 [Overriding Variables],
page 77). For example,
extradeps=
$(objects) : $(extradeps)
means that the command ‘make extradeps=foo.h’ will consider ‘foo.h’ as a dependency
of each object file, but plain ‘make’ will not.
If none of the explicit rules for a target has commands, then make searches for an appli-
cable implicit rule to find some commands see Chapter 10 [Using Implicit Rules], page 83).
the pattern ‘%.o’, with ‘foo’ as the stem. The targets ‘foo.c’ and ‘foo.out’ do not match
that pattern.
The dependency names for each target are made by substituting the stem for the ‘%’ in
each dependency pattern. For example, if one dependency pattern is ‘%.c’, then substitution
of the stem ‘foo’ gives the dependency name ‘foo.c’. It is legitimate to write a dependency
pattern that does not contain ‘%’; then this dependency is the same for all targets.
‘%’ characters in pattern rules can be quoted with preceding backslashes (‘\’). Back-
slashes that would otherwise quote ‘%’ characters can be quoted with more backslashes.
Backslashes that quote ‘%’ characters or other backslashes are removed from the pattern
before it is compared to file names or has a stem substituted into it. Backslashes that
are not in danger of quoting ‘%’ characters go unmolested. For example, the pattern
‘the\%weird\\%pattern\\’ has ‘the%weird\’ preceding the operative ‘%’ character, and
‘pattern\\’ following it. The final two backslashes are left alone because they cannot
affect any ‘%’ character.
Here is an example, which compiles each of ‘foo.o’ and ‘bar.o’ from the corresponding
‘.c’ file:
objects = foo.o bar.o
all: $(objects)
include $(sources:.c=.d)
(This example uses a substitution variable reference to translate the list of source files ‘foo.c
bar.c’ into a list of dependency makefiles, ‘foo.d bar.d’. See Section 6.3.1 [Substitution
Refs], page 48, for full information on substitution references.) Since the ‘.d’ files are
makefiles like any others, make will remake them as necessary with no further work from
you. See Section 3.5 [Remaking Makefiles], page 11.
32 GNU make
Chapter 5: Writing the Commands in Rules 33
will consider them a single command and pass them, together, to a shell which will execute
them in sequence. For example:
foo : bar/lose
cd bar; gobble lose > ../foo
If you would like to split a single shell command into multiple lines of text, you must
use a backslash at the end of all but the last subline. Such a sequence of lines is combined
into a single line, by deleting the backslash-newline sequences, before passing it to the shell.
Thus, the following is equivalent to the preceding example:
foo : bar/lose
cd bar; \
gobble lose > ../foo
The program used as the shell is taken from the variable SHELL. By default, the program
‘/bin/sh’ is used.
On MS-DOS, if SHELL is not set, the value of the variable COMSPEC (which is always set)
is used instead.
The processing of lines that set the variable SHELL in Makefiles is different on MS-DOS.
The stock shell, ‘command.com’, is ridiculously limited in its functionality and many users of
make tend to install a replacement shell. Therefore, on MS-DOS, make examines the value
of SHELL, and changes its behavior based on whether it points to a Unix-style or DOS-style
shell. This allows reasonable functionality even if SHELL points to ‘command.com’.
If SHELL points to a Unix-style shell, make on MS-DOS additionally checks whether that
shell can indeed be found; if not, it ignores the line that sets SHELL. In MS-DOS, GNU
make searches for the shell in the following places:
1. In the precise place pointed to by the value of SHELL. For example, if the makefile
specifies ‘SHELL = /bin/sh’, make will look in the directory ‘/bin’ on the current drive.
2. In the current directory.
3. In each of the directories in the PATH variable, in order.
In every directory it examines, make will first look for the specific file (‘sh’ in the example
above). If this is not found, it will also look in that directory for that file with one of the
known extensions which identify executable files. For example ‘.exe’, ‘.com’, ‘.bat’, ‘.btm’,
‘.sh’, and some others.
If any of these attempts is successful, the value of SHELL will be set to the full pathname
of the shell as found. However, if none of these is found, the value of SHELL will not be
changed, and thus the line that sets it will be effectively ignored. This is so make will only
support features specific to a Unix-style shell if such a shell is actually installed on the
system where make runs.
Note that this extended search for the shell is limited to the cases where SHELL is set
from the Makefile; if it is set in the environment or command line, you are expected to set
it to the full pathname of the shell, exactly as things are on Unix.
The effect of the above DOS-specific processing is that a Makefile that says ‘SHELL =
/bin/sh’ (as many Unix makefiles do), will work on MS-DOS unaltered if you have e.g.
‘sh.exe’ installed in some directory along your PATH.
Chapter 5: Writing the Commands in Rules 35
Unlike most variables, the variable SHELL is never set from the environment. This is
because the SHELL environment variable is used to specify your personal choice of shell
program for interactive use. It would be very bad for personal choices like this to affect
the functioning of makefiles. See Section 6.9 [Variables from the Environment], page 55.
However, on MS-DOS and MS-Windows the value of SHELL in the environment is used,
since on those systems most users do not set this variable, and therefore it is most likely
set specifically to be used by make. On MS-DOS, if the setting of SHELL is not suitable
for make, you can set the variable MAKESHELL to the shell that make should use; this will
override the value of SHELL.
run at once, based on the load average. The ‘-l’ or ‘--max-load’ option is followed by a
floating-point number. For example,
-l 2.5
will not let make start more than one job if the load average is above 2.5. The ‘-l’ option
with no following number removes the load limit, if one was given with a previous ‘-l’
option.
More precisely, when make goes to start up a job, and it already has at least one job
running, it checks the current load average; if it is not lower than the limit given with ‘-l’,
make waits until the load average goes below that limit, or until all the other jobs finish.
By default, there is no load limit.
The usual behavior assumes that your purpose is to get the specified targets up to date;
once make learns that this is impossible, it might as well report the failure immediately.
The ‘-k’ option says that the real purpose is to test as many of the changes made in the
program as possible, perhaps to find several independent problems so that you can correct
them all before the next attempt to compile. This is why Emacs’ compile command passes
the ‘-k’ flag by default.
Usually when a command fails, if it has changed the target file at all, the file is corrupted
and cannot be used—or at least it is not completely updated. Yet the file’s timestamp says
that it is now up to date, so the next time make runs, it will not try to update that file.
The situation is just the same as when the command is killed by a signal; see Section 5.5
[Interrupts], page 37. So generally the right thing to do is to delete the target file if the
command fails after beginning to change the file. make will do this if .DELETE_ON_ERROR
appears as a target. This is almost always what you want make to do, but it is not historical
practice; so for compatibility, you must explicitly request it.
subsystem:
$(MAKE) -C subdir
You can write recursive make commands just by copying this example, but there are
many things to know about how they work and why, and about how the sub-make relates
to the top-level make.
For your convenience, GNU make sets the variable CURDIR to the pathname of the current
working directory for you. If -C is in effect, it will contain the path of the new directory, not
the original. The value has the same precedence it would have if it were set in the makefile
(by default, an environment variable CURDIR will not override this value). Note that setting
this variable has no effect on the operation of make
To pass down, or export, a variable, make adds the variable and its value to the environ-
ment for running each command. The sub-make, in turn, uses the environment to initialize
its table of variable values. See Section 6.9 [Variables from the Environment], page 55.
Except by explicit request, make exports a variable only if it is either defined in the
environment initially or set on the command line, and if its name consists only of let-
ters, numbers, and underscores. Some shells cannot cope with environment variable names
consisting of characters other than letters, numbers, and underscores.
The special variables SHELL and MAKEFLAGS are always exported (unless you unexport
them). MAKEFILES is exported if you set it to anything.
make automatically passes down variable values that were defined on the command line,
by putting them in the MAKEFLAGS variable. See the next section.
Variables are not normally passed down if they were created by default by make (see
Section 10.3 [Variables Used by Implicit Rules], page 87). The sub-make will define these
for itself.
If you want to export specific variables to a sub-make, use the export directive, like this:
export variable . . .
If you want to prevent a variable from being exported, use the unexport directive, like this:
unexport variable . . .
As a convenience, you can define a variable and export it at the same time by doing:
export variable = value
has the same result as:
variable = value
export variable
and
export variable := value
has the same result as:
variable := value
export variable
Likewise,
export variable += value
is just like:
variable += value
export variable
See Section 6.6 [Appending More Text to Variables], page 52.
You may notice that the export and unexport directives work in make in the same way
they work in the shell, sh.
If you want all variables to be exported by default, you can use export by itself:
export
This tells make that variables which are not explicitly mentioned in an export or unexport
directive should be exported. Any variable given in an unexport directive will still not be
exported. If you use export by itself to export variables by default, variables whose names
40 GNU make
contain characters other than alphanumerics and underscores will not be exported unless
specifically mentioned in an export directive.
The behavior elicited by an export directive by itself was the default in older versions of
GNU make. If your makefiles depend on this behavior and you want to be compatible with
old versions of make, you can write a rule for the special target .EXPORT_ALL_VARIABLES
instead of using the export directive. This will be ignored by old makes, while the export
directive will cause a syntax error.
Likewise, you can use unexport by itself to tell make not to export variables by default.
Since this is the default behavior, you would only need to do this if export had been used
by itself earlier (in an included makefile, perhaps). You cannot use export and unexport
by themselves to have variables exported for some commands and not for others. The last
export or unexport directive that appears by itself determines the behavior for the entire
run of make.
As a special feature, the variable MAKELEVEL is changed when it is passed down from
level to level. This variable’s value is a string which is the depth of the level as a decimal
number. The value is ‘0’ for the top-level make; ‘1’ for a sub-make, ‘2’ for a sub-sub-make,
and so on. The incrementation happens when make sets up the environment for a command.
The main use of MAKELEVEL is to test it in a conditional directive (see Chapter 7 [Con-
ditional Parts of Makefiles], page 59); this way you can write a makefile that behaves one
way if run recursively and another way if run directly by you.
You can use the variable MAKEFILES to cause all sub-make commands to use additional
makefiles. The value of MAKEFILES is a whitespace-separated list of file names. This variable,
if defined in the outer-level makefile, is passed down through the environment; then it serves
as a list of extra makefiles for the sub-make to read before the usual or specified ones. See
Section 3.4 [The Variable MAKEFILES], page 11.
argument, meaning to run as many jobs as possible in parallel, this is passed down, since
multiple infinities are no more than one.
If you do not want to pass the other flags down, you must change the value of MAKEFLAGS,
like this:
subsystem:
cd subdir && $(MAKE) MAKEFLAGS=
The command line variable definitions really appear in the variable MAKEOVERRIDES,
and MAKEFLAGS contains a reference to this variable. If you do want to pass flags down
normally, but don’t want to pass down the command line variable definitions, you can reset
MAKEOVERRIDES to empty, like this:
MAKEOVERRIDES =
This is not usually useful to do. However, some systems have a small fixed limit on the size
of the environment, and putting so much information in into the value of MAKEFLAGS can
exceed it. If you see the error message ‘Arg list too long’, this may be the problem. (For
strict compliance with POSIX.2, changing MAKEOVERRIDES does not affect MAKEFLAGS if the
special target ‘.POSIX’ appears in the makefile. You probably do not care about this.)
A similar variable MFLAGS exists also, for historical compatibility. It has the same
value as MAKEFLAGS except that it does not contain the command line variable defini-
tions, and it always begins with a hyphen unless it is empty (MAKEFLAGS begins with
a hyphen only when it begins with an option that has no single-letter version, such as
‘--warn-undefined-variables’). MFLAGS was traditionally used explicitly in the recursive
make command, like this:
subsystem:
cd subdir && $(MAKE) $(MFLAGS)
but now MAKEFLAGS makes this usage redundant. If you want your makefiles to be compat-
ible with old make programs, use this technique; it will work fine with more modern make
versions too.
The MAKEFLAGS variable can also be useful if you want to have certain options, such as
‘-k’ (see Section 9.7 [Summary of Options], page 78), set each time you run make. You
simply put a value for MAKEFLAGS in your environment. You can also set MAKEFLAGS in a
makefile, to specify additional flags that should also be in effect for that makefile. (Note
that you cannot use MFLAGS this way. That variable is set only for compatibility; make does
not interpret a value you set for it in any way.)
When make interprets the value of MAKEFLAGS (either from the environment or from a
makefile), it first prepends a hyphen if the value does not already begin with one. Then
it chops the value into words separated by blanks, and parses these words as if they were
options given on the command line (except that ‘-C’, ‘-f’, ‘-h’, ‘-o’, ‘-W’, and their long-
named versions are ignored; and there is no error for an invalid option).
If you do put MAKEFLAGS in your environment, you should be sure not to include any
options that will drastically affect the actions of make and undermine the purpose of make-
files and of make itself. For instance, the ‘-t’, ‘-n’, and ‘-q’ options, if put in one of these
variables, could have disastrous consequences and would certainly have at least surprising
and probably annoying effects.
42 GNU make
In command execution, each line of a canned sequence is treated just as if the line
appeared on its own in the rule, preceded by a tab. In particular, make invokes a separate
subshell for each line. You can use the special prefix characters that affect command lines
(‘@’, ‘-’, and ‘+’) on each line of a canned sequence. See Chapter 5 [Writing the Commands
in Rules], page 33. For example, using this canned sequence:
define frobnicate
@echo "frobnicating target $@"
frob-step-1 $< -o $@-step-1
frob-step-2 $@-step-1 -o $@
endef
make will not echo the first line, the echo command. But it will echo the following two
command lines.
On the other hand, prefix characters on the command line that refers to a canned
sequence apply to every line in the sequence. So the rule:
frob.out: frob.in
@$(frobnicate)
does not echo any commands. (See Section 5.1 [Command Echoing], page 33, for a full
explanation of ‘@’.)
$(objects) : defs.h
Variable references work by strict textual substitution. Thus, the rule
foo = c
prog.o : prog.$(foo)
$(foo)$(foo) -$(foo) prog.$(foo)
46 GNU make
could be used to compile a C program ‘prog.c’. Since spaces before the variable value are
ignored in variable assignments, the value of foo is precisely ‘c’. (Don’t actually write your
makefiles this way!)
A dollar sign followed by a character other than a dollar sign, open-parenthesis or open-
brace treats that single character as the variable name. Thus, you could reference the
variable x with ‘$x’. However, this practice is strongly discouraged, except in the case of
the automatic variables (see Section 10.5.3 [Automatic Variables], page 92).
all:;echo $(foo)
will echo ‘Huh?’: ‘$(foo)’ expands to ‘$(bar)’ which expands to ‘$(ugh)’ which finally
expands to ‘Huh?’.
This flavor of variable is the only sort supported by other versions of make. It has its
advantages and its disadvantages. An advantage (most would say) is that:
CFLAGS = $(include_dirs) -O
include_dirs = -Ifoo -Ibar
will do what was intended: when ‘CFLAGS’ is expanded in a command, it will expand to
‘-Ifoo -Ibar -O’. A major disadvantage is that you cannot append something on the end
of a variable, as in
CFLAGS = $(CFLAGS) -O
because it will cause an infinite loop in the variable expansion. (Actually make detects the
infinite loop and reports an error.)
Another disadvantage is that any functions (see Chapter 8 [Functions for Transforming
Text], page 63) referenced in the definition will be executed every time the variable is
expanded. This makes make run slower; worse, it causes the wildcard and shell functions
to give unpredictable results because you cannot easily control when they are called, or
even how many times.
To avoid all the problems and inconveniences of recursively expanded variables, there is
another flavor: simply expanded variables.
Chapter 6: How to Use Variables 47
Simply expanded variables are defined by lines using ‘:=’ (see Section 6.5 [Setting Vari-
ables], page 51). The value of a simply expanded variable is scanned once and for all,
expanding any references to other variables and functions, when the variable is defined.
The actual value of the simply expanded variable is the result of expanding the text that
you write. It does not contain any references to other variables; it contains their values as
of the time this variable was defined. Therefore,
x := foo
y := $(x) bar
x := later
is equivalent to
y := foo bar
x := later
When a simply expanded variable is referenced, its value is substituted verbatim.
Here is a somewhat more complicated example, illustrating the use of ‘:=’ in conjunction
with the shell function. (See Section 8.6 [The shell Function], page 71.) This example
also shows use of the variable MAKELEVEL, which is changed when it is passed down from
level to level. (See Section 5.6.2 [Communicating Variables to a Sub-make], page 38, for
information about MAKELEVEL.)
ifeq (0,${MAKELEVEL})
cur-dir := $(shell pwd)
whoami := $(shell whoami)
host-type := $(shell arch)
MAKE := ${MAKE} host-type=${host-type} whoami=${whoami}
endif
An advantage of this use of ‘:=’ is that a typical ‘descend into a directory’ command then
looks like this:
${subdirs}:
${MAKE} cur-dir=${cur-dir}/$@ -C $@ all
Simply expanded variables generally make complicated makefile programming more pre-
dictable because they work like variables in most programming languages. They allow you
to redefine a variable using its own value (or its value processed in some way by one of
the expansion functions) and to use the expansion functions much more efficiently (see
Chapter 8 [Functions for Transforming Text], page 63).
You can also use them to introduce controlled leading whitespace into variable values.
Leading whitespace characters are discarded from your input before substitution of variable
references and function calls; this means you can include leading spaces in a variable value
by protecting them with variable references, like this:
nullstring :=
space := $(nullstring) # end of the line
Here the value of the variable space is precisely one space. The comment ‘# end of the line’
is included here just for clarity. Since trailing space characters are not stripped from vari-
able values, just a space at the end of the line would have the same effect (but be rather
hard to read). If you put whitespace at the end of a variable value, it is a good idea to put
a comment like that at the end of the line to make your intent clear. Conversely, if you do
48 GNU make
not want any whitespace characters at the end of your variable value, you must remember
not to put a random comment on the end of the line after some whitespace, such as this:
dir := /foo/bar # directory to put the frobs in
Here the value of the variable dir is ‘/foo/bar ’ (with four trailing spaces), which was
probably not the intention. (Imagine something like ‘$(dir)/file’ with this definition!)
There is another assignment operator for variables, ‘?=’. This is called a conditional
variable assignment operator, because it only has an effect if the variable is not yet defined.
This statement:
FOO ?= bar
is exactly equivalent to this (see Section 8.5 [The origin Function], page 70):
ifeq ($(origin FOO), undefined)
FOO = bar
endif
Note that a variable set to an empty value is still defined, so ‘?=’ will not set that
variable.
For example:
x = variable1
variable2 := Hello
y = $(subst 1,2,$(x))
z = y
a := $($($(z)))
eventually defines a as ‘Hello’. It is doubtful that anyone would ever want to write a nested
reference as convoluted as this one, but it works: ‘$($($(z)))’ expands to ‘$($(y))’ which
becomes ‘$($(subst 1,2,$(x)))’. This gets the value ‘variable1’ from x and changes it
by substitution to ‘variable2’, so that the entire string becomes ‘$(variable2)’, a simple
variable reference whose value is ‘Hello’.
A computed variable name need not consist entirely of a single variable reference. It can
contain several variable references, as well as some invariant text. For example,
a_dirs := dira dirb
1_dirs := dir1 dir2
a_files := filea fileb
1_files := file1 file2
ifeq "$(use_a)" "yes"
a1 := a
else
a1 := 1
endif
ifeq "$(use_dirs)" "yes"
df := dirs
else
df := files
endif
dirs := $($(a1)_$(df))
will give dirs the same value as a_dirs, 1_dirs, a_files or 1_files depending on the
settings of use_a and use_dirs.
Computed variable names can also be used in substitution references:
a_objects := a.o b.o c.o
1_objects := 1.o 2.o 3.o
sources := $($(a1)_objects:.o=.c)
defines sources as either ‘a.c b.c c.c’ or ‘1.c 2.c 3.c’, depending on the value of a1.
The only restriction on this sort of use of nested variable references is that they cannot
specify part of the name of a function to be called. This is because the test for a recognized
function name is done before the expansion of nested references. For example,
ifdef do_sort
func := sort
else
func := strip
endif
bar := a d b g q c
Chapter 6: How to Use Variables 51
The variable name may contain function and variable references, which are expanded
when the line is read to find the actual variable name to use.
There is no limit on the length of the value of a variable except the amount of swapping
space on the computer. When a variable definition is long, it is a good idea to break it into
several lines by inserting backslash-newline at convenient places in the definition. This will
not affect the functioning of make, but it will make the makefile easier to read.
Most variable names are considered to have the empty string as a value if you have never
set them. Several variables have built-in initial values that are not empty, but you can set
them in the usual ways (see Section 10.3 [Variables Used by Implicit Rules], page 87).
Several special variables are set automatically to a new value for each rule; these are called
the automatic variables (see Section 10.5.3 [Automatic Variables], page 92).
If you’d like a variable to be set to a value only if it’s not already set, then you can
use the shorthand operator ‘?=’ instead of ‘=’. These two settings of the variable ‘FOO’ are
identical (see Section 8.5 [The origin Function], page 70):
FOO ?= bar
and
ifeq ($(origin FOO), undefined)
FOO = bar
endif
variable := value
variable += more
is exactly equivalent to:
variable := value
variable := $(variable) more
On the other hand, when you use ‘+=’ with a variable that you defined first to be
recursively-expanded using plain ‘=’, make does something a bit different. Recall that when
you define a recursively-expanded variable, make does not expand the value you set for
variable and function references immediately. Instead it stores the text verbatim, and saves
these variable and function references to be expanded later, when you refer to the new
variable (see Section 6.2 [The Two Flavors of Variables], page 46). When you use ‘+=’ on
a recursively-expanded variable, it is this unexpanded text to which make appends the new
text you specify.
variable = value
variable += more
is roughly equivalent to:
temp = value
variable = $(temp) more
except that of course it never defines a variable called temp. The importance of this comes
when the variable’s old value contains variable references. Take this common example:
CFLAGS = $(includes) -O
...
CFLAGS += -pg # enable profiling
The first line defines the CFLAGS variable with a reference to another variable, includes.
(CFLAGS is used by the rules for C compilation; see Section 10.2 [Catalogue of Implicit
Rules], page 84.) Using ‘=’ for the definition makes CFLAGS a recursively-expanded variable,
meaning ‘$(includes) -O’ is not expanded when make processes the definition of CFLAGS.
Thus, includes need not be defined yet for its value to take effect. It only has to be defined
before any reference to CFLAGS. If we tried to append to the value of CFLAGS without using
‘+=’, we might do it like this:
CFLAGS := $(CFLAGS) -pg # enable profiling
This is pretty close, but not quite what we want. Using ‘:=’ redefines CFLAGS as a simply-
expanded variable; this means make expands the text ‘$(CFLAGS) -pg’ before setting the
variable. If includes is not yet defined, we get ‘ -O -pg’, and a later definition of includes
will have no effect. Conversely, by using ‘+=’ we set CFLAGS to the unexpanded value
‘$(includes) -O -pg’. Thus we preserve the reference to includes, so if that variable gets
defined at any later point, a reference like ‘$(CFLAGS)’ still uses its value.
since two commands separated by semicolon behave much like two separate shell commands.
However, note that using two separate lines means make will invoke the shell twice, running
an independent subshell for each line. See Section 5.2 [Command Execution], page 33.
If you want variable definitions made with define to take precedence over command-line
variable definitions, you can use the override directive together with define:
override define two-lines
foo
$(bar)
endef
See Section 6.7 [The override Directive], page 53.
The other exception is target-specific variable values. This feature allows you to define
different values for the same variable, based on the target that make is currently building.
As with automatic variables, these values are only available within the context of a target’s
command script (and in other target-specific assignments).
Set a target-specific variable value like this:
target . . . : variable-assignment
or like this:
target . . . : override variable-assignment
Multiple target values create a target-specific variable value for each member of the
target list individually.
The variable-assignment can be any valid form of assignment; recursive (‘=’), static
(‘:=’), appending (‘+=’), or conditional (‘?=’). All variables that appear within the variable-
assignment are evaluated within the context of the target: thus, any previously-defined
target-specific variable values will be in effect. Note that this variable is actually distinct
from any “global” value: the two variables do not have to have the same flavor (recursive
vs. static).
Target-specific variables have the same priority as any other makefile variable. Variables
provided on the command-line (and in the environment if the ‘-e’ option is in force) will
take precedence. Specifying the override directive will allow the target-specific variable
value to be preferred.
There is one more special feature of target-specific variables: when you define a target-
specific variable, that variable value is also in effect for all dependencies of this target
(unless those dependencies override it with their own target-specific variable value). So, for
example, a statement like this:
prog : CFLAGS = -g
prog : prog.o foo.o bar.o
will set CFLAGS to ‘-g’ in the command script for ‘prog’, but it will also set CFLAGS to ‘-g’ in
the command scripts that create ‘prog.o’, ‘foo.o’, and ‘bar.o’, and any command scripts
which create their dependencies.
foo: $(objects)
ifeq ($(CC),gcc)
$(CC) -o foo $(objects) $(libs_for_gcc)
else
$(CC) -o foo $(objects) $(normal_libs)
endif
This conditional uses three directives: one ifeq, one else and one endif.
The ifeq directive begins the conditional, and specifies the condition. It contains two
arguments, separated by a comma and surrounded by parentheses. Variable substitution
is performed on both arguments and then they are compared. The lines of the makefile
following the ifeq are obeyed if the two arguments match; otherwise they are ignored.
The else directive causes the following lines to be obeyed if the previous conditional
failed. In the example above, this means that the second alternative linking command
is used whenever the first alternative is not used. It is optional to have an else in a
conditional.
The endif directive ends the conditional. Every conditional must end with an endif.
Unconditional makefile text follows.
As this example illustrates, conditionals work at the textual level: the lines of the con-
ditional are treated as part of the makefile, or ignored, according to the condition. This is
why the larger syntactic units of the makefile, such as rules, may cross the beginning or the
end of the conditional.
When the variable CC has the value ‘gcc’, the above example has this effect:
foo: $(objects)
$(CC) -o foo $(objects) $(libs_for_gcc)
When the variable CC has any other value, the effect is this:
foo: $(objects)
$(CC) -o foo $(objects) $(normal_libs)
60 GNU make
ifeq ($(CC),gcc)
libs=$(libs_for_gcc)
else
libs=$(normal_libs)
endif
foo: $(objects)
$(CC) -o foo $(objects) $(libs)
except within the directive name or within an argument. A comment starting with ‘#’ may
appear at the end of the line.
The other two directives that play a part in a conditional are else and endif. Each of
these directives is written as one word, with no arguments. Extra spaces are allowed and
ignored at the beginning of the line, and spaces or tabs at the end. A comment starting
with ‘#’ may appear at the end of the line.
Conditionals affect which lines of the makefile make uses. If the condition is true, make
reads the lines of the text-if-true as part of the makefile; if the condition is false, make
ignores those lines completely. It follows that syntactic units of the makefile, such as rules,
may safely be split across the beginning or the end of the conditional.
make evaluates conditionals when it reads a makefile. Consequently, you cannot use
automatic variables in the tests of conditionals because they are not defined until commands
are run (see Section 10.5.3 [Automatic Variables], page 92).
To prevent intolerable confusion, it is not permitted to start a conditional in one makefile
and end it in another. However, you may write an include directive within a conditional,
provided you do not attempt to terminate the conditional inside the included file.
Functions allow you to do text processing in the makefile to compute the files to operate
on or the commands to use. You use a function in a function call, where you give the name
of the function and some text (the arguments) for the function to operate on. The result
of the function’s processing is substituted into the makefile at the point of the call, just as
a variable might be substituted.
$(patsubst %.o,%.c,$(objects))
$(strip string)
Removes leading and trailing whitespace from string and replaces each inter-
nal sequence of one or more whitespace characters with a single space. Thus,
‘$(strip a b c )’ results in ‘a b c’.
The function strip can be very useful when used in conjunction with condi-
tionals. When comparing something with the empty string ‘’ using ifeq or
ifneq, you usually want a string of just whitespace to match the empty string
(see Chapter 7 [Conditionals], page 59).
Thus, the following may fail to have the desired results:
.PHONY: all
ifneq "$(needs_made)" ""
all: $(needs_made)
else
all:;@echo ’Nothing to make!’
endif
Replacing the variable reference ‘$(needs_made)’ with the function call ‘$(strip $(needs_made
in the ifneq directive would make it more robust.
$(findstring find,in)
Searches in for an occurrence of find. If it occurs, the value is find; otherwise,
the value is empty. You can use this function in a conditional to test for the
presence of a specific substring in a given string. Thus, the two examples,
$(findstring a,a b c)
$(findstring a,b c)
produce the values ‘a’ and ‘’ (the empty string), respectively. See Section 7.3
[Testing Flags], page 62, for a practical application of findstring.
$(filter pattern. . .,text)
Removes all whitespace-separated words in text that do not match any of the
pattern words, returning only matching words. The patterns are written using
‘%’, just like the patterns used in the patsubst function above.
The filter function can be used to separate out different types of strings (such
as file names) in a variable. For example:
sources := foo.c bar.c baz.s ugh.h
foo: $(sources)
cc $(filter %.c %.s,$(sources)) -o foo
says that ‘foo’ depends of ‘foo.c’, ‘bar.c’, ‘baz.s’ and ‘ugh.h’ but only
‘foo.c’, ‘bar.c’ and ‘baz.s’ should be specified in the command to the com-
piler.
$(filter-out pattern. . .,text)
Removes all whitespace-separated words in text that do match the pattern
words, returning only the words that do not match. This is the exact opposite
of the filter function.
For example, given:
66 GNU make
$(notdir names. . .)
Extracts all but the directory-part of each file name in names. If the file name
contains no slash, it is left unchanged. Otherwise, everything through the last
slash is removed from it.
A file name that ends with a slash becomes an empty string. This is unfortunate,
because it means that the result does not always have the same number of
whitespace-separated file names as the argument had; but we do not see any
other valid alternative.
For example,
$(notdir src/foo.c hacks)
produces the result ‘foo.c hacks’.
$(suffix names. . .)
Extracts the suffix of each file name in names. If the file name contains a period,
the suffix is everything starting with the last period. Otherwise, the suffix is
the empty string. This frequently means that the result will be empty when
names is not, and if names contains multiple file names, the result may contain
fewer file names.
For example,
$(suffix src/foo.c src-1.0/bar.c hacks)
produces the result ‘.c .c’.
$(basename names. . .)
Extracts all but the suffix of each file name in names. If the file name contains
a period, the basename is everything starting up to (and not including) the last
period. Periods in the directory part are ignored. If there is no period, the
basename is the entire file name. For example,
$(basename src/foo.c src-1.0/bar hacks)
produces the result ‘src/foo src-1.0/bar hacks’.
$(addsuffix suffix,names. . .)
The argument names is regarded as a series of names, separated by whitespace;
suffix is used as a unit. The value of suffix is appended to the end of each
individual name and the resulting larger names are concatenated with single
spaces between them. For example,
$(addsuffix .c,foo bar)
produces the result ‘foo.c bar.c’.
$(addprefix prefix,names. . .)
The argument names is regarded as a series of names, separated by whitespace;
prefix is used as a unit. The value of prefix is prepended to the front of each
individual name and the resulting larger names are concatenated with single
spaces between them. For example,
$(addprefix src/,foo bar)
produces the result ‘src/foo src/bar’.
68 GNU make
$(join list1,list2)
Concatenates the two arguments word by word: the two first words (one from
each argument) concatenated form the first word of the result, the two second
words form the second word of the result, and so on. So the nth word of the
result comes from the nth word of each argument. If one argument has more
words that the other, the extra words are copied unchanged into the result.
For example, ‘$(join a b,.c .o)’ produces ‘a.c b.o’.
Whitespace between the words in the lists is not preserved; it is replaced with
a single space.
This function can merge the results of the dir and notdir functions, to produce
the original list of files which was given to those two functions.
$(word n,text)
Returns the nth word of text. The legitimate values of n start from 1. If n is
bigger than the number of words in text, the value is empty. For example,
$(word 2, foo bar baz)
returns ‘bar’.
$(wordlist s,e,text)
Returns the list of words in text starting with word s and ending with word e
(inclusive). The legitimate values of s and e start from 1. If s is bigger than
the number of words in text, the value is empty. If e is bigger than the number
of words in text, words up to the end of text are returned. If s is greater than
e, make swaps them for you. For example,
$(wordlist 2, 3, foo bar baz)
returns ‘bar baz’.
$(words text)
Returns the number of words in text. Thus, the last word of text is $(word $(words text),text)
$(firstword names. . .)
The argument names is regarded as a series of names, separated by whitespace.
The value is the first name in the series. The rest of the names are ignored.
For example,
$(firstword foo bar)
produces the result ‘foo’. Although $(firstword text) is the same as $(word
1,text), the firstword function is retained for its simplicity.
$(wildcard pattern)
The argument pattern is a file name pattern, typically containing wildcard
characters (as in shell file name patterns). The result of wildcard is a space-
separated list of the names of existing files that match the pattern. See Sec-
tion 4.2 [Using Wildcard Characters in File Names], page 16.
Chapter 8: Functions for Transforming Text 69
This information is primarily useful (other than for your curiosity) to determine if you
want to believe the value of a variable. For example, suppose you have a makefile ‘foo’ that
includes another makefile ‘bar’. You want a variable bletch to be defined in ‘bar’ if you
run the command ‘make -f bar’, even if the environment contains a definition of bletch.
However, if ‘foo’ defined bletch before including ‘bar’, you do not want to override that
definition. This could be done by using an override directive in ‘foo’, giving that definition
precedence over the later definition in ‘bar’; unfortunately, the override directive would
also override any command line definitions. So, ‘bar’ could include:
ifdef bletch
ifeq "$(origin bletch)" "environment"
bletch = barf, gag, etc.
endif
endif
If bletch has been defined from the environment, this will redefine it.
If you want to override a previous definition of bletch if it came from the environment,
even under ‘-e’, you could instead write:
ifneq "$(findstring environment,$(origin bletch))" ""
bletch = barf, gag, etc.
endif
Here the redefinition takes place if ‘$(origin bletch)’ returns either ‘environment’ or
‘environment override’. See Section 8.2 [Functions for String Substitution and Analysis],
page 64.
targets not in the makefile may be specified, if make can find implicit rules that say how to
make them.
Make will set the special variable MAKECMDGOALS to the list of goals you specified on the
command line. If no goals were given on the command line, this variable is empty. Note
that this variable should be used only in special circumstances.
An example of appropriate use is to avoid including ‘.d’ files during clean rules (see
Section 4.12 [Automatic Dependencies], page 30), so make won’t create them only to imme-
diately remove them again:
sources = foo.c bar.c
ifneq ($(MAKECMDGOALS),clean)
include $(sources:.c=.d)
endif
One use of specifying a goal is if you want to compile only a part of the program, or only
one of several programs. Specify as a goal each file that you wish to remake. For example,
consider a directory containing several programs, with a makefile that starts like this:
.PHONY: all
all: size nm ld ar as
If you are working on the program size, you might want to say ‘make size’ so that only
the files of that program are recompiled.
Another use of specifying a goal is to make files that are not normally made. For
example, there may be a file of debugging output, or a version of the program that is
compiled specially for testing, which has a rule in the makefile but is not a dependency of
the default goal.
Another use of specifying a goal is to run the commands associated with a phony target
(see Section 4.4 [Phony Targets], page 22) or empty target (see Section 4.6 [Empty Target
Files to Record Events], page 24). Many makefiles contain a phony target named ‘clean’
which deletes everything except source files. Naturally, this is done only if you request it
explicitly with ‘make clean’. Following is a list of typical phony and empty target names.
See Section 14.5 [Standard Targets], page 117, for a detailed list of all the standard target
names which GNU software packages use.
‘all’ Make all the top-level targets the makefile knows about.
‘clean’ Delete all files that are normally created by running make.
‘mostlyclean’
Like ‘clean’, but may refrain from deleting a few files that people normally
don’t want to recompile. For example, the ‘mostlyclean’ target for GCC does
not delete ‘libgcc.a’, because recompiling it is rarely necessary and takes a lot
of time.
‘distclean’
‘realclean’
‘clobber’ Any of these targets might be defined to delete more files than ‘clean’ does. For
example, this would delete configuration files or links that you would normally
create as preparation for compilation, even if the makefile itself cannot create
these files.
Chapter 9: How to Run make 75
‘install’ Copy the executable file into a directory that users typically search for com-
mands; copy any auxiliary files that the executable uses into the directories
where it will look for them.
‘print’ Print listings of the source files that have changed.
‘tar’ Create a tar file of the source files.
‘shar’ Create a shell archive (shar file) of the source files.
‘dist’ Create a distribution file of the source files. This might be a tar file, or a shar
file, or a compressed version of one of the above, or even more than one of the
above.
‘TAGS’ Update a tags table for this program.
‘check’
‘test’ Perform self tests on the program this makefile builds.
With the ‘-n’ flag, make prints the commands that it would normally execute but does
not execute them.
With the ‘-t’ flag, make ignores the commands in the rules and uses (in effect) the
command touch for each target that needs to be remade. The touch command is also
printed, unless ‘-s’ or .SILENT is used. For speed, make does not actually invoke the
program touch. It does the work directly.
With the ‘-q’ flag, make prints nothing and executes no commands, but the exit status
code it returns is zero if and only if the targets to be considered are already up to date. If
the exit status is one, then some updating needs to be done. If make encounters an error,
the exit status is two, so you can distinguish an error from a target that is not up to date.
It is an error to use more than one of these three flags in the same invocation of make.
The ‘-n’, ‘-t’, and ‘-q’ options do not affect command lines that begin with ‘+’ characters
or contain the strings ‘$(MAKE)’ or ‘${MAKE}’. Note that only the line containing the ‘+’
character or the strings ‘$(MAKE)’ or ‘${MAKE}’ is run regardless of these options. Other
lines in the same rule are not run unless they too begin with ‘+’ or contain ‘$(MAKE)’ or
‘${MAKE}’ (See Section 5.6.1 [How the MAKE Variable Works], page 38.)
The ‘-W’ flag provides two features:
• If you also use the ‘-n’ or ‘-q’ flag, you can see what make would do if you were to
modify some files.
• Without the ‘-n’ or ‘-q’ flag, when make is actually executing commands, the ‘-W’ flag
can direct make to act as if some files had been modified, without actually modifying
the files.
Note that the options ‘-p’ and ‘-v’ allow you to obtain other information about make or
about the makefiles in use (see Section 9.7 [Summary of Options], page 78).
1. Recompile the source files that need compilation for reasons independent of the par-
ticular header file, with ‘make -o headerfile’. If several header files are involved, use a
separate ‘-o’ option for each header file.
2. Touch all the object files with ‘make -t’.
When you are compiling a program that you have just changed, this is not what you
want. Instead, you would rather that make try compiling every file that can be tried, to
show you as many compilation errors as possible.
On these occasions, you should use the ‘-k’ or ‘--keep-going’ flag. This tells make
to continue to consider the other dependencies of the pending targets, remaking them if
necessary, before it gives up and returns nonzero status. For example, after an error in
compiling one object file, ‘make -k’ will continue compiling other object files even though it
already knows that linking them will be impossible. In addition to continuing after failed
shell commands, ‘make -k’ will continue as much as possible after discovering that it does
not know how to make a target or dependency file. This will always cause an error message,
but without ‘-k’, it is a fatal error (see Section 9.7 [Summary of Options], page 78).
The usual behavior of make assumes that your purpose is to get the goals up to date;
once make learns that this is impossible, it might as well report the failure immediately.
The ‘-k’ flag says that the real purpose is to test as much as possible of the changes made
in the program, perhaps to find several independent problems so that you can correct them
all before the next attempt to compile. This is why Emacs’ M-x compile command passes
the ‘-k’ flag by default.
‘-h’
‘--help’
Remind you of the options that make understands and then exit.
‘-i’
‘--ignore-errors’
Ignore all errors in commands executed to remake files. See Section 5.4 [Errors
in Commands], page 36.
‘-I dir’
‘--include-dir=dir’
Specifies a directory dir to search for included makefiles. See Section 3.3 [In-
cluding Other Makefiles], page 10. If several ‘-I’ options are used to specify
several directories, the directories are searched in the order specified.
‘-j [jobs]’
‘--jobs=[jobs]’
Specifies the number of jobs (commands) to run simultaneously. With no argu-
ment, make runs as many jobs simultaneously as possible. If there is more than
one ‘-j’ option, the last one is effective. See Section 5.3 [Parallel Execution],
page 35, for more information on how commands are run. Note that this option
is ignored on MS-DOS.
‘-k’
‘--keep-going’
Continue as much as possible after an error. While the target that failed, and
those that depend on it, cannot be remade, the other dependencies of these
targets can be processed all the same. See Section 9.6 [Testing the Compilation
of a Program], page 77.
‘-l [load]’
‘--load-average[=load]’
‘--max-load[=load]’
Specifies that no new jobs (commands) should be started if there are other jobs
running and the load average is at least load (a floating-point number). With no
argument, removes a previous load limit. See Section 5.3 [Parallel Execution],
page 35.
‘-n’
‘--just-print’
‘--dry-run’
‘--recon’
Print the commands that would be executed, but do not execute them. See
Section 9.3 [Instead of Executing the Commands], page 75.
‘-o file’
‘--old-file=file’
‘--assume-old=file’
Do not remake the file file even if it is older than its dependencies, and do not
remake anything on account of changes in file. Essentially the file is treated as
80 GNU make
very old and its rules are ignored. See Section 9.4 [Avoiding Recompilation of
Some Files], page 76.
‘-p’
‘--print-data-base’
Print the data base (rules and variable values) that results from reading the
makefiles; then execute as usual or as otherwise specified. This also prints the
version information given by the ‘-v’ switch (see below). To print the data base
without trying to remake any files, use ‘make -p -f /dev/null’.
‘-q’
‘--question’
“Question mode”. Do not run any commands, or print anything; just return
an exit status that is zero if the specified targets are already up to date, one
if any remaking is required, or two if an error is encountered. See Section 9.3
[Instead of Executing the Commands], page 75.
‘-r’
‘--no-builtin-rules’
Eliminate use of the built-in implicit rules (see Chapter 10 [Using Implicit
Rules], page 83). You can still define your own by writing pattern rules (see
Section 10.5 [Defining and Redefining Pattern Rules], page 90). The ‘-r’ option
also clears out the default list of suffixes for suffix rules (see Section 10.7 [Old-
Fashioned Suffix Rules], page 96). But you can still define your own suffixes
with a rule for .SUFFIXES, and then define your own suffix rules. Note that
only rules are affected by the -r option; default variables remain in effect (see
Section 10.3 [Variables Used by Implicit Rules], page 87).
‘-s’
‘--silent’
‘--quiet’
Silent operation; do not print the commands as they are executed. See Sec-
tion 5.1 [Command Echoing], page 33.
‘-S’
‘--no-keep-going’
‘--stop’
Cancel the effect of the ‘-k’ option. This is never necessary except in a recursive
make where ‘-k’ might be inherited from the top-level make via MAKEFLAGS (see
Section 5.6 [Recursive Use of make], page 37) or if you set ‘-k’ in MAKEFLAGS in
your environment.
‘-t’
‘--touch’
Touch files (mark them up to date without really changing them) instead of
running their commands. This is used to pretend that the commands were
done, in order to fool future invocations of make. See Section 9.3 [Instead of
Executing the Commands], page 75.
Chapter 9: How to Run make 81
‘-v’
‘--version’
Print the version of the make program plus a copyright, a list of authors, and a
notice that there is no warranty; then exit.
‘-w’
‘--print-directory’
Print a message containing the working directory both before and after execut-
ing the makefile. This may be useful for tracking down errors from complicated
nests of recursive make commands. See Section 5.6 [Recursive Use of make],
page 37. (In practice, you rarely need to specify this option since ‘make’ does
it for you; see Section 5.6.4 [The ‘--print-directory’ Option], page 42.)
‘--no-print-directory’
Disable printing of the working directory under -w. This option is useful when
-w is turned on automatically, but you do not want to see the extra messages.
See Section 5.6.4 [The ‘--print-directory’ Option], page 42.
‘-W file’
‘--what-if=file’
‘--new-file=file’
‘--assume-new=file’
Pretend that the target file has just been modified. When used with the ‘-n’
flag, this shows you what would happen if you were to modify that file. Without
‘-n’, it is almost the same as running a touch command on the given file
before running make, except that the modification time is changed only in the
imagination of make. See Section 9.3 [Instead of Executing the Commands],
page 75.
‘--warn-undefined-variables’
Issue a warning message whenever make sees a reference to an undefined vari-
able. This can be helpful when you are trying to debug makefiles which use
variables in complex ways.
82 GNU make
Chapter 10: Using Implicit Rules 83
Of course, when you write the makefile, you know which implicit rule you want make
to use, and you know it will choose that one because you know which possible dependency
files are supposed to exist. See Section 10.2 [Catalogue of Implicit Rules], page 84, for a
catalogue of all the predefined implicit rules.
Above, we said an implicit rule applies if the required dependencies “exist or can be
made”. A file “can be made” if it is mentioned explicitly in the makefile as a target or a
dependency, or if an implicit rule can be recursively found for how to make it. When an
implicit dependency is the result of another implicit rule, we say that chaining is occurring.
See Section 10.4 [Chains of Implicit Rules], page 89.
In general, make searches for an implicit rule for each target, and for each double-colon
rule, that has no commands. A file that is mentioned only as a dependency is considered a
target whose rule specifies nothing, so implicit rule search happens for it. See Section 10.8
[Implicit Rule Search Algorithm], page 98, for the details of how the search is done.
Note that explicit dependencies do not influence implicit rule search. For example,
consider this explicit rule:
foo.o: foo.p
The dependency on ‘foo.p’ does not necessarily mean that make will remake ‘foo.o’ ac-
cording to the implicit rule to make an object file, a ‘.o’ file, from a Pascal source file, a
‘.p’ file. For example, if ‘foo.c’ also exists, the implicit rule to make an object file from a C
source file is used instead, because it appears before the Pascal rule in the list of predefined
implicit rules (see Section 10.2 [Catalogue of Implicit Rules], page 84).
If you do not want an implicit rule to be used for a target that has no commands, you
can give that target empty commands by writing a semicolon (see Section 5.8 [Defining
Empty Commands], page 43).
In more complicated cases, such as when there is no object file whose name
derives from the executable file name, you must write an explicit command for
linking.
Each kind of file automatically made into ‘.o’ object files will be automatically
linked by using the compiler (‘$(CC)’, ‘$(FC)’ or ‘$(PC)’; the C compiler ‘$(CC)’
is used to assemble ‘.s’ files) without the ‘-c’ option. This could be done by
using the ‘.o’ object files as intermediates, but it is faster to do the compiling
and linking in one step, so that’s how it’s done.
Yacc for C programs
‘n.c’ is made automatically from ‘n.y’ by running Yacc with the command
‘$(YACC) $(YFLAGS)’.
Lex for C programs
‘n.c’ is made automatically from ‘n.l’ by by running Lex. The actual command
is ‘$(LEX) $(LFLAGS)’.
Lex for Ratfor programs
‘n.r’ is made automatically from ‘n.l’ by by running Lex. The actual command
is ‘$(LEX) $(LFLAGS)’.
The convention of using the same suffix ‘.l’ for all Lex files regardless of whether
they produce C code or Ratfor code makes it impossible for make to determine
automatically which of the two languages you are using in any particular case. If
make is called upon to remake an object file from a ‘.l’ file, it must guess which
compiler to use. It will guess the C compiler, because that is more common.
If you are using Ratfor, make sure make knows this by mentioning ‘n.r’ in the
makefile. Or, if you are using Ratfor exclusively, with no C files, remove ‘.c’
from the list of implicit rule suffixes with:
.SUFFIXES:
.SUFFIXES: .o .r .f .l . . .
Making Lint Libraries from C, Yacc, or Lex programs
‘n.ln’ is made from ‘n.c’ by running lint. The precise command is ‘$(LINT) $(LINTFLAGS) $(
The same command is used on the C code produced from ‘n.y’ or ‘n.l’.
TEX and Web
‘n.dvi’ is made from ‘n.tex’ with the command ‘$(TEX)’. ‘n.tex’ is made
from ‘n.web’ with ‘$(WEAVE)’, or from ‘n.w’ (and from ‘n.ch’ if it exists or can
be made) with ‘$(CWEAVE)’. ‘n.p’ is made from ‘n.web’ with ‘$(TANGLE)’ and
‘n.c’ is made from ‘n.w’ (and from ‘n.ch’ if it exists or can be made) with
‘$(CTANGLE)’.
Texinfo and Info
‘n.dvi’ is made from ‘n.texinfo’, ‘n.texi’, or ‘n.txinfo’, with the com-
mand ‘$(TEXI2DVI) $(TEXI2DVI_FLAGS)’. ‘n.info’ is made from ‘n.texinfo’,
‘n.texi’, or ‘n.txinfo’, with the command ‘$(MAKEINFO) $(MAKEINFO_FLAGS)’.
RCS Any file ‘n’ is extracted if necessary from an RCS file named either ‘n,v’ or
‘RCS/n,v’. The precise command used is ‘$(CO) $(COFLAGS)’. ‘n’ will not be
extracted from RCS if it already exists, even if the RCS file is newer. The
Chapter 10: Using Implicit Rules 87
rules for RCS are terminal (see Section 10.5.5 [Match-Anything Pattern Rules],
page 94), so RCS files cannot be generated from another source; they must
actually exist.
SCCS Any file ‘n’ is extracted if necessary from an SCCS file named either ‘s.n’
or ‘SCCS/s.n’. The precise command used is ‘$(GET) $(GFLAGS)’. The rules
for SCCS are terminal (see Section 10.5.5 [Match-Anything Pattern Rules],
page 94), so SCCS files cannot be generated from another source; they must
actually exist.
For the benefit of SCCS, a file ‘n’ is copied from ‘n.sh’ and made executable
(by everyone). This is for shell scripts that are checked into SCCS. Since RCS
preserves the execution permission of a file, you do not need to use this feature
with RCS.
We recommend that you avoid using of SCCS. RCS is widely held to be superior,
and is also free. By choosing free software in place of comparable (or inferior)
proprietary software, you support the free software movement.
Usually, you want to change only the variables listed in the table above, which are
documented in the following section.
However, the commands in built-in implicit rules actually use variables such as COMPILE.c,
LINK.p, and PREPROCESS.S, whose values contain the commands listed above.
make follows the convention that the rule to compile a ‘.x’ source file uses the variable
COMPILE.x. Similarly, the rule to produce an executable from a ‘.x’ file uses LINK.x; and
the rule to preprocess a ‘.x’ file uses PREPROCESS.x.
Every rule that produces an object file uses the variable OUTPUT_OPTION. make defines
this variable either to contain ‘-o $@’, or to be empty, depending on a compile-time option.
You need the ‘-o’ option to ensure that the output goes into the right file when the source
file is in a different directory, as when using VPATH (see Section 4.3 [Directory Search],
page 18). However, compilers on some systems do not accept a ‘-o’ switch for object files.
If you use such a system, and use VPATH, some compilations will put their output in the
wrong place. A possible workaround for this problem is to give OUTPUT_OPTION the value
‘; mv $*.o $@’.
The variables used in implicit rules fall into two classes: those that are names of programs
(like CC) and those that contain arguments for the programs (like CFLAGS). (The “name of
a program” may also contain some command arguments, but it must start with an actual
executable program name.) If a variable value contains more than one argument, separate
them with spaces.
Here is a table of variables used as names of programs in built-in rules:
AR Archive-maintaining program; default ‘ar’.
AS Program for doing assembly; default ‘as’.
CC Program for compiling C programs; default ‘cc’.
CXX Program for compiling C++ programs; default ‘g++’.
CO Program for extracting a file from RCS; default ‘co’.
CPP Program for running the C preprocessor, with results to standard output; de-
fault ‘$(CC) -E’.
FC Program for compiling or preprocessing Fortran and Ratfor programs; default
‘f77’.
GET Program for extracting a file from SCCS; default ‘get’.
LEX Program to use to turn Lex grammars into C programs or Ratfor programs;
default ‘lex’.
PC Program for compiling Pascal programs; default ‘pc’.
YACC Program to use to turn Yacc grammars into C programs; default ‘yacc’.
YACCR Program to use to turn Yacc grammars into Ratfor programs; default ‘yacc
-r’.
MAKEINFO Program to convert a Texinfo source file into an Info file; default ‘makeinfo’.
TEX Program to make TEX dvi files from TEX source; default ‘tex’.
TEXI2DVI Program to make TEX dvi files from Texinfo source; default ‘texi2dvi’.
WEAVE Program to translate Web into TEX; default ‘weave’.
CWEAVE Program to translate C Web into TEX; default ‘cweave’.
TANGLE Program to translate Web into Pascal; default ‘tangle’.
CTANGLE Program to translate C Web into C; default ‘ctangle’.
RM Command to remove a file; default ‘rm -f’.
Here is a table of variables whose values are additional arguments for the programs
above. The default values for all of these is the empty string, unless otherwise noted.
ARFLAGS Flags to give the archive-maintaining program; default ‘rv’.
ASFLAGS Extra flags to give to the assembler (when explicitly invoked on a ‘.s’ or ‘.S’
file).
CFLAGS Extra flags to give to the C compiler.
Chapter 10: Using Implicit Rules 89
a dependency of the special target .INTERMEDIATE. This takes effect even if the file is
mentioned explicitly in some other way.
You can prevent automatic deletion of an intermediate file by marking it as a secondary
file. To do this, list it as a dependency of the special target .SECONDARY. When a file
is secondary, make will not create the file merely because it does not already exist, but
make does not automatically delete the file. Marking a file as secondary also marks it as
intermediate.
You can list the target pattern of an implicit rule (such as ‘%.o’) as a dependency of the
special target .PRECIOUS to preserve intermediate files made by implicit rules whose target
patterns match that file’s name; see Section 5.5 [Interrupts], page 37.
A chain can involve more than two implicit rules. For example, it is possible to make a
file ‘foo’ from ‘RCS/foo.y,v’ by running RCS, Yacc and cc. Then both ‘foo.y’ and ‘foo.c’
are intermediate files that are deleted at the end.
No single implicit rule can appear more than once in a chain. This means that make will
not even consider such a ridiculous thing as making ‘foo’ from ‘foo.o.o’ by running the
linker twice. This constraint has the added benefit of preventing any infinite loop in the
search for an implicit rule chain.
There are some special implicit rules to optimize certain cases that would otherwise
be handled by rule chains. For example, making ‘foo’ from ‘foo.c’ could be handled by
compiling and linking with separate chained rules, using ‘foo.o’ as an intermediate file. But
what actually happens is that a special rule for this case does the compilation and linking
with a single cc command. The optimized rule is used in preference to the step-by-step
chain because it comes earlier in the ordering of rules.
characters long. (There must be at least one character to match the ‘%’.) The substring
that the ‘%’ matches is called the stem.
‘%’ in a dependency of a pattern rule stands for the same stem that was matched by the
‘%’ in the target. In order for the pattern rule to apply, its target pattern must match the
file name under consideration, and its dependency patterns must name files that exist or
can be made. These files become dependencies of the target.
Thus, a rule of the form
%.o : %.c ; command. . .
specifies how to make a file ‘n.o’, with another file ‘n.c’ as its dependency, provided that
‘n.c’ exists or can be made.
There may also be dependencies that do not use ‘%’; such a dependency attaches to every
file made by this pattern rule. These unvarying dependencies are useful occasionally.
A pattern rule need not have any dependencies that contain ‘%’, or in fact any depen-
dencies at all. Such a rule is effectively a general wildcard. It provides a way to make any
file that matches the target pattern. See Section 10.6 [Last Resort], page 96.
Pattern rules may have more than one target. Unlike normal rules, this does not act
as many different rules with the same dependencies and commands. If a pattern rule has
multiple targets, make knows that the rule’s commands are responsible for making all of the
targets. The commands are executed only once to make all the targets. When searching
for a pattern rule to match a target, the target patterns of a rule other than the one
that matches the target in need of a rule are incidental: make worries only about giving
commands and dependencies to the file presently in question. However, when this file’s
commands are run, the other targets are marked as having been updated themselves.
The order in which pattern rules appear in the makefile is important since this is the
order in which they are considered. Of equally applicable rules, only the first one found is
used. The rules you write take precedence over those that are built in. Note however, that
a rule whose dependencies actually exist or are mentioned always takes priority over a rule
with dependencies that must be made by chaining other implicit rules.
means that its dependency may not be an intermediate file (see Section 10.5.5 [Match-
Anything Pattern Rules], page 94).
This pattern rule has two targets:
%.tab.c %.tab.h: %.y
bison -d $<
This tells make that the command ‘bison -d x.y’ will make both ‘x.tab.c’ and ‘x.tab.h’.
If the file ‘foo’ depends on the files ‘parse.tab.o’ and ‘scan.o’ and the file ‘scan.o’
depends on the file ‘parse.tab.h’, when ‘parse.y’ is changed, the command ‘bison -d
parse.y’ will be executed only once, and the dependencies of both ‘parse.tab.o’ and
‘scan.o’ will be satisfied. (Presumably the file ‘parse.tab.o’ will be recompiled from
‘parse.tab.c’ and the file ‘scan.o’ from ‘scan.c’, while ‘foo’ is linked from ‘parse.tab.o’,
‘scan.o’, and its other dependencies, and it will execute happily ever after.)
$+ This is like ‘$^’, but dependencies listed more than once are duplicated in the
order they were listed in the makefile. This is primarily useful for use in linking
commands where it is meaningful to repeat library file names in a particular
order.
$* The stem with which an implicit rule matches (see Section 10.5.4 [How Patterns
Match], page 94). If the target is ‘dir/a.foo.b’ and the target pattern is
‘a.%.b’ then the stem is ‘dir/foo’. The stem is useful for constructing names
of related files.
In a static pattern rule, the stem is part of the file name that matched the ‘%’
in the target pattern.
In an explicit rule, there is no stem; so ‘$*’ cannot be determined in that way.
Instead, if the target name ends with a recognized suffix (see Section 10.7 [Old-
Fashioned Suffix Rules], page 96), ‘$*’ is set to the target name minus the
suffix. For example, if the target name is ‘foo.c’, then ‘$*’ is set to ‘foo’, since
‘.c’ is a suffix. GNU make does this bizarre thing only for compatibility with
other implementations of make. You should generally avoid using ‘$*’ except in
implicit rules or static pattern rules.
If the target name in an explicit rule does not end with a recognized suffix, ‘$*’
is set to the empty string for that rule.
‘$?’ is useful even in explicit rules when you wish to operate on only the dependencies
that have changed. For example, suppose that an archive named ‘lib’ is supposed to contain
copies of several object files. This rule copies just the changed object files into the archive:
lib: foo.o bar.o lose.o win.o
ar r lib $?
Of the variables listed above, four have values that are single file names, and two have
values that are lists of file names. These six have variants that get just the file’s directory
name or just the file name within the directory. The variant variables’ names are formed
by appending ‘D’ or ‘F’, respectively. These variants are semi-obsolete in GNU make since
the functions dir and notdir can be used to get a similar effect (see Section 8.3 [Functions
for File Names], page 66). Note, however, that the ‘F’ variants all omit the trailing slash
which always appears in the output of the dir function. Here is a table of the variants:
‘$(@D)’ The directory part of the file name of the target, with the trailing slash removed.
If the value of ‘$@’ is ‘dir/foo.o’ then ‘$(@D)’ is ‘dir’. This value is ‘.’ if ‘$@’
does not contain a slash.
‘$(@F)’ The file-within-directory part of the file name of the target. If the value of ‘$@’
is ‘dir/foo.o’ then ‘$(@F)’ is ‘foo.o’. ‘$(@F)’ is equivalent to ‘$(notdir $@)’.
‘$(*D)’
‘$(*F)’ The directory part and the file-within-directory part of the stem; ‘dir’ and
‘foo’ in this example.
‘$(%D)’
‘$(%F)’ The directory part and the file-within-directory part of the target archive mem-
ber name. This makes sense only for archive member targets of the form
94 GNU make
from ‘foo.c.c’, or by Pascal compilation-and-linking from ‘foo.c.p’, and many other pos-
sibilities.
We know these possibilities are ridiculous since ‘foo.c’ is a C source file, not an exe-
cutable. If make did consider these possibilities, it would ultimately reject them, because
files such as ‘foo.c.o’ and ‘foo.c.p’ would not exist. But these possibilities are so numer-
ous that make would run very slowly if it had to consider them.
To gain speed, we have put various constraints on the way make considers match-anything
rules. There are two different constraints that can be applied, and each time you define a
match-anything rule you must choose one or the other for that rule.
One choice is to mark the match-anything rule as terminal by defining it with a double
colon. When a rule is terminal, it does not apply unless its dependencies actually exist.
Dependencies that could be made with other implicit rules are not good enough. In other
words, no further chaining is allowed beyond a terminal rule.
For example, the built-in implicit rules for extracting sources from RCS and SCCS files
are terminal; as a result, if the file ‘foo.c,v’ does not exist, make will not even consider
trying to make it as an intermediate file from ‘foo.c,v.o’ or from ‘RCS/SCCS/s.foo.c,v’.
RCS and SCCS files are generally ultimate source files, which should not be remade from
any other files; therefore, make can save time by not looking for ways to remake them.
If you do not mark the match-anything rule as terminal, then it is nonterminal. A
nonterminal match-anything rule cannot apply to a file name that indicates a specific type
of data. A file name indicates a specific type of data if some non-match-anything implicit
rule target matches it.
For example, the file name ‘foo.c’ matches the target for the pattern rule ‘%.c : %.y’
(the rule to run Yacc). Regardless of whether this rule is actually applicable (which hap-
pens only if there is a file ‘foo.y’), the fact that its target matches is enough to prevent
consideration of any nonterminal match-anything rules for the file ‘foo.c’. Thus, make will
not even consider trying to make ‘foo.c’ as an executable file from ‘foo.c.o’, ‘foo.c.c’,
‘foo.c.p’, etc.
The motivation for this constraint is that nonterminal match-anything rules are used for
making files containing specific types of data (such as executable files) and a file name with
a recognized suffix indicates some other specific type of data (such as a C source file).
Special built-in dummy pattern rules are provided solely to recognize certain file names so
that nonterminal match-anything rules will not be considered. These dummy rules have no
dependencies and no commands, and they are ignored for all other purposes. For example,
the built-in implicit rule
%.p :
exists to make sure that Pascal source files such as ‘foo.p’ match a specific target pattern
and thereby prevent time from being wasted looking for ‘foo.p.o’ or ‘foo.p.c’.
Dummy pattern rules such as the one for ‘%.p’ are made for every suffix listed as valid
for use in suffix rules (see Section 10.7 [Old-Fashioned Suffix Rules], page 96).
the new rule is defined, the built-in one is replaced. The new rule’s position in the sequence
of implicit rules is determined by where you write the new rule.
You can cancel a built-in implicit rule by defining a pattern rule with the same target
and dependencies, but no commands. For example, the following would cancel the rule that
runs the assembler:
%.o : %.s
name. A two-suffix rule whose target and source suffixes are ‘.o’ and ‘.c’ is equivalent to
the pattern rule ‘%.o : %.c’.
A single-suffix rule is defined by a single suffix, which is the source suffix. It matches
any file name, and the corresponding implicit dependency name is made by appending the
source suffix. A single-suffix rule whose source suffix is ‘.c’ is equivalent to the pattern rule
‘% : %.c’.
Suffix rule definitions are recognized by comparing each rule’s target against a defined
list of known suffixes. When make sees a rule whose target is a known suffix, this rule is
considered a single-suffix rule. When make sees a rule whose target is two known suffixes
concatenated, this rule is taken as a double-suffix rule.
For example, ‘.c’ and ‘.o’ are both on the default list of known suffixes. Therefore,
if you define a rule whose target is ‘.c.o’, make takes it to be a double-suffix rule with
source suffix ‘.c’ and target suffix ‘.o’. Here is the old-fashioned way to define the rule for
compiling a C source file:
.c.o:
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
Suffix rules cannot have any dependencies of their own. If they have any, they are treated
as normal files with funny names, not as suffix rules. Thus, the rule:
.c.o: foo.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
tells how to make the file ‘.c.o’ from the dependency file ‘foo.h’, and is not at all like the
pattern rule:
%.o: %.c foo.h
$(CC) -c $(CFLAGS) $(CPPFLAGS) -o $@ $<
which tells how to make ‘.o’ files from ‘.c’ files, and makes all ‘.o’ files using this pattern
rule also depend on ‘foo.h’.
Suffix rules with no commands are also meaningless. They do not remove previous
rules as do pattern rules with no commands (see Section 10.5.6 [Canceling Implicit Rules],
page 95). They simply enter the suffix or pair of suffixes concatenated as a target in the
data base.
The known suffixes are simply the names of the dependencies of the special target
.SUFFIXES. You can add your own suffixes by writing a rule for .SUFFIXES that adds
more dependencies, as in:
.SUFFIXES: .hack .win
which adds ‘.hack’ and ‘.win’ to the end of the list of suffixes.
If you wish to eliminate the default known suffixes instead of just adding to them, write
a rule for .SUFFIXES with no dependencies. By special dispensation, this eliminates all
existing dependencies of .SUFFIXES. You can then write another rule to add the suffixes
you want. For example,
.SUFFIXES: # Delete the default suffixes
.SUFFIXES: .c .o .h # Define our suffix list
The ‘-r’ or ‘--no-builtin-rules’ flag causes the default list of suffixes to be empty.
98 GNU make
The variable SUFFIXES is defined to the default list of suffixes before make reads any
makefiles. You can change the list of suffixes with a rule for the special target .SUFFIXES,
but that does not alter this variable.
Once a rule that applies has been found, for each target pattern of the rule other than
the one that matched t or n, the ‘%’ in the pattern is replaced with s and the resultant file
name is stored until the commands to remake the target file t are executed. After these
commands are executed, each of these stored file names are entered into the data base and
marked as having been updated and having the same update status as the file t.
When the commands of a pattern rule are executed for t, the automatic variables are set
corresponding to the target and dependencies. See Section 10.5.3 [Automatic Variables],
page 92.
100 GNU make
Chapter 11: Using make to Update Archive Files 101
cc -c bar.c -o bar.o
ar r foo.a bar.o
rm -f bar.o
Here make has envisioned the file ‘bar.o’ as an intermediate file. See Section 10.4 [Chains
of Implicit Rules], page 89.
Implicit rules such as this one are written using the automatic variable ‘$%’. See Sec-
tion 10.5.3 [Automatic Variables], page 92.
An archive member name in an archive cannot contain a directory name, but it may
be useful in a makefile to pretend that it does. If you write an archive member target
‘foo.a(dir/file.o)’, make will perform automatic updating with this command:
ar r foo.a dir/file.o
which has the effect of copying the file ‘dir/file.o’ into a member named ‘file.o’. In
connection with such usage, the automatic variables %D and %F may be useful.
We do not really know if we got this from either of them or thought it up ourselves at
the same time. See Section 10.4 [Chains of Implicit Rules], page 89.
• The automatic variable $^ containing a list of all dependencies of the current target.
We did not invent this, but we have no idea who did. See Section 10.5.3 [Automatic
Variables], page 92. The automatic variable $+ is a simple extension of $^.
• The “what if” flag (‘-W’ in GNU make) was (as far as we know) invented by Andrew
Hume in mk. See Section 9.3 [Instead of Executing the Commands], page 75.
• The concept of doing several things at once (parallelism) exists in many incarnations
of make and similar programs, though not in the System V or BSD implementations.
See Section 5.2 [Command Execution], page 33.
• Modified variable references using pattern substitution come from SunOS 4. See Sec-
tion 6.1 [Basics of Variable References], page 45. This functionality was provided in
GNU make by the patsubst function before the alternate syntax was implemented for
compatibility with SunOS 4. It is not altogether clear who inspired whom, since GNU
make had patsubst before SunOS 4 was released.
• The special significance of ‘+’ characters preceding command lines (see Section 9.3
[Instead of Executing the Commands], page 75) is mandated by IEEE Standard 1003.2-
1992 (POSIX.2).
• The ‘+=’ syntax to append to the value of a variable comes from SunOS 4 make. See
Section 6.6 [Appending More Text to Variables], page 52.
• The syntax ‘archive(mem1 mem2. . .)’ to list multiple members in a single archive file
comes from SunOS 4 make. See Section 11.1 [Archive Members], page 101.
• The -include directive to include makefiles with no error for a nonexistent file comes
from SunOS 4 make. (But note that SunOS 4 make does not allow multiple makefiles
to be specified in one -include directive.) The same feature appears with the name
sinclude in SGI make and perhaps others.
The remaining features are inventions new in GNU make:
• Use the ‘-v’ or ‘--version’ option to print version and copyright information.
• Use the ‘-h’ or ‘--help’ option to summarize the options to make.
• Simply-expanded variables. See Section 6.2 [The Two Flavors of Variables], page 46.
• Pass command-line variable assignments automatically through the variable MAKE to
recursive make invocations. See Section 5.6 [Recursive Use of make], page 37.
• Use the ‘-C’ or ‘--directory’ command option to change directory. See Section 9.7
[Summary of Options], page 78.
• Make verbatim variable definitions with define. See Section 6.8 [Defining Variables
Verbatim], page 54.
• Declare phony targets with the special target .PHONY.
Andrew Hume of AT&T Bell Labs implemented a similar feature with a different syntax
in his mk program. This seems to be a case of parallel discovery. See Section 4.4 [Phony
Targets], page 22.
• Manipulate text by calling functions. See Chapter 8 [Functions for Transforming Text],
page 63.
Chapter 12: Features of GNU make 107
• Use the ‘-o’ or ‘--old-file’ option to pretend a file’s modification-time is old. See
Section 9.4 [Avoiding Recompilation of Some Files], page 76.
• Conditional execution.
This feature has been implemented numerous times in various versions of make; it seems
a natural extension derived from the features of the C preprocessor and similar macro
languages and is not a revolutionary concept. See Chapter 7 [Conditional Parts of
Makefiles], page 59.
• Specify a search path for included makefiles. See Section 3.3 [Including Other Make-
files], page 10.
• Specify extra makefiles to read with an environment variable. See Section 3.4 [The
Variable MAKEFILES], page 11.
• Strip leading sequences of ‘./’ from file names, so that ‘./file’ and ‘file’ are considered
to be the same file.
• Use a special search method for library dependencies written in the form ‘-lname’.
See Section 4.3.6 [Directory Search for Link Libraries], page 21.
• Allow suffixes for suffix rules (see Section 10.7 [Old-Fashioned Suffix Rules], page 96)
to contain any characters. In other versions of make, they must begin with ‘.’ and not
contain any ‘/’ characters.
• Keep track of the current level of make recursion using the variable MAKELEVEL. See
Section 5.6 [Recursive Use of make], page 37.
• Provide any goals given on the command line in the variable MAKECMDGOALS. See
Section 9.2 [Arguments to Specify the Goals], page 73.
• Specify static pattern rules. See Section 4.10 [Static Pattern Rules], page 27.
• Provide selective vpath search. See Section 4.3 [Searching Directories for Dependen-
cies], page 18.
• Provide computed variable references. See Section 6.1 [Basics of Variable References],
page 45.
• Update makefiles. See Section 3.5 [How Makefiles Are Remade], page 11. System V
make has a very, very limited form of this functionality in that it will check out SCCS
files for makefiles.
• Various new built-in implicit rules. See Section 10.2 [Catalogue of Implicit Rules],
page 84.
• The built-in variable ‘MAKE_VERSION’ gives the version number of make.
108 GNU make
Chapter 13: Incompatibilities and Missing Features 109
• GNU make does not include any built-in implicit rules for compiling or preprocessing
EFL programs. If we hear of anyone who is using EFL, we will gladly add them.
• It appears that in SVR4 make, a suffix rule can be specified with no commands, and it
is treated as if it had empty commands (see Section 5.8 [Empty Commands], page 43).
For example:
.c.a:
will override the built-in ‘.c.a’ suffix rule.
We feel that it is cleaner for a rule without commands to always simply add to the
dependency list for the target. The above example can be easily rewritten to get the
desired behavior in GNU make:
.c.a: ;
• Some versions of make invoke the shell with the ‘-e’ flag, except under ‘-k’ (see Sec-
tion 9.6 [Testing the Compilation of a Program], page 77). The ‘-e’ flag tells the shell
to exit as soon as any program it runs returns a nonzero status. We feel it is cleaner
to write each shell command line to stand on its own and not require this special
treatment.
Chapter 14: Makefile Conventions 111
14 Makefile Conventions
This chapter describes conventions for writing the Makefiles for GNU programs.
GNU distributions usually contain some files which are not source files—for example, Info
files, and the output from Autoconf, Automake, Bison or Flex. Since these files normally
appear in the source directory, they should always appear in the source directory, not in
the build directory. So Makefile rules to update them should put the updated files in the
source directory.
However, if a file does not appear in the distribution, then the Makefile should not put
it in the source directory, because building a program in ordinary circumstances should not
modify the source directory in any way.
Try to make the build and installation targets, at least (and all their subtargets) work
correctly with a parallel make.
Therefore, here are the variables Makefiles should use to specify directories:
‘datadir’ The directory for installing read-only architecture independent data files. This
should normally be ‘/usr/local/share’, but write it as ‘$(prefix)/share’.
(If you are using Autoconf, write it as ‘@datadir@’.) As a special exception,
see ‘$(infodir)’ and ‘$(includedir)’ below.
‘sysconfdir’
The directory for installing read-only data files that pertain to a single machine–
that is to say, files for configuring a host. Mailer and network configuration
files, ‘/etc/passwd’, and so forth belong here. All the files in this direc-
tory should be ordinary ASCII text files. This directory should normally be
‘/usr/local/etc’, but write it as ‘$(prefix)/etc’. (If you are using Autoconf,
write it as ‘@sysconfdir@’.)
Do not install executables here in this directory (they probably belong in
‘$(libexecdir)’ or ‘$(sbindir)’). Also do not install files that are modified in
the normal course of their use (programs whose purpose is to change the configu-
ration of the system excluded). Those probably belong in ‘$(localstatedir)’.
‘sharedstatedir’
The directory for installing architecture-independent data files which the pro-
grams modify while they run. This should normally be ‘/usr/local/com’,
but write it as ‘$(prefix)/com’. (If you are using Autoconf, write it as
‘@sharedstatedir@’.)
‘localstatedir’
The directory for installing data files which the programs modify while they run,
and that pertain to one specific machine. Users should never need to modify
files in this directory to configure the package’s operation; put such configura-
tion information in separate files that go in ‘$(datadir)’ or ‘$(sysconfdir)’.
‘$(localstatedir)’ should normally be ‘/usr/local/var’, but write it as
‘$(prefix)/var’. (If you are using Autoconf, write it as ‘@localstatedir@’.)
‘libdir’ The directory for object files and libraries of object code. Do not install
executables here, they probably ought to go in ‘$(libexecdir)’ instead.
The value of libdir should normally be ‘/usr/local/lib’, but write it as
‘$(exec_prefix)/lib’. (If you are using Autoconf, write it as ‘@libdir@’.)
‘infodir’ The directory for installing the Info files for this package. By default, it should
be ‘/usr/local/info’, but it should be written as ‘$(prefix)/info’. (If you
are using Autoconf, write it as ‘@infodir@’.)
‘lispdir’ The directory for installing any Emacs Lisp files in this package. By default, it
should be ‘/usr/local/share/emacs/site-lisp’, but it should be written as
‘$(prefix)/share/emacs/site-lisp’.
If you are using Autoconf, write the default as ‘@lispdir@’. In order to make
‘@lispdir@’ work, you need the following lines in your ‘configure.in’ file:
lispdir=’${datadir}/emacs/site-lisp’
AC_SUBST(lispdir)
116 GNU make
‘includedir’
The directory for installing header files to be included by user programs
with the C ‘#include’ preprocessor directive. This should normally be
‘/usr/local/include’, but write it as ‘$(prefix)/include’. (If you are
using Autoconf, write it as ‘@includedir@’.)
Most compilers other than GCC do not look for header files in directory
‘/usr/local/include’. So installing the header files this way is only useful
with GCC. Sometimes this is not a problem because some libraries are only
really intended to work with GCC. But some libraries are intended to work
with other compilers. They should install their header files in two places, one
specified by includedir and one specified by oldincludedir.
‘oldincludedir’
The directory for installing ‘#include’ header files for use with compilers other
than GCC. This should normally be ‘/usr/include’. (If you are using Auto-
conf, you can write it as ‘@oldincludedir@’.)
The Makefile commands should check whether the value of oldincludedir is
empty. If it is, they should not try to use it; they should cancel the second
installation of the header files.
A package should not replace an existing header in this directory unless the
header came from the same package. Thus, if your Foo package provides a
header file ‘foo.h’, then it should install the header file in the oldincludedir
directory if either (1) there is no ‘foo.h’ there or (2) the ‘foo.h’ that exists
came from the Foo package.
To tell whether ‘foo.h’ came from the Foo package, put a magic string in the
file—part of a comment—and grep for that string.
Unix-style man pages are installed in one of the following:
‘mandir’ The top-level directory for installing the man pages (if any) for this package. It
will normally be ‘/usr/local/man’, but you should write it as ‘$(prefix)/man’.
(If you are using Autoconf, write it as ‘@mandir@’.)
‘man1dir’ The directory for installing section 1 man pages. Write it as ‘$(mandir)/man1’.
‘man2dir’ The directory for installing section 2 man pages. Write it as ‘$(mandir)/man2’
‘. . .’
Don’t make the primary documentation for any GNU software be a man page.
Write a manual in Texinfo instead. Man pages are just for the sake of people
running GNU software on Unix, which is a secondary application only.
‘manext’ The file name extension for the installed man page. This should contain a
period followed by the appropriate digit; it should normally be ‘.1’.
‘man1ext’ The file name extension for installed section 1 man pages.
‘man2ext’ The file name extension for installed section 2 man pages.
‘. . .’ Use these names instead of ‘manext’ if the package needs to install man pages
in more than one section of the manual.
Chapter 14: Makefile Conventions 117
of the variables prefix and exec_prefix, as well as all subdirectories that are
needed. One way to do this is by means of an installdirs target as described
below.
Use ‘-’ before any command for installing a man page, so that make will ignore
any errors. This is in case there are systems that don’t have the Unix man page
documentation system installed.
The way to install Info files is to copy them into ‘$(infodir)’ with $(INSTALL_
DATA) (see Section 14.3 [Command Variables], page 113), and then run the
install-info program if it is present. install-info is a program that edits
the Info ‘dir’ file to add or update the menu entry for the given Info file; it is
part of the Texinfo package. Here is a sample rule to install an Info file:
$(infodir)/foo.info: foo.info
$(POST_INSTALL)
# There may be a newer info file in . than in srcdir.
-if test -f foo.info; then d=.; \
else d=$(srcdir); fi; \
$(INSTALL_DATA) $$d/foo.info $@; \
# Run install-info only if it exists.
# Use ‘if’ instead of just prepending ‘-’ to the
# line so we notice real errors from install-info.
# We use ‘$(SHELL) -c’ because some shells do not
# fail gracefully when there is an unknown command.
if $(SHELL) -c ’install-info --version’ \
>/dev/null 2>&1; then \
install-info --dir-file=$(infodir)/dir \
$(infodir)/foo.info; \
else true; fi
When writing the install target, you must classify all the commands into
three categories: normal ones, pre-installation commands and post-installation
commands. See Section 14.6 [Install Command Categories], page 121.
‘uninstall’
Delete all the installed files—the copies that the ‘install’ target creates.
This rule should not modify the directories where compilation is done, only the
directories where files are installed.
The uninstallation commands are divided into three categories, just like the in-
stallation commands. See Section 14.6 [Install Command Categories], page 121.
‘install-strip’
Like install, but strip the executable files while installing them. In many
cases, the definition of this target can be very simple:
install-strip:
$(MAKE) INSTALL_PROGRAM=’$(INSTALL_PROGRAM) -s’ \
install
Normally we do not recommend stripping an executable unless you are sure the
program has no bugs. However, it can be reasonable to install a stripped exe-
Chapter 14: Makefile Conventions 119
cutable for actual execution while saving the unstripped executable elsewhere
in case there is a bug.
‘clean’
Delete all files from the current directory that are normally created by build-
ing the program. Don’t delete the files that record the configuration. Also
preserve files that could be made by building, but normally aren’t because the
distribution comes with them.
Delete ‘.dvi’ files here if they are not part of the distribution.
‘distclean’
Delete all files from the current directory that are created by configuring or
building the program. If you have unpacked the source and built the program
without creating any other files, ‘make distclean’ should leave only the files
that were in the distribution.
‘mostlyclean’
Like ‘clean’, but may refrain from deleting a few files that people normally
don’t want to recompile. For example, the ‘mostlyclean’ target for GCC does
not delete ‘libgcc.a’, because recompiling it is rarely necessary and takes a lot
of time.
‘maintainer-clean’
Delete almost everything from the current directory that can be reconstructed
with this Makefile. This typically includes everything deleted by distclean,
plus more: C source files produced by Bison, tags tables, Info files, and so on.
The reason we say “almost everything” is that running the command ‘make
maintainer-clean’ should not delete ‘configure’ even if ‘configure’ can be
remade using a rule in the Makefile. More generally, ‘make maintainer-clean’
should not delete anything that needs to exist in order to run ‘configure’ and
then begin to build the program. This is the only exception; maintainer-clean
should delete everything else that can be rebuilt.
The ‘maintainer-clean’ target is intended to be used by a maintainer of the
package, not by ordinary users. You may need special tools to reconstruct
some of the files that ‘make maintainer-clean’ deletes. Since these files are
normally included in the distribution, we don’t take care to make them easy to
reconstruct. If you find you need to unpack the full distribution again, don’t
blame us.
To help make users aware of this, the commands for the special maintainer-
clean target should start with these two:
@echo ’This command is intended for maintainers to use; it’
@echo ’deletes files that may need special tools to rebuild.’
‘TAGS’ Update a tags table for this program.
‘info’ Generate any Info files needed. The best way to write the rules is as follows:
info: foo.info
120 GNU make
installdirs
It’s useful to add a target named ‘installdirs’ to create the directories
where files are installed, and their parent directories. There is a script called
‘mkinstalldirs’ which is convenient for this; you can find it in the Texinfo
package. You can use a rule like this:
# Make sure all installation directories (e.g. $(bindir))
# actually exist by making them if necessary.
installdirs: mkinstalldirs
$(srcdir)/mkinstalldirs $(bindir) $(datadir) \
$(libdir) $(infodir) \
$(mandir)
This rule should not modify the directories where compilation is done. It should
do nothing but create installation directories.
If you don’t use a category line at the beginning of the install rule, all the commands
are classified as normal until the first category line. If you don’t use any category lines, all
the commands are classified as normal.
These are the category lines for uninstall:
$(PRE_UNINSTALL) # Pre-uninstall commands follow.
$(POST_UNINSTALL) # Post-uninstall commands follow.
$(NORMAL_UNINSTALL) # Normal commands follow.
Typically, a pre-uninstall command would be used for deleting entries from the Info
directory.
If the install or uninstall target has any dependencies which act as subroutines of
installation, then you should start each dependency’s commands with a category line, and
start the main target’s commands with a category line also. This way, you can ensure
that each command is placed in the right category regardless of which of the dependencies
actually run.
Pre-installation and post-installation commands should not run any programs except for
these:
[ basename bash cat chgrp chmod chown cmp cp dd diff echo
egrep expand expr false fgrep find getopt grep gunzip gzip
hostname install install-info kill ldconfig ln ls md5sum
mkdir mkfifo mknod mv printenv pwd rm rmdir sed sort tee
test touch true uname xargs yes
The reason for distinguishing the commands in this way is for the sake of making binary
packages. Typically a binary package contains all the executables and other files that need
to be installed, and has its own method of installing them—so it does not need to run the
normal installation commands. But installing the binary package does need to execute the
pre-installation and post-installation commands.
Programs to build binary packages work by extracting the pre-installation and post-
installation commands. Here is one way of extracting the pre-installation commands:
make -n install -o all \
PRE_INSTALL=pre-install \
POST_INSTALL=post-install \
NORMAL_INSTALL=normal-install \
| gawk -f pre-install.awk
where the file ‘pre-install.awk’ could contain this:
$0 ~ /^\t[ \t]*(normal_install|post_install)[ \t]*$/ {on = 0}
on {print $0}
$0 ~ /^\t[ \t]*pre_install[ \t]*$/ {on = 1}
The resulting file of pre-installation commands is executed as a shell script as part of
installing the binary package.
Appendix A: Quick Reference 123
$(addsuffix suffix,names. . .)
Append suffix to each word in names.
See Section 8.3 [Functions for File Names], page 66.
$(addprefix prefix,names. . .)
Prepend prefix to each word in names.
See Section 8.3 [Functions for File Names], page 66.
$(join list1,list2)
Join two parallel lists of words.
See Section 8.3 [Functions for File Names], page 66.
$(word n,text)
Extract the nth word (one-origin) of text.
See Section 8.3 [Functions for File Names], page 66.
$(words text)
Count the number of words in text.
See Section 8.3 [Functions for File Names], page 66.
$(firstword names. . .)
Extract the first word of names.
See Section 8.3 [Functions for File Names], page 66.
$(wildcard pattern. . .)
Find file names matching a shell file name pattern (not a ‘%’ pattern).
See Section 4.2.3 [The Function wildcard], page 17.
$(shell command)
Execute a shell command and return its output.
See Section 8.6 [The shell Function], page 71.
$(origin variable)
Return a string describing how the make variable variable was defined.
See Section 8.5 [The origin Function], page 70.
$(foreach var,words,text)
Evaluate text with var bound to each word in words, and concatenate the
results.
See Section 8.4 [The foreach Function], page 69.
Here is a summary of the automatic variables. See Section 10.5.3 [Automatic Variables],
page 92, for full information.
$@ The file name of the target.
$% The target member name, when the target is an archive member.
$< The name of the first dependency.
$? The names of all the dependencies that are newer than the target, with spaces
between them. For dependencies which are archive members, only the member
named is used (see Chapter 11 [Archives], page 101).
126 GNU make
$^
$+ The names of all the dependencies, with spaces between them. For dependencies
which are archive members, only the member named is used (see Chapter 11
[Archives], page 101). The value of $^ omits duplicate dependencies, while $+
retains them and preserves their order.
$* The stem with which an implicit rule matches (see Section 10.5.4 [How Patterns
Match], page 94).
$(@D)
$(@F) The directory part and the file-within-directory part of $@.
$(*D)
$(*F) The directory part and the file-within-directory part of $*.
$(%D)
$(%F) The directory part and the file-within-directory part of $%.
$(<D)
$(<F) The directory part and the file-within-directory part of $<.
$(^D)
$(^F) The directory part and the file-within-directory part of $^.
$(+D)
$(+F) The directory part and the file-within-directory part of $+.
$(?D)
$(?F) The directory part and the file-within-directory part of $?.
These variables are used specially by GNU make:
MAKEFILES
Makefiles to be read on every invocation of make.
See Section 3.4 [The Variable MAKEFILES], page 11.
VPATH
Directory search path for files not found in the current directory.
See Section 4.3.1 [VPATH Search Path for All Dependencies], page 18.
SHELL
The name of the system default command interpreter, usually ‘/bin/sh’. You
can set SHELL in the makefile to change the shell used to run commands. See
Section 5.2 [Command Execution], page 33.
MAKESHELL
On MS-DOS only, the name of the command interpreter that is to be used by
make. This value takes precedence over the value of SHELL. See Section 5.2
[MAKESHELL variable], page 33.
MAKE
The name with which make was invoked. Using this variable in commands has
special meaning. See Section 5.6.1 [How the MAKE Variable Works], page 38.
Appendix A: Quick Reference 127
MAKELEVEL
The number of levels of recursion (sub-makes).
See Section 5.6.2 [Variables/Recursion], page 38.
MAKEFLAGS
The flags given to make. You can set this in the environment or a makefile to
set flags.
See Section 5.6.3 [Communicating Options to a Sub-make], page 40.
MAKECMDGOALS
The targets given to make on the command line. Setting this variable has no
effect on the operation of make.
See Section 9.2 [Arguments to Specify the Goals], page 73.
CURDIR
Set to the pathname of the current working directory (after all -C options are
processed, if any). Setting this variable has no effect on the operation of make.
See Section 5.6 [Recursive Use of make], page 37.
SUFFIXES
The default list of suffixes before make reads any makefiles.
128 GNU make
Appendix B: Errors Generated by Make 129
If you want that file to be built, you will need to add a rule to your makefile
describing how that target can be built. Other possible sources of this problem
are typos in the makefile (if that filename is wrong) or a corrupted source tree
(if that file is not supposed to be built, but rather only a dependency).
‘No targets specified and no makefile found. Stop.’
‘No targets. Stop.’
The former means that you didn’t provide any targets to be built on the com-
mand line, and make couldn’t find any makefiles to read in. The latter means
that some makefile was found, but it didn’t contain any default target and
none was given on the command line. GNU make has nothing to do in these
situations.
‘Makefile ‘xxx’ was not found.’
‘Included makefile ‘xxx’ was not found.’
A makefile specified on the command line (first form) or included (second form)
was not found.
‘warning: overriding commands for target ‘xxx’’
‘warning: ignoring old commands for target ‘xxx’’
GNU make allows commands to be specified only once per target (except for
double-colon rules). If you give commands for a target which already has been
defined to have commands, this warning is issued and the second set of com-
mands will overwrite the first set.
‘Circular xxx <- yyy dependency dropped.’
This means that make detected a loop in the dependency graph: after tracing
the dependency yyy of target xxx, and its dependencies, etc., one of them
depended on xxx again.
‘Recursive variable ‘xxx’ references itself (eventually). Stop.’
This means you’ve defined a normal (recursive) make variable xxx that, when
its expanded, will refer to itself (xxx). This is not allowed; either use simply-
expanded variables (:=) or use the append operator (+=).
‘Unterminated variable reference. Stop.’
This means you forgot to provide the proper closing parenthesis or brace in
your variable or function reference.
‘insufficient arguments to function ‘xxx’. Stop.’
This means you haven’t provided the requisite number of arguments for this
function. See the documentation of the function for a description of its argu-
ments.
‘missing target pattern. Stop.’
‘multiple target patterns. Stop.’
‘target pattern contains no ‘%’. Stop.’
These are generated for malformed static pattern rules. The first means there’s
no pattern in the target section of the rule, the second means there are multiple
patterns in the target section, and the third means the target doesn’t contain
a pattern character (%).
Appendix C: Complex Makefile Example 131
srcdir = .
# libraries.
# -DBSD42 If you have sys/dir.h (unless
# you use -DPOSIX), sys/file.h,
# and st_blocks in ‘struct stat’.
# -DUSG If you have System V/ANSI C
# string and memory functions
# and headers, sys/sysmacros.h,
# fcntl.h, getcwd, no valloc,
# and ndir.h (unless
# you use -DDIRENT).
# -DNO_MEMORY_H If USG or STDC_HEADERS but do not
# include memory.h.
# -DDIRENT If USG and you have dirent.h
# instead of ndir.h.
# -DSIGTYPE=int If your signal handlers
# return int, not void.
# -DNO_MTIO If you lack sys/mtio.h
# (magtape ioctls).
# -DNO_REMOTE If you do not have a remote shell
# or rexec.
# -DUSE_REXEC To use rexec for remote tape
# operations instead of
# forking rsh or remsh.
# -DVPRINTF_MISSING If you lack vprintf function
# (but have _doprnt).
# -DDOPRNT_MISSING If you lack _doprnt function.
# Also need to define
# -DVPRINTF_MISSING.
# -DFTIME_MISSING If you lack ftime system call.
# -DSTRSTR_MISSING If you lack strstr function.
# -DVALLOC_MISSING If you lack valloc function.
# -DMKDIR_MISSING If you lack mkdir and
# rmdir system calls.
# -DRENAME_MISSING If you lack rename system call.
# -DFTRUNCATE_MISSING If you lack ftruncate
# system call.
# -DV7 On Version 7 Unix (not
# tested in a long time).
# -DEMUL_OPEN3 If you lack a 3-argument version
# of open, and want to emulate it
# with system calls you do have.
# -DNO_OPEN3 If you lack the 3-argument open
# and want to disable the tar -k
# option instead of emulating open.
# -DXENIX If you have sys/inode.h
# and need it 94 to be included.
-DVPRINTF_MISSING -DBSD42
# Set this to rtapelib.o unless you defined NO_REMOTE,
# in which case make it empty.
RTAPELIB = rtapelib.o
LIBS =
DEF_AR_FILE = /dev/rmt8
DEFBLOCKING = 20
CDEBUG = -g
CFLAGS = $(CDEBUG) -I. -I$(srcdir) $(DEFS) \
-DDEF_AR_FILE=\"$(DEF_AR_FILE)\" \
-DDEFBLOCKING=$(DEFBLOCKING)
LDFLAGS = -g
prefix = /usr/local
# Prefix for each installed program,
# normally empty or ‘g’.
binprefix =
tar: $(OBJS)
$(CC) $(LDFLAGS) -o $@ $(OBJS) $(LIBS)
rmt: rmt.c
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ rmt.c
tar.info: tar.texinfo
makeinfo tar.texinfo
install: all
$(INSTALL) tar $(bindir)/$(binprefix)tar
-test ! -f rmt || $(INSTALL) rmt /etc/rmt
$(INSTALLDATA) $(srcdir)/tar.info* $(infodir)
$(OBJS): tar.h port.h testpad.h
regex.o buffer.o tar.o: regex.h
# getdate.y has 8 shift/reduce conflicts.
testpad.h: testpad
./testpad
testpad: testpad.o
$(CC) -o $@ testpad.o
TAGS: $(SRCS)
etags $(SRCS)
clean:
rm -f *.o tar rmt testpad testpad.h core
distclean: clean
rm -f TAGS Makefile config.status
realclean: distclean
rm -f tar.info*
shar: $(SRCS) $(AUX)
shar $(SRCS) $(AUX) | compress \
> tar-‘sed -e ’/version_string/!d’ \
-e ’s/[^0-9.]*\([0-9.]*\).*/\1/’ \
-e q
version.c‘.shar.Z
dist: $(SRCS) $(AUX)
echo tar-‘sed \
-e ’/version_string/!d’ \
-e ’s/[^0-9.]*\([0-9.]*\).*/\1/’ \
-e q
version.c‘ > .fname
-rm -rf ‘cat .fname‘
mkdir ‘cat .fname‘
ln $(SRCS) $(AUX) ‘cat .fname‘
-rm -rf ‘cat .fname‘ .fname
tar chZf ‘cat .fname‘.tar.Z ‘cat .fname‘
Appendix C: Complex Makefile Example 135
Index of Concepts
# --load-average . . . . . . . . . . . . . . . . . . . . . . . . . . . 35, 79
# (comments), in commands . . . . . . . . . . . . . . . . . . . 33 --makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10, 73, 78
# (comments), in makefile . . . . . . . . . . . . . . . . . . . . . . . 9 --max-load . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35, 79
#include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30 --new-file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 81
--new-file, and recursion . . . . . . . . . . . . . . . . . . . . . 40
$ --no-builtin-rules . . . . . . . . . . . . . . . . . . . . . . . . . . 80
$, in function call . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 63 --no-keep-going . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
$, in rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15 --no-print-directory . . . . . . . . . . . . . . . . . . . . 42, 81
$, in variable name . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49 --old-file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76, 79
$, in variable reference . . . . . . . . . . . . . . . . . . . . . . . . . 45 --old-file, and recursion . . . . . . . . . . . . . . . . . . . . . 40
--print-data-base. . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
--print-directory. . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
% --print-directory, and --directory . . . . . . . . . 42
%, in pattern rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 90 --print-directory, and recursion . . . . . . . . . . . . . 42
%, quoting in patsubst. . . . . . . . . . . . . . . . . . . . . . . . . 64 --print-directory, disabling . . . . . . . . . . . . . . . . . 42
%, quoting in static pattern . . . . . . . . . . . . . . . . . . . . 28 --question . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 80
%, quoting in vpath . . . . . . . . . . . . . . . . . . . . . . . . . . . . 19 --quiet . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 80
%, quoting with \ (backslash) . . . . . . . . . . . 19, 28, 64 --recon . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 75, 79
--silent . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 80
* --stop . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
* (wildcard character) . . . . . . . . . . . . . . . . . . . . . . . . . 16 --touch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 80
--touch, and recursion . . . . . . . . . . . . . . . . . . . . . . . . 38
, --version . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
,v (RCS file extension) . . . . . . . . . . . . . . . . . . . . . . . . 86 --warn-undefined-variables . . . . . . . . . . . . . . . . . 81
--what-if . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 81
- -b . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
-C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38, 78
- (in commands) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36
-C, and -w . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
-, and define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
-C, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
--assume-new . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 81
-d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
--assume-new, and recursion . . . . . . . . . . . . . . . . . . . 40
-e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
--assume-old . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76, 79
-e (shell flag) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
--assume-old, and recursion . . . . . . . . . . . . . . . . . . . 40
-f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10, 73, 78
--debug . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
-f, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
--directory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38, 78
-h . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
--directory, and --print-directory . . . . . . . . . 42
-i . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36, 79
--directory, and recursion . . . . . . . . . . . . . . . . . . . . 40
-I . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10, 79
--dry-run . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 75, 79
-j . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35, 79
--environment-overrides . . . . . . . . . . . . . . . . . . . . . 78
-j, and archive update . . . . . . . . . . . . . . . . . . . . . . . 102
--file . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10, 73, 78
-j, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
--file, and recursion. . . . . . . . . . . . . . . . . . . . . . . . . . 40
-k . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36, 78, 79
--help . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
-l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 79
--ignore-errors . . . . . . . . . . . . . . . . . . . . . . . . . . 36, 79
-l (library search) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
--include-dir . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10, 79
-l (load average) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
--jobs. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35, 79
-m . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
--jobs, and recursion. . . . . . . . . . . . . . . . . . . . . . . . . . 40
-M (to compiler) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
--just-print . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 75, 79
-MM (to GNU compiler) . . . . . . . . . . . . . . . . . . . . . . . . 30
--keep-going . . . . . . . . . . . . . . . . . . . . . . . . . . 36, 78, 79
138 GNU make
-n . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 75, 79 :
-o . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 76, 79 :: rules (double-colon) . . . . . . . . . . . . . . . . . . . . . . . . 29
-o, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40 := . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46, 51
-p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
-q . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 80 =
-r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 = . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46, 51
-s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33, 80
-S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80 ?
-t . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 80
? (wildcard character) . . . . . . . . . . . . . . . . . . . . . . . . . 16
-t, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38
?= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48, 51
-v . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
-w . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 81
-W . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 75, 81
@
-w, and -C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 @ (in commands) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33
-w, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 @, and define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
-W, and recursion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
-w, disabling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42 [
[. . .] (wildcard characters). . . . . . . . . . . . . . . . . . . . . 16
__.SYMDEF . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.
.a (archives) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 103 ~
.c . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
~ (tilde) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
.C . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
.cc . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
+
.ch . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
+, and define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
.d . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31
+= . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
.def . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
.dvi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
.f . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85
\
.F . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 \ (backslash), for continuation lines . . . . . . . . . . . . . 4
.info . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 \ (backslash), in commands . . . . . . . . . . . . . . . . . . . . 34
.l . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 \ (backslash), to quote % . . . . . . . . . . . . . . . 19, 28, 64
.ln . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86
.mod . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 A
.o . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84, 85 algorithm for directory search . . . . . . . . . . . . . . . . . . 20
.p . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 all (standard target) . . . . . . . . . . . . . . . . . . . . . . . . . . 74
.PRECIOUS intermediate files . . . . . . . . . . . . . . . . . . . 90 appending to variables . . . . . . . . . . . . . . . . . . . . . . . . . 52
.r . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 ar . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.s . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 archive . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 101
.S . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 archive member targets . . . . . . . . . . . . . . . . . . . . . . . 101
.sh . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87 archive symbol directory updating . . . . . . . . . . . . 102
.sym . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85 archive, and -j . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 102
.tex . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 archive, and parallel execution . . . . . . . . . . . . . . . . 102
.texi . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 archive, suffix rule for . . . . . . . . . . . . . . . . . . . . . . . . 103
.texinfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 Arg list too long . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
.txinfo . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 arguments of functions . . . . . . . . . . . . . . . . . . . . . . . . . 63
.w . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 as . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 85, 88
.web . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 assembly, rule to compile . . . . . . . . . . . . . . . . . . . . . . 85
.y . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 86 automatic generation of dependencies . . . . . . 10, 30
Index of Concepts 139
$ .SUFFIXES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24, 97
$% . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
$(%D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 /
$(%F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 /usr/gnu/include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
$(*D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 /usr/include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
$(*F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 /usr/local/include . . . . . . . . . . . . . . . . . . . . . . . . . . 10
$(?D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
$(?F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94 ?
$(@D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
? (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 92
$(@F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93
?D (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 94
$(^D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
?F (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 94
$(^F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
$(<D) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
$(<F) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 94
@
$* . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 @ (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 92
$*, and static pattern . . . . . . . . . . . . . . . . . . . . . . . . . . 28 @D (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 93
$? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 @F (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 93
$@ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
$+ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 +
$^ . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92 + (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 92
$< . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 92
^
% ^ (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 92
% (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 92 ^D (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 94
%D (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 93 ^F (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 94
%F (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 93
<
* < (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 92
<D (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 94
* (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . . 93
<F (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 94
* (automatic variable), unsupported bizarre usage
. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109
*D (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 93 A
*F (automatic variable) . . . . . . . . . . . . . . . . . . . . . . . . 93 addprefix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
addsuffix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
. AR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
ARFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.DEFAULT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24, 96
AS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.DEFAULT, and empty commands . . . . . . . . . . . . . . . 43
ASFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.DELETE_ON_ERROR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
.EXPORT_ALL_VARIABLES . . . . . . . . . . . . . . . . . . . 25, 40
.IGNORE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25, 36
B
.INTERMEDIATE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 basename . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
.PHONY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22, 24
.POSIX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 C
.PRECIOUS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24, 37 CC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.SECONDARY. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25 CFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
.SILENT . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25, 33 CO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
146 GNU make
COFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 M
COMSPEC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34 MAKE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38, 47
CPP . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 MAKECMDGOALS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 74
CPPFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
CTANGLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
CWEAVE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 MAKEFILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11, 40
CXX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 MAKEFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
CXXFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 MAKEINFO . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
MAKELEVEL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40, 47
D MAKEOVERRIDES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
define . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 MFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
dir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
N
E notdir . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
else . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
endef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
O
endif . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 origin . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 70
export . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39 OUTPUT_OPTION . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
override . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 53
F P
FC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
patsubst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48, 64
FFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
PC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
filter . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
PFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
filter-out. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
findstring. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
firstword . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 R
foreach . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 69 RFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
RM . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
G S
GET . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
shell . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 71
GFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
SHELL . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
GNUmakefile. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
SHELL (command execution) . . . . . . . . . . . . . . . . . . . 33
GPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 66
strip . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 65
I subst . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26, 64
ifdef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 suffix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 67
ifeq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 SUFFIXES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 97
ifndef . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60
ifneq . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 60 T
include . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
TANGLE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
TEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
J TEXI2DVI . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
join . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
U
L unexport . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
LDFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
LEX . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 V
LFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89 vpath . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18, 19
Index of Functions, Variables, & Directives 147
VPATH . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18 words . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
W
WEAVE . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88 Y
wildcard . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17, 68 YACC . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
word . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 YACCR . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 88
wordlist . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68 YFLAGS . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
148 GNU make
i
Short Contents
1 Overview of make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 An Introduction to Makefiles . . . . . . . . . . . . . . . . . . . . . . . 3
3 Writing Makefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
4 Writing Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
5 Writing the Commands in Rules . . . . . . . . . . . . . . . . . . . . 33
6 How to Use Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
7 Conditional Parts of Makefiles . . . . . . . . . . . . . . . . . . . . . 59
8 Functions for Transforming Text . . . . . . . . . . . . . . . . . . . . 63
9 How to Run make . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73
10 Using Implicit Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83
11 Using make to Update Archive Files . . . . . . . . . . . . . . . . 101
12 Features of GNU make . . . . . . . . . . . . . . . . . . . . . . . . . 105
13 Incompatibilities and Missing Features . . . . . . . . . . . . . . . 109
14 Makefile Conventions . . . . . . . . . . . . . . . . . . . . . . . . . . . 111
Appendix A Quick Reference . . . . . . . . . . . . . . . . . . . . . . . . 123
Appendix B Errors Generated by Make . . . . . . . . . . . . . . . . . 129
Appendix C Complex Makefile Example . . . . . . . . . . . . . . . . 131
Index of Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
Index of Functions, Variables, & Directives . . . . . . . . . . . . . . . 145
ii GNU make
iii
Table of Contents
1 Overview of make . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 How to Read This Manual . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 Problems and Bugs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 An Introduction to Makefiles . . . . . . . . . . . . . . . 3
2.1 What a Rule Looks Like . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
2.2 A Simple Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
2.3 How make Processes a Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2.4 Variables Make Makefiles Simpler. . . . . . . . . . . . . . . . . . . . . . . . . 6
2.5 Letting make Deduce the Commands . . . . . . . . . . . . . . . . . . . . . . 7
2.6 Another Style of Makefile . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.7 Rules for Cleaning the Directory. . . . . . . . . . . . . . . . . . . . . . . . . . 8
3 Writing Makefiles . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.1 What Makefiles Contain . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
3.2 What Name to Give Your Makefile . . . . . . . . . . . . . . . . . . . . . . . 9
3.3 Including Other Makefiles . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
3.4 The Variable MAKEFILES . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.5 How Makefiles Are Remade . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
3.6 Overriding Part of Another Makefile . . . . . . . . . . . . . . . . . . . . . 13
4 Writing Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.1 Rule Syntax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
4.2 Using Wildcard Characters in File Names . . . . . . . . . . . . . . . . 16
4.2.1 Wildcard Examples . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
4.2.2 Pitfalls of Using Wildcards . . . . . . . . . . . . . . . . . . . . . 17
4.2.3 The Function wildcard . . . . . . . . . . . . . . . . . . . . . . . . 17
4.3 Searching Directories for Dependencies . . . . . . . . . . . . . . . . . . . 18
4.3.1 VPATH: Search Path for All Dependencies . . . . . . . . 18
4.3.2 The vpath Directive . . . . . . . . . . . . . . . . . . . . . . . . . . . 19
4.3.3 How Directory Searches are Performed . . . . . . . . . . 20
4.3.4 Writing Shell Commands with Directory Search . . 21
4.3.5 Directory Search and Implicit Rules . . . . . . . . . . . . . 21
4.3.6 Directory Search for Link Libraries . . . . . . . . . . . . . . 21
4.4 Phony Targets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
4.5 Rules without Commands or Dependencies . . . . . . . . . . . . . . . 23
4.6 Empty Target Files to Record Events . . . . . . . . . . . . . . . . . . . . 24
4.7 Special Built-in Target Names . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
4.8 Multiple Targets in a Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.9 Multiple Rules for One Target . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
4.10 Static Pattern Rules . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
iv GNU make