【问题标题】:How can I manage make targets that don't have suffixes?如何管理没有后缀的 make 目标?
【发布时间】:2011-08-09 16:23:07
【问题描述】:

我有一个生成多个目标的 make 文件。比如:

target-a: target-a.src target-include.src
        @$(BUILD_TOOL) -f $< -o $@
target-b: target-b.src target-include.src
        @$(BUILD_TOOL) -f $< -o $@
target-c: target-c.src target-include.src
        @$(BUILD_TOOL) -f $< -o $@

实际的构建过程(上面缩写为$(BUILD_TOOL))是一个多行的事情,涉及编译器、脚本和各种诸如此类的东西,但可以说,构建过程作用于第一个目标依赖项($&lt;)并产生输出目标 ($@)。

这是相当笨拙的。 我下面的内容是否会被视为替换上面内容的安全方式(使用没有后缀的模式规则)?

all: target-a target-b target-c

% : %.src target-include.src
        @$(BUILD_TOOL) -f $< -o $@

make 工具是 GNU,我很乐意使用它的强大扩展。

【问题讨论】:

    标签: makefile gnu-make


    【解决方案1】:

    如果target是文字字符串,renierpost的解决方案非常好。如果不是(或者即使是),这将起作用:

    TARGETS := target-a target-b target-c
    
    all: $(TARGETS)
    
    $(TARGETS): % : %.src target-include.src
        @$(BUILD_TOOL) -f $< -o $@
    

    请注意,此规则不会构建您不希望的目标,甚至不会构建 target-include

    【讨论】:

    • 我如何修改 makefile 以使其生成 target-include.src? #(TARGETS): target-include.src 后面紧跟$(TARGETS): % : %.src target-include.src ... 可以接受吗?
    • 这将是可接受的(即合法),但它不会做任何事情。只需为target-include.src 设置一条规则,Make 将在需要时重建target-include.src
    【解决方案2】:

    这取决于你的 Makefile 的其余部分,但原则上这应该可以工作, 如果所有文件都在一个目录中。

    最好在目标上使用扩展。

    target 是文字字符串吗?在这种情况下,您可以更具体 (并稍微加快规则应用程序,但它已经很快了)通过使用

    all: target-a target-b target-c
    
    target-% : target-%.src target-include.src
        @$(BUILD_TOOL) -f $< -o $@
    

    如果您想从文件系统上的target-*.src 文件名或类似文件名中自动推断出target-a target-b target-c 的名称,GNU make 的高级语法将发挥作用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-11-13
      • 2014-11-26
      • 2017-10-13
      • 1970-01-01
      • 2016-08-14
      • 1970-01-01
      • 2020-02-07
      相关资源
      最近更新 更多