2

我有以下makefile(片段)

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)

当我跑步时,make dev我看到以下内容

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

当更改src/socket.h(其他人都依赖的文件)并运行make时,我希望它会重建整个项目,但它只发出一个动作

g++ -ggdb3 -ansi -Wall -Werror -pedantic-errors obj/area.o obj/avatar.o obj/board.o obj/socket.o  -o ./game

我相信我正确地生成了自动依赖项 - 所以我觉得我根本没有正确使用它们。我哪里出错了?我知道makefile:59:...错误是一个线索,但我以前从未使用过自动生成的依赖项。

提前致谢; 干杯!

4

1 回答 1

2

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).

于 2010-02-05T19:42:02.517 回答