依赖项中的Makefile字符串替换不起作用 [英] Makefile string substitution in dependency not working

查看:86
本文介绍了依赖项中的Makefile字符串替换不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个文件夹结构,其中我的所有源文件都位于./src/中,而我的所有目标文件都位于./obj/中(具有相同的内部目录结构,使用路径替换进行镜像).我创建了以下makefile:

I have a folder structure where all my source files are in ./src/, and all my object files are in ./obj/ (with the same internal directory structure, mirrored using path substitutions). I've created the following makefile:

$(EXECUTABLE): $(OBJECTS)
    @echo Linking $(EXECUTABLE)...
    $(CXX) $(LDLIBS) $(OBJECTS) -o $(EXECUTABLE)

%.o: $(subst o,cpp,$(subst obj/,src/,$@))
    @echo Building $@...
    $(CXX) $(CPPFLAGS) -c $(subst o,cpp,$(subst obj/,src/,$@)) -o $@

这不起作用! Make一直声称目标文件是最新的,即使源文件实际上早于目标文件.另一方面,如果我这样做:

Which doesn't work! Make keeps claiming that the object files are up to date, even when the source file is actually older than the object file. On the other hand, if I do this:

obj/main.o: src/main.cpp
    @echo Building $@...
    $(CXX) $(CPPFLAGS) -c src/main.cpp -o $@

对于每个源文件,它都能完美运行.我检查了一下,两个subst给出了相同的结果(obj/main.o变成了预期的src/main.cpp).但是Make出于某种原因不接受依赖.

For every source file, it works perfectly. I checked, and the two subst give the same result (obj/main.o becomes src/main.cpp as expected). Yet Make doesn't accept the dependency for some reason.

这给了我很多悲伤,有人可以解释我要去哪里了吗?我不知道发生了什么,我认为我的替换将发挥相同的作用,因为它提供了相同的输出.我是否不允许在依赖项中使用subst$@?

This is giving me a lot of grief, can somebody explain where I am going wrong? I don't understand what is going on, I thought my substitution would work the same since it gives the same output. Am I not allowed to use subst, or $@ in the dependencies or something?

推荐答案

在先决条件中,不能仅在命令中使用$@.

You can't use $@ in the prerequisites, only in the commands.

但是您可以这样做:

$(OBJECTS): obj/%.o : src/%.cpp
    @echo Building $@ from $<...
    $(CXX) $(CPPFLAGS) -c $< -o $@

这篇关于依赖项中的Makefile字符串替换不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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