对目标的先决条件下达命令 [英] Impose an order for the prerequisites of a target

查看:86
本文介绍了对目标的先决条件下达命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个makefile片段:

I have a makefile snippet:

all: $(objects)
fresh: clean all
clean: ;rm $(objects)

在这里,我要确保在执行make fresh时-clean应该在all之前.

Here, I want to ensure that when I do make fresh - clean should precede all.

但是考虑到我做make all时不应该制作clean的方式,我该如何确定呢?

But how can I make sure this, given that when I do make all, clean should not be made?

我可以想象一种方法可能是这样

I can imagine that one way could be like this

fresh: clean
    make all

是解决此问题的正确方法(或唯一方法)吗?

Is it the right (or the only) way to solve this issue?

推荐答案

如果使用GNU make:

If you use GNU make:

all:
    @echo $@
    @sleep 1
    @echo end $@

clean:
    @echo $@
    @sleep 1
    @echo end $@

fresh:: clean
fresh:: all

.PHONY: clean fresh all

请注意目标 s fresh之后的双冒号!请参见文档:

Please note the double colon after targets fresh! See the documentation:

目标的双冒号规则按其顺序执行 出现在makefile中.

The double-colon rules for a target are executed in the order they appear in the makefile.

如果运行make -j2 fresh,则表明它按预期工作:

If you run make -j2 fresh it shows it works as expect:

clean
end clean
all
end all

但是使用fresh:: clean all不能并行正常工作(可能是意外的).

But with fresh:: clean all doesn't work properly parallel (maybe unexpected).

使用BSD制作:

 all:
    @echo $@
    @sleep 1
    @echo end $@

clean:
    @echo $@
    @sleep 1
    @echo end $@

fresh:  clean all
    @echo $@

.ORDER: clean all
.PHONY: clean all fresh

请注意,该行以.ORDER开头.它在并行化中也很好用(请参阅

Note the line begin with .ORDER. It works well in parallelization too (see man make). Without parallelization the order of dependencies in line fresh: counts.

这篇关于对目标的先决条件下达命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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