简体   繁体   English

Makefile目录规则依赖性重建

[英]Makefile directory rule dependency rebuild

I got Makefile like this 我有这样的Makefile

all: sub-dir my-rule

sub-dir:
    $(MAKE) -C $@

my-rule: sub-dir
    g++ main.cpp

.PHONY sub-dir

So basically I want to wait for sub-dir to finish before building my-rule but my-rule is rebuilt everytime - even if there where no changes in sub-dir. 因此,基本上我想在构建my-rule之前等待sub-dir完成,但是my-rule每次都会重新构建-即使sub-dir中没有更改。

How can I make it to wait for sub-dir and rebuild my-rule only when there were changes in sub-dir? 仅当子目录发生更改时,如何才能等待子目录并重建my-rule?

When you write a rule like: 当您编写如下规则时:

my-rule: sub-dir

the target ( my-rule ) will be rebuilt if the prerequisite ( sub-dir ) is newer. 如果先决条件( sub-dir )是新的,则将重建目标( my-rule )。 Make doesn't care whether the target or prerequisite are files are directories, only what their last modified time is. Make不在乎目标是文件还是目录,而是文件的最后修改时间。

Your makefile has many issues. 您的makefile有很多问题。 The simplest one is that you never create a target my-rule , so as far as make is concerned that target is always out of date (non-existent == out of date). 最简单的一个是,您永远不会创建目标my-rule ,因此make认为目标总是过期(不存在==过期)。

You have to write your rule like this so that the recipe updates the target: 您必须这样编写规则,以便配方更新目标:

my-rule: sub-dir
        g++ main.cpp -o $@

Of course change my-rule if that's not the program you want to create. 当然,如果那不是您要创建的程序,请更改my-rule

Second, directory modification times are updated when the directory changes. 其次,目录更改时间在目录更改时更新。 The directory changes when something in that directory is renamed, added, or removed. 重命名,添加或删除该目录中的内容时,该目录会更改。 So if you invoke the sub-make and the sub-make renames, adds, or removes something in that directory then the my-rule target will be out of date. 因此,如果您调用子品牌,并且该子品牌在该目录中重命名,添加或删除了某些内容,则my-rule目标将过期。 If nothing is renamed, added, or removed, then my-rule will NOT be out of date. 如果不改名,添加或删除,然后my-rule不会过时的。

In general you almost never want to list a directory as a prerequisite. 通常,您几乎永远不会希望将目录列出为先决条件。 Instead, you should list the targets that the sub-make creates as the prerequisite, like this (supposing the sub-make creates libfoo. ): 相反,您应该列出子make创建的目标作为先决条件,如下所示(假设子make创建libfoo. ):

sub-dir/libfoo.a: FORCE
        $(MAKE) -C $(@D)
FORCE:

my-rule: sub-dir/libfoo.a
        ...

The FORCE rule is there to force the sub-make to be invoked even if sub-dir/libfoo.a already exists. 即使sub-dir/libfoo.a已经存在,也可以使用FORCE规则强制调用子make。

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

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