简体   繁体   English

如何动态包含makefile

[英]How to include makefile dynamically

Now I have two makefiles, A.mk and B.mk. 现在,我有两个生成文件A.mk和B.mk。

A.mk will include B.mk But B.mk is dynamically generated by some command which is executed in A.mk A.mk将包括B.mk,但是B.mk是由A.mk中执行的某些命令动态生成的

Here is the pseudo code of A.mk 这是A.mk的伪代码

command to generate B.mk 
include B.mk
compile and link

The question is the include command is something like #include macro in C file. 问题是include命令类似于C文件中的#include宏。 the A.mk try to load the B.mk before the command is executed. A.mk尝试在执行命令之前加载B.mk。

Does anyone can give me some advise? 有人可以给我一些建议吗?

Many Thanks Jerry 非常感谢杰里

It is possible to make it work like this: 可以使它像这样工作:

a.mk (at this point, b.mk doesn't exist) a.mk (此时b.mk不存在)

-include b.mk

all:
    @echo foo : $(FOO)

b.mk:
    @echo "FOO=2" > b.mk

With make -f a.mk all we obtain this : 使用make -f a.mk all我们可以获得:

foo : 2 富:2

The sign - in front of the include directive allows not to generate a warning if b.mk doesn't exist. 如果b.mk不存在,则include指令前面的符号-不允许生成警告。

As stated by MadScientist in the comments, you don't need to put B.mk as prerequisite of any target. 正如MadScientist在评论中所述,您无需将B.mk作为任何目标的先决条件。 If make sees it's missing and finds a corresponding target it will automatically build it. 如果make看到缺少它并找到相应的目标,它将自动构建它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM