我试图通过向vpath/VPATH添加源路径来使用我的Makefile (为Windows做准备)。这似乎微不足道,但由于某种原因,我无法使它发挥作用。
我的目录结构如下:
├── Makefile
├── out\
└── src\
└── hello.cpp我的Makefile是:
TGT=out
OBJ=hello.o
VPATH=src
# vpath %.cpp src
all: $(TGT)\app.exe
$(TGT)\app : $(TGT)\$(OBJ)
g++ $^ -o $@
$(TGT)\%.o : %.cpp
g++ -Wall -Wextra -Werror -c $<改到vpath对我没有帮助。我这里好像出了什么根本问题。我看到的错误是:
make: *** No rule to make target `out\hello.o', needed by `out\app'. Stop. 编辑:make -d的调试输出
Considering target file `all'.
File `all' does not exist.
No implicit rule found for `all'.
Considering target file `out\app'.
File `out\app' does not exist.
Considering target file `out\hello.o'.
File `out\hello.o' does not exist.
Looking for an implicit rule for `out\hello.o'.
Trying pattern rule with stem `hello'.
Looking for a rule with intermediate file `out\hello.cpp'.
Avoiding implicit rule recursion.
Trying pattern rule with stem `hello.cpp'.
No implicit rule found for `out\hello.o'.
Finished prerequisites of target file `out\hello.o'.
Must remake target `out\hello.o'.发布于 2018-03-12 19:16:13
正如MadScientist指出的那样,你应该避免反斜杠,因为它们有这样奇怪的结果,如果你在整个Makefile中使用正斜杠,你就不会遇到这个问题,也就是说可以绕过它们。
这里有一些不对劲的地方:
all之后的第一个规则应该将$(TGT)\app.exe作为目标。%之前的反斜杠将将其转换为文字%,然后转义反斜杠。一旦您修复了所有这些,您应该会发现vpath按预期工作,完整的固定Makefile是
TGT=out
OBJ=hello.o
vpath %.cpp src
all: $(TGT)\app.exe
$(TGT)\app.exe : $(TGT)\$(OBJ)
g++ $^ -o $@
$(TGT)\\%.o : %.cpp
g++ -Wall -Wextra -Werror -c $< -o $@https://stackoverflow.com/questions/49240736
复制相似问题