如何在Makefile中强制重新评估变量 [英] How to force revaluation of variables in makefile

查看:178
本文介绍了如何在Makefile中强制重新评估变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个运行二进制文件以生成cpp文件的makefile.然后,我编译cpp文件并为每个cpp文件生成.o文件.

I have a makefile which runs a binary to generate the cpp files. I then compile the cpp files and generate .o files for each cpp file.

问题是当第一次调用makefile时,文件夹为空,因为尚未调用二进制文件,并且

The problem is when the makefile is first invoked, the folder is empty because the binary is not invoked yet and

CPP_FILES=$(wildcard $(MY_DIR)/inc/output/*.cpp) 

将是一个空变量.

所以我将行更改为

CPPFILES=$$(wildcard $(MY_DIR)/inc/output/*.cpp) 
OBJFILES=$$(patsubst $(MY_DIR)/*.cpp,$(MY_DIR)/*.o,$(CPPFILES))

//运行二进制文件以生成cpp文件

//ran the binary to generate the cpp files

.SECONDEXPANSION:
libEXP.so:$(CPPFILES),$(OBJFILES)
       $(CC) $(CFLAGS) -I(INC) -L(LIBS) -shared -o $(OBJFILES) $@

二级扩展失败,表明外壳找不到通配符命令.
我在做什么错??

The secondary expansion fails saying shell cannot find wildcard command.
What am I doing wrong??

推荐答案

您遇到的问题是,始终要从最终目标BACKWARDS到源头,总要解决该问题,然后再开始构建并解决先决条件. Make从库开始,然后了解其先决条件,然后了解其先决条件,然后了解THOSE的先决条件,等等.

The problem you have is make always works from the ultimate target BACKWARDS to the sources, then starts building and resolving prerequisites back forward again. Make starts with the library, then sees what its prerequisites are, then sees what the prerequisites are for those, then the prerequisites of THOSE, etc. etc.

因此,即使您用第二次扩展延迟了$(OBJFILES)值,make也会对其进行扩展,但仍然没有创建.cpp文件的情况.

So, when make expands the $(OBJFILES) value even if you delay it with second expansion, it's still the case that the .cpp files have not been created yet.

您必须做出解释,以确保存在可以生成.cpp文件的规则.使make理解后,它将为您调用规则.但是,除非您要执行类似先构建所有.cpp文件然后递归调用make的操作,否则您将无法使用通配符来确定要构建的.cpp文件.

You have to explain to make that there is a rule that can generate the .cpp file. Once make understands that, it will invoke your rule for you. You will not be able to use wildcard to determine the .cpp files to be built, though, unless you want to do something like first build all the .cpp files then recursively invoke make.

您问为什么第二个扩展无效.我在第一段中提到了这一点,但可能还不够清楚. Make经历两个阶段的处理.首先,它读取makefile文件,其次,它运行规则并构建事物.辅助扩展步骤将解析延迟到第二步为止...但是在这种情况下,这对您没有帮助.

You asked why second expansion didn't work. I mentioned this in my first paragraph but probably wasn't clear enough. Make goes through two stages of processing. First it reads makefiles, second it runs through the rules and build things. The secondary expansion step delays resolution until that second step... but that's not going to help you in this case.

您要求make生成libEXP.so.为此,它需要扩展libEXP.so的先决条件,以便它知道目标所依赖的对象. THIS 是执行第二次扩展的时间,但是这里我们尚未构建任何.cpp文件,因为我们不知道我们是否需要它们,因此通配符扩展为空. Make不会创建这些.cpp文件,除非它知道它们需要它们,直到它已经有了它们时才知道它需要它们,因此它可以为libEXP.so的先决条件生成.o文件列表.显然这是行不通的:)

You ask make to build libEXP.so. To do that it needs to expand the prerequisites of libEXP.so so it knows what the target depends on. THIS is when the second expansion is performed, but here we haven't built any .cpp files yet, because we don't know we need them, so the wildcard expands to nothing. Make won't create those .cpp files until it knows it needs them, and it can't know it needs them until it already has them so it can generate the list of .o files for prerequisites of libEXP.so. This is obviously not going to work :)

这篇关于如何在Makefile中强制重新评估变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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