- Dependencies are based on a target and several files on which the target
is dependent.
- If one of the dependent files is newer than the target file then a set
of rules (commands) will be executed to bring the target up to date.
target : dependencies
rules
StringClass.o : StringClass.cc StringClass.h
g++ -Wall -c StringClass.cc
All rules must be one TAB in from left margin. There can be multiple
lines of rules for each dependency.
- make will create a tree of dependencies to ensure that all targets are
up to date
StringTest : StringTest.o StringClass.o
g++ -o StringTest StringTest.o StringClass.o
StringTest.o : StringTest.cc StringClass.h
g++ -Wall -c StringTest.cc
StringClass.o : StringClass.cc StringClass.h
g++ -Wall -c StringClass.cc
make defaults to building the first target specified in the make file.
It looks at each of the dependencies and ensures that they are up to
date performing the defined steps if they are not.
- Suffix rules, based on file extensions, can define generic dependencies
.cc.o :
g++ -Wall -c $<
This defines the generic build a .o from .c rule. It uses one of make's
builtin macros described below.
Note the reverse of the target/dependency extension.
- make has several builtin macros
- $< - the current dependcy file
- $@ - the current target file
- $* - the base name of the current target
- $? - all dependencies that are newer than the target
- Macros allow you to define constants used in the make file.
name = definition
C++ = g++
C++OPTS = -Wall
LDOPTS = -L/u/csc173/ca1cs173/lib
STRINGTESTOBJS = StringTest.o StringClass.o
.cc.o :
$(C++) $(C++OPTS) -c $<
StringTest : $(STRINGTESTOBJS)
$(C++) -o $@ $(STRINGTESTOBJS) $(LDOPTS)
- Pattern matching rules are a little more general than suffix rules. %
is used as a wildcard character. The rule will be used if a target line
matches the pattern.
C++ = g++
C++OPTS = -Wall
LDOPTS = -L/u/csc173/ca1cs173/lib
STRINGTESTOBJS = StringTest.o StringClass.o
%.o : %.cc
$(C++) $(C++OPTS) -c $<
StringTest : $(STRINGTESTOBJS)
$(C++) -o $@ $(STRINGTESTOBJS) $(LDOPTS)
StringTest.o : StringTest.cc StringClass.h
StringClass.o : StringClass.cc StringClass.h