Hi,
I'm revising my code to make it a bit cleaner and right now I'm working on the makefile.
I'm having a problem with defining a pattern rule that uses % to access another variable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
|
# program output name
OUT=a.out
# compiler flags
FLAGS=-ggdb -std=c++0x
# library dependencies
LIBS=-lSDL -lSDL_image -lboost_system -lboost_chrono -lGL
# where to store built objects
BIN=bin
# The objects to build
OBJECTS=Geometry.o Enemy.o Surface.o Player.o SDLWindow.o Path.o Territory.o Board.o Diminisher.o Main.o
# Header Dependencies per object
Geometry_H=Geometry.h
Enemy_H=Enemy.h $(Geometry_H)
Surface_H=Surface.h $(Geometry_H)
Player_H=Player.h $(Geometry_H) $(Surface_H)
SDLWindow_H=SDLWindow.h $(Surface_H)
Path_H=Path.h $(Geometry_H)
Territory_H=Territory.h $(Geometry_H)
Board_H=Board.h $(Territory_H) $(Geometry_H) $(Player_H) $(Path_H) $(Enemy_H)
Diminisher_H=Diminisher.h $(SDLWindow_H) $(Geometry_H) $(Surface_H) $(Board_H)
Main_H=$(Diminisher_H)
# make program
default: $(BIN) $(addprefix $(BIN)/, $(OBJECTS))
g++ -o $(OUT) $(FLAGS) $(addprefix $(BIN)/, $(OBJECTS)) $(LIBS)
# create object bin directory
$(BIN):
@ if [ ! -d "./$(BIN)" ]; then \
mkdir $(BIN); \
fi
@echo "mkdir $(BIN)"
# command to make an object
$(BIN)/%.o: %.cpp $(%_H)
g++ -o $@ $(FLAGS) -c $<
# delete all objects and program
clean:
rm -rf $(BIN)
rm -f $(OUT)
|
Previously I had all the make rules explicitly written out. For example, Geometry read:
1 2
|
$(BIN)/Geometry.o: Geometry.cpp $(Geometry_H)
g++ -o $@ $(FLAGS) -c $<
|
The problem I'm having is in bold.
In the new code, $(%_H) isn't expanding to Geometry_H.
I've tried many other variants such as:
$($%_H)
$( $(value %)_H )
I'm hoping someone knows the correct syntax.
The project will still build with this code, but if I edit a header file and run make without cleaning first, make will not rebuild the objects that depend on this header as defined by the *_H variables.
The old code would rebuild the targets that depended on the edited header.