GNU Make
Martin Všetička
2011
Martin Všetička
2011
Initial release in 1977.
Original Make author: Stuart Feldman
GNU Make authors: Richard Stallman, Roland McGrath and Paul Smith
Development: In active development. New features are being added.
Project homepage: http://savannah.gnu.org/projects/make/
Manual: http://www.gnu.org/software/make/manual/
Target is usually a file that is generated by a program.
Prerequisite is a file that is used as input to create the target.Recipe - shell commands
Rule explains how and when to remake certain files which are the targets of the particular rule.Rule syntax:
target ... : prerequisites ... myprog: prog.c lib.c
recipe gcc -o myprog prog.c lib.c
...
.ONESHELL special target is in effect.
Example:
.PHONY test clean
test:
tmpDir="/tmp" # variable in shell
mkdir $$tmpDir/my_project_name # does not know the 'tmpDir' variable!
clean:
-rm -f *.o # first '-' is processed via make
# and suppresses errors of the rm command
+echo "Done" # The line is run in a subshell despite
# the make's flags '-n' (just print), '-t' (touch) and
# '-q' (question)
GNUmakefile, makefile and
Makefile.
explicit rules,implicit rules,variable definitions,
directives, and
comments (# character).
edit : main.o kbd.o command.o insert.o \
search.o files.o
cc -o edit main.o kbd.o command.o insert.o \
search.o files.o
main.o : main.c defs.h
cc -c main.c
kbd.o : kbd.c defs.h command.h
cc -c kbd.c
command.o : command.c defs.h command.h
cc -c command.c
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
clean :
rm edit main.o kbd.o command.o search.o files.o
default goal: the target for make to consider,
if you do not otherwise specify one.
default goal is the target of the first rule in the first makefile.$ make
$ make targetname
include directive
include filenames...
include foo *.mk
is equivalent to:
include foo a.mk b.mk c.mk
include directive (2)
-I or --include-dir.
/usr/gnu/include,
/usr/local/include,
/usr/include.
Phony targets
phony targets:
$ make clean
.PHONY: clean # make's rule: target '.PHONY', prerequisite 'clean'
clean:
rm *.o temp
Static Pattern Rules
Syntax:
targets ...: target-pattern: prereq-patterns ...
recipe
...
target-pattern to extract a part of the target name,
called the stem. This stem is substituted into each of the
prereq-patterns to make the prerequisite names
(one from each prereq-pattern).
objects = foo.o bar.o
all: $(objects)
$(objects): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
variable is a name
(any case-sensitive sequence of characters not
containing ':', '#', '=')
defined in a makefile to represent a string of text,
called the variable's value.
objects = program.o foo.o utils.o # set variable's value
program : $(objects) # variable usage
cc -o program $(objects) # variable usage
Recursively expanded variable:
define directive for values with newlines).
foo = $(bar)
bar = $(ugh)
ugh = Hi there!
all:;echo $(foo) # prints 'Hi there!' (whitespace is gone!)
CFLAGS = $(include_dirs) -O # It is OK because value is ...
include_dirs = -Ifoo -Ibar # ... stored verbatim! (advantage)
CFLAGS = $(CFLAGS) -O # INFINITE LOOP! (disadvantage)
Simply expanded variables:
x := foo
y := $(x) bar
x := later
is equivalent to
y := foo bar
x := later
variable += text to appendobjects = main.o foo.o bar.o utils.o
objects += another.o
objects := main.o foo.o bar.o utils.o
objects += another.o
substitution reference
$(variable:a=b)substitution reference replaces every a
at the end of a word with b in that value, and
substitute the resulting string.
foo := a.o b.o c.o
bar := $(foo:.o=.c) # variable 'bar' contains: 'a.c b.c c.c'
patsubst expansion function
$ make variablename=value # make looks for '=' character
Note: Ordinary assignments of the same variable in the
makefile are ignored. You may override them though:
override var1 = value # recursive expanded variable
override var2 := value # simple expanded variable
# To append more text to a variable defined on the command line:
override var3 += more text
Why is it useful?
CFLAGS=-g # serves as default value
cc -c $(CFLAGS) foo.c
and in shell run: $ make CFLAGS='-g -O'.
Automatic variables:
lib: foo.o bar.o lose.o win.o
ar r lib $^ # $^ contains the names of all the ...
# ... prerequisites, with spaces between them.
$@, $<,
$?, $^,
etc.
Environmental variables
make sees
when it starts up is transformed into a make variable.
$(function arguments)$(subst ee,EE,feet on the street)
Returns: fEEt on the strEEtall - Make all the top-level targets the makefile knows about.
clean -
Delete all files that are normally created by running make.
install -
Copy the executable file into a directory that users typically
search for commands; 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.
check, test -
Perform self tests on the program this makefile builds.
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.
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.
Implicit rules tell make how to use customary
techniques so that you do not have to specify them in detail when you want to use them.
.c file and makes a .o file.
So make applies the implicit rule for C compilation when
it sees this combination of file name endings.
foo.o: foo.c
foo : foo.o bar.o
cc -o foo foo.o bar.o $(CFLAGS) $(LDFLAGS)
chaining is
occurring.
n.o is made automatically from n.c
with a recipe:
$(CC) $(CPPFLAGS) $(CFLAGS) -c
n.o is made automatically from
n.cc, n.cpp, or n.C with a
recipe of the form: $(CXX) $(CPPFLAGS) $(CXXFLAGS) -c
$ make -p # prints make's rules
.SUFFIXES, default value:.out, .a, .ln, .o, .c, .cc, .C, .cpp, .p, ...,
.h, ...
CC -
Program for compiling C programs; default 'cc'.
CXX -
Program for compiling C++ programs; default 'g++'.
CPP -
Program for running the C preprocessor, with results to standard
output; default $(CC) -E.
CFLAGS -
Extra flags to give to the C compiler.
CXXFLAGS -
Extra flags to give to the C++ compiler.
CPPFLAGS -
Extra flags to give to the C preprocessor and programs that use
it (the C and Fortran compilers).
cc -M to generate make's rule.
name.d from a C source file called
name.c:
%.d: %.c
@set -e; rm -f $@; \
$(CC) -M $(CPPFLAGS) $< > $@.$$$$; \
sed 's,\($*\)\.o[ :]*,\1.o $@ : ,g' < $@.$$$$ > $@; \
rm -f $@.$$$$
main.o : main.c defs.h
into:
main.o main.d : main.c defs.h