INSTALLDIR = /home/faculty/kena/tmp/
$(INSTALLDIR)/program : program
cp program $(INSTALLDIR)
make
as $(I)NSTALLDIRprogram: main.o input.o output.o
g++ main.o input.o output.o -o program
EXECUTABLE = program
OBJECTS = main.o input.o output.o
$(EXECUTABLE): $(OBJECTS)
g++ $(OBJECTS) -o $(EXECUTABLE)
Automatic Variable | Definition |
---|---|
$@ | The target of the rule |
$< | The first dependency |
$^ | All of the dependencies |
$? | All of the dependencies newer than the target of the rule |
$* | The stem of a pattern matching rule. If you are building input.o from input.c , the stem is “input”. This automatic variable only works in pattern matching rules. |
program: input.o output.o
g++ input.o output.o -o program
g++ $^ -o $@
input.o: input.c defs.h
g++ -c input.c -o input.o
g++ -c $< -o $@
output.o: output.c defs.h
g++ -c output.c -o output.o
g++ -c $< -o $@
lab5.tar: README makefile lab5.cpp
tar rf lab5.tar $?
VPATH = $(HOME)/csci3308/src/lab05
lab05.o: lab05.c
g++ -c lab05.c -o lab05.o
g++ -c lab05.c -o lab05.o
g++: lab05.c: No such file or directory
VPATH = $(HOME)/csci3308/src/lab05
lab05.o: lab05.c
g++ -c $< -o $@
$ make
g++ -c /home/faculty/kena/csci3308/src/lab05/lab05.c -o lab05.o
CXX = g++
CFLAGS = -c -g
program: main.o input.o output.o
$(CXX) $^ -o $@
main.o: main.cpp defs.h
$(CXX) $(CFLAGS) $<
input.o: input.cpp defs.h
$(CXX) $(CFLAGS) $<
output.o: output.cpp defs.h
$(CXX) $(CFLAGS) $<
Thus our rules in the previous examples that took care of compiling files can be expressed as:
%.o: %.cpp
$(CXX) $(CFLAGS) $<
This is not exactly the same as our previous rules, why? Does it matter?
Make supports pattern matching through the presence of the character “%” in rules
%.o: %.cpp
$(CXX) $(CFLAGS) $<
If you type “make input.o” the rule becomes
input.o: input.cpp
$(CXX) $(CFLAGS) $<
Note: automatic variables are required. Why?
If you create a makefile that contains just the following rule:
program: program.o
$(CC) $(CFLAGS) $^ -o $@
Make will act as if you had also included the following rule
%.o: %.cpp:
$(CC) $(CFLAGS) -c $&