使用VPATH/vpath的正确方法? [英] Right way to use VPATH/vpath?

查看:54
本文介绍了使用VPATH/vpath的正确方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图通过将源路径添加到vpath/中来使用我的Makefile( Make for Windows )VPATH.这看似微不足道,但由于某种原因我无法使其正常工作

I am trying to use my Makefile (Make for Windows) by adding source paths to vpath/VPATH. This seems trivial but for some reason I am unable to get it to work

我的目录结构是这样的:

My directory structure is like this:

├── Makefile
├── out\
└── src\
    └── hello.cpp

我的Makefile是:

My Makefile is:

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并没有帮助我.我似乎在这里有些根本上的错误.我看到的错误是:

changing to vpath didn't help me. I seem to have something fundamentally wrong here. The error I see is:

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

推荐答案

正如MadScientist指出的那样,您应该避免使用反斜杠,因为这样会产生奇怪的结果,如果您在整个Makefile中都使用了正斜杠,那么您就不会遇到这个问题,表示可以解决这些问题.

As MadScientist points out you should avoid backslashes as they have odd results like this, had you used forward slashes throughout your Makefile you wouldn't have had this issue, that said it is possible to work around them.

这里有些错误:

  • 您没有发布要再次使用的同一Makefile, all 之后的第一个规则应以 $(TGT)\ app.exe 为目标.
  • 模式规则中之前的反斜杠会将其转换为原义的,转义反斜杠
  • 您忘记告诉gcc在哪里输出目标文件
  • You haven't posted the same Makefile you're using again, the first rule after all should have $(TGT)\app.exe as a target.
  • A backslash before % in a pattern rule will turn it into a literal %, escape the backslash
  • You forgot to tell gcc where to output the object file

修复了所有这些问题后,您应该会发现 vpath 可以按预期工作,完整的修复Makefile是

Once you've fixed all of this you should find vpath works as expected, the complete fixed Makefile is

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 $@

这篇关于使用VPATH/vpath的正确方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆