我有一个包含多个Makefile的大型项目,我需要修改项目的构建顺序。但是,我似乎无法理解何时评估作为先决条件的变量。我读过的所有东西似乎都表明在构建依赖图之前解析了整个Makefile,所以我不明白为什么这个示例不起作用:
OBJECTS=main.o
EXE_NAME=test.exe
$(EXE_NAME): $(OBJECTS)
@echo PREREQUISITES: $^
@echo OBJECTS: $(OBJECTS)
$(CXX) $(CXXFLAGS) $(OBJECTS) -o $@
#include shape.mk
#----VV---Contents of shape.mk---VV----
OBJECTS+=Shape.o
OBJECTS+=Square.o
Shape.o: Shape.hpp
Square.o: Shape.hpp Square.hpp
#----^^----End of shape.mk-------^^----
main.o: Square.hpp
.PHONY: clean
clean:
-rm -fr $(OBJECTS) $(EXE_NAME)
然而,当我运行这个Makefile时,我得到:
PREREQUISITES: main.o
OBJECTS: main.o Shape.o Square.o
g++ main.o Shape.o Square.o -o test.exe
g++: Shape.o: No such file or directory
g++: Square.o: No such file or directory
make: *** [test.exe] Error 1
为什么在构建依赖图时OBJECT没有正确的值?
答案 0 :(得分:4)
是的,在构建依赖关系图之前解析整个Makefile,但是按顺序读取规则和定义。您将test.exe
规则定义为
test.exe: main.o
...
然后,您继续重新定义OBJECTS
,但您已经提交了该规则,因此Make将用于构建图表。
试试这个:
$(EXE_NAME): # This will be first, the default target.
include shape.mk
$(EXE_NAME): $(OBJECTS)
...