问题描述
我有一个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:
如果运行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
开头.它在并行化中也很好用(请参阅 man make ).如果不进行并行化,则fresh:
行中的依存关系顺序就会计数.
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.
这篇关于对目标的先决条件下达命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!