Rectifying autogenerated dependencies (from gcc) in make
I have the following makefile (fragment)
SRC_DIR = src
OBJ_DIR = obj
DEP_DIR = dep
BIN_DIR = .
SRC_FILES := $(wildcard $(SRC_DIR)/*.cpp)
OBJ_FILES := $(patsubst $(SRC_DIR)/%.cpp,$(OBJ_DIR)/%.o,$(SRC_FILES))
DEP_FILES := $(patsubst $(SRC_DIR)/%.cpp,$(DEP_DIR)/%.d,$(SRC_FILES))
# Development build directive
dev: $(DEP_FILES) $(OBJ_FILES)
$(CPPC) $(LIBS) $(FLAGS_DEV) $(OBJ_FILES) 开发者_如何转开发-o $(BIN_DIR)/$(PROJECT)
# Object file directives
$(OBJ_DIR)/%.o: $(SRC_DIR)/%.cpp $(DEP_DIR)/%.d
$(CPPC) -c $(FLAGS_DEV) $< -o $@
# Dependency directives
$(DEP_DIR)/%.d: $(SRC_DIR)/%.cpp
$(CPPC) -MM -MD $< -o $@
include $(DEP_FILES)
When I run make dev
I see the following
makefile:59: dep/area.d: No such file or directory
makefile:59: dep/avatar.d: No such file or directory
makefile:59: dep/board.d: No such file or directory
makefile:59: dep/socket.d: No such file or directory
g++ -MM -MD src/socket.cpp -o dep/socket.d
g++ -MM -MD src/board.cpp -o dep/board.d
g++ -MM -MD src/avatar.cpp -o dep/avatar.d
g++ -MM -MD src/area.cpp -o dep/area.d
g++ -c -ggdb3 -ansi -Wall -Werror -pedantic-errors src/area.cpp -o obj/area.o
g++ -c -ggdb3 -ansi -Wall -Werror -pedantic-errors src/avatar.cpp -o obj/avatar.o
g++ -c -ggdb3 -ansi -Wall -Werror -pedantic-errors src/board.cpp -o obj/board.o
g++ -c -ggdb3 -ansi -Wall -Werror -pedantic-errors src/socket.cpp -o obj/socket.o
g++ -ggdb3 -ansi -Wall -Werror -pedantic-errors obj/area.o obj/avatar.o obj/board.o obj/socket.o -o ./game
When changing src/socket.h
(a file upon which the others all depend) and running make
, I expected it would rebuild the entire project, but it only emits one action
g++ -ggdb3 -ansi -Wall -Werror -pedantic-errors obj/area.o obj/avatar.o obj/board.o obj/socket.o -o ./game
I believe I'm generating the automatic dependencies correctly - so I get the sense I'm simply not using them properly. Where have I gone wrong? I know the makefile:59:...
errors are a clue, but I've never worked with auto generated dependencies before.
Thanks in advance; Cheers!
Unfortunately, your *.d
files aren't getting their full dependencies; they depend on all of the header files, too. One way to fix this would be to add an extra line to the %.d
directive:
# Dependency directives
$(DEP_DIR)/%.d: $(SRC_DIR)/%.cpp
$(CPPC) -MM -MD $< -o $@
sed -i 'p;s|$(OBJ_DIR)/\(.*\)\.o:|$(DEP_DIR)/\1.d:|' $@
If the -i
scares you, you could try sponge
(in the moreutils
package on my distribution).
精彩评论