Professional Documents
Culture Documents
Makefiles
What is make?
Make is a utility to track dependencies in a system of files, and to
automate compilations
Examples of dependencies - C program
Objs depend on source for that object
Objs depend on headers
Binary depends on objs
Libs depend on objs
Uses files called Makefiles as configuration
Hierarchical dependencies determined by mod time - if any
dependency is newer than target, remake target with command
Many things described here are specific to GNU make (gmake in the gnu
locker on Athena).
Makefiles - basic
naming - GNUmakefile, makefile, Makefile in that order
"Makefile" is recommended - fairly standard, appears at top of dir
use GNUmakefile only for gmake-specific makefiles.
explicit rules (basic)
TARGET... : DEPENDENCIES... [; COMMAND]
COMMAND
...
...
Default goal is first rule that does not start with a period
Comments - `#' starts
1 of 6 28/08/10 22:19
Intro To UNIX Software Development - Mak... http://www.cs.bu.edu/teaching/c/makefile...
Commands
Each line is run in a different shell, if you want to cause a command to
affect the next, use ; in the command so that make interprets it as a
single command:
foo:
cd foodir ; doit
or
foo:
cd foodir ; \
doit
Variables
Naming
Case sensitive
Any characters except trailing whitespace, `:', `#', or `='
Should avoid all but alphanumerics and underscore
Setting
VARNAME = VARVALUE (note: trailing spaces not truncated)
make VARNAME=value
inherits most environment variables except $SHELL and a few
others
Referencing: $(VARNAME)
Standard variables for program Makefiles
$(OBJS)
$(SHELL)
$(MAKE)
$(CC)
$(CFLAGS)
$(INCLUDE)
$(LDFLAGS)
Automatic variables - make sets these in special cases for substitutions
and other purposes. More later
2 of 6 28/08/10 22:19
Intro To UNIX Software Development - Mak... http://www.cs.bu.edu/teaching/c/makefile...
and
foo: blah
doit
bar: blah
doit
baz: blah
doit
are equivalent
Multiple rules for a single target are acceptable - make will check all
files that depend on a given updated file. Useful for adding
dependencies.
FILES = foo bar baz
foo: bar
doit
bar baz: blah
dothat
$(FILES): gag
# no commands allowed here
will add dependency on gag to $(FILES) (ie, if gag changes, files will
get rebuild)
Multiple commands are NOT acceptable - there can be only one set of
commands for a given target; all other rules using the target must not
have commands.
exception - double-colon rules' commands are independent.
Implicit rules
Default rules, either internal to make or user-defined, which set
dependencies based on classes of filename.
Pattern rules
TARGET-PATTERN: DEP-PATTERN
COMMANDS
eg
%.o: %.c
$(CC) -c $(CFLAGS) $(CPPFLAGS) $< -o $@
Can be chained, i.e. .y -> .c -> .o -> the .c file does not exist, but make
considers it for dependency purposes. Make deletes the temp files
when it's done.
3 of 6 28/08/10 22:19
Intro To UNIX Software Development - Mak... http://www.cs.bu.edu/teaching/c/makefile...
Example:
OBJS = foo.o bar.o
$(OBJS): %.o: %.c
$(CC) -c $(CFLAGS) $< -o $@
makedepend
Parses .c files for other files they include, then writes them in the form
of a dependency rule in a makefile.
4 of 6 28/08/10 22:19
Intro To UNIX Software Development - Mak... http://www.cs.bu.edu/teaching/c/makefile...
For example, if foo.c #includes foo.h, bar.h, and quux.c, it will add a
rule of the form:
foo.c: foo.h bar.h quux.c
to the makefile
old way to use:
dep:
makedepend $(CPPFLAGS) *.c
will remake all dependencies when make dep is called, and write them
to the end of the current makefile
gmake way to use (as suggested by Mike Whitson):
include *.d
%.d: %.c
$(SHELL) -ec '$(CC) -M \
$(CPPFLAGS) $< | \
sed '\''s/$*.o/& $@/g'\'' \
> $@'
Recursion
Useful if you want to have subsections of code in subdirs with their own
makefiles
Basic syntax:
subsystem:
cd subdir; $(MAKE)
Always use $(MAKE) rather than passing `make' - inherits path to make
and command line arguments
Exporting variables to sub-makes normally only happens for variables
defined in the environment or the command line.
export VARIABLE
unexport VARIABLE
Archives (libraries)
A specific member of an archive can be target or dependency:
ARCHIVE(MEMBERS)
5 of 6 28/08/10 22:19
Intro To UNIX Software Development - Mak... http://www.cs.bu.edu/teaching/c/makefile...
eg
libfoo.a(foo.o bar.o): foo.o bar.o
ar -r libfoo.a foo.o bar.o
Other functionality
GNU gmake supports conditionals, loops, functions, and more
Look in info pages (M-x info in emacs) for more information
Other options
imake and xmkmf
autoconf and configure
6 of 6 28/08/10 22:19