Chapter 15: Makefile Conventions 149
15 Makefile Conventions
This chapter describes conventions for writing the Makefiles for GNU programs. Using Au-
tomake will help you write a Makefile that follows these conventions. For more information
on portable Makefiles, see posix and Section “Portable Make” in Autoconf .
15.1 General Conventions for Makefiles
Every Makefile should contain this line:
SHELL = /bin/sh
to avoid trouble on systems where the SHELL variable might be inherited from the environ-
ment. (This is never a problem with GNU make.)
Different make programs have incompatible suffix lists and implicit rules, and this some-
times creates confusion or misbehavior. So it is a good idea to set the suffix list explicitly
using only the suffixes you need in the particular Makefile, like this:
.SUFFIXES:
.SUFFIXES: .c .o
The first line clears out the suffix list, the second introduces all suffixes which may be
subject to implicit rules in this Makefile.
Don’t assume that . is in the path for command execution. When you need to run
programs that are a part of your package during the make, please make sure that it uses ./
if the program is built as part of the make or $(srcdir)/ if the file is an unchanging part
of the source code. Without one of these prefixes, the current search path is used.
The distinction between ./ (the build directory) and $(srcdir)/ (the source directory)
is important because users can build in a separate directory using the ‘--srcdir’ option to
configure. A rule of the form:
foo.1 : foo.man sedscript
sed -f sedscript foo.man > foo.1
will fail when the build directory is not the source directory, because foo.man and sedscript
are in the source directory.
When using GNU make, relying on ‘VPATH’ to find the source file will work in the case
where there is a single dependency file, since the make automatic variable ‘$<’ will represent
the source file wherever it is. (Many versions of make set ‘$<’ only in implicit rules.) A
Makefile target like
foo.o : bar.c
$(CC) -I. -I$(srcdir) $(CFLAGS) -c bar.c -o foo.o
should instead be written as
foo.o : bar.c
$(CC) -I. -I$(srcdir) $(CFLAGS) -c $< -o $@
in order to allow ‘VPATH’ to work correctly. When the target has multiple dependencies,
using an explicit ‘$(srcdir)’ is the easiest way to make the rule work well. For example,
the target above for foo.1 is best written as:
foo.1 : foo.man sedscript
sed -f $(srcdir)/sedscript $(srcdir)/foo.man > $@
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
Comentarios a estos manuales