Makefile:对于每个以.abc结尾的文件,请执行操作以创建目标.xyz

问题描述 投票:0回答:1

您能否编写一个makefile,使每个以.abc结尾的现有文件根据某些给定规则生成一个.xyz文件?总结一下

t1.xyz : s1.abc
    touch t1.xyz; s1.abc > t1.xyz

t2.xyz : s2.abc
    touch t2.xyz; s2.abc > t2.xyz

t3.xyz : s3.abc
    touch t3.xyz; s3.abc > t3.xyz

如果需要,可以为特定目标/源手动覆盖该模式,那就太好了。

makefile
1个回答
0
投票

让我们分阶段进行。我们从(类似)您的makefile开始:

t1.xyz : s1.abc
    do_something s1.abc > t1.xyz

t2.xyz : s2.abc
    do_something s2.abc > t2.xyz

t3.xyz : s3.abc
    do_something s3.abc > t3.xyz

然后,我们使用automatic variables删除一些冗余:

t1.xyz : s1.abc
    do_something $^ > $@

t2.xyz : s2.abc
    do_something $^ > $@

t3.xyz : s3.abc
    do_something $^ > $@

我们注意到这些配方都是相同的,因此我们将规则替换为一个pattern rule

t%.xyz : s%.abc
    do_something $^ > $@

然后我们可以添加更多功能:

SOME_TARGETS:= t1.xyz t2.xyz t3.xyz
SOME_SPECIAL_TARGETS:= t4.xyz t5.xyz
ALL_POSSIBLE_TARGETS:= $(patsubst s%.abc,t%.xyz,$(wildcard s*.abc))

all: $(ALL_POSSIBLE_TARGETS)

t%.xyz : s%.abc
    do_something $^ > $@

# this overrides the pattern rule for a specific target
t7.xyz : s7.abc
    do_something_special $^ > $@

# this overrides the pattern rule for a list of targets
$(SOME_SPECIAL_TARGETS): t%.xyz : s%.abc
    do_something_special $^ > $@

可以多做一些抛光,但这应该让您忙一会儿。

© www.soinside.com 2019 - 2024. All rights reserved.