【问题标题】:Rectifying autogenerated dependencies (from gcc) in make在 make 中纠正自动生成的依赖项(来自 gcc)
【发布时间】:2011-01-13 15:46:32
【问题描述】:

我有以下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:... 错误是一个线索,但我以前从未使用过自动生成的依赖项。

提前致谢;干杯!

【问题讨论】:

    标签: dependencies makefile gnu-make


    【解决方案1】:

    不幸的是,您的*.d 文件没有获得它们的全部依赖关系;它们也依赖于所有的头文件。解决此问题的一种方法是在 %.d 指令中添加额外的一行:

    # Dependency directives
    $(DEP_DIR)/%.d: $(SRC_DIR)/%.cpp
      $(CPPC) -MM -MD $< -o $@
      sed -i 'p;s|$(OBJ_DIR)/\(.*\)\.o:|$(DEP_DIR)/\1.d:|' $@
    

    如果-i 吓到你,你可以试试sponge(在我的发行版中的moreutils 包中)。

    【讨论】:

    • 感谢您的提示 - 实际上我最终使用了 sed -i 's|\(.*\)\.o:|$(OBJ_DIR)/\1.o $(OBJ_DIR)/\1.d:|' $@。干杯!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-12
    • 2021-05-03
    • 2011-07-10
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    相关资源
    最近更新 更多