【问题标题】:Makefile with multiple rules to make a single file使用多个规则制作单个文件的 Makefile
【发布时间】:2018-12-12 20:43:29
【问题描述】:

我想用多种方式创建一个生成文件的 Makefile:

.PHONY: app bin tgt

app file: app-dependency ; touch file

bin file: bin-dependency ; touch file

tgt: file ; touch tgt

这样我就可以运行make tgt,并根据文件app-dependencybin-dependency 是否存在来决定要运行的目标。

当我编写类似上述内容时,我收到以下警告:

警告:覆盖目标“文件”的配方
警告:忽略目标“文件”的旧配方

【问题讨论】:

  • 是 app 和 bin 目录吗? “应用程序文件”看起来不像是有效的语法。有一个工作示例会有所帮助,也许使用“触摸”来创建文件。
  • 您需要重写您的问题以使用一致的命名,否则我们无法判断您在做什么。您的错误消息显示“目标”,但示例 makefile 中没有“目标”。请考虑创建一个 MCVE stackoverflow.com/help/mcve
  • 啊,没关系,我知道你在做什么。

标签: makefile


【解决方案1】:

您的规范不是 100% 完整的,所以让我们发明缺少的内容:如果 app-dependency 存在,则使用 app-rule,否则如果 bin-dependency 存在,则使用 bin-rule,否则会引发错误。如果这不是您想要的,请编辑您的问题并尝试添加缺少的规范。

使条件句是一种做你想做的事的方法:

.PHONY: app bin tgt

ifeq ($(wildcard app-dependency),app-dependency)

app file: app-dependency
    app-rule

else ifeq ($(wildcard bin-dependency),bin-dependency)

bin file: bin-dependency
    bin-rule

else

app bin file:
    $(error app-dependency and bin-dependency not found)

endif

tgt: file
    tgt-rule

【讨论】:

    【解决方案2】:

    由于 app、bin 和 tgt 是假的,因此无论如何都不会使用依赖项,除非在缺少规则时阻止规则构建。

    tgt 无法知道要构建哪个版本的文件。如果 app 和 bin 创建的文件相同,则将其分解:

    file: ; touch file
    app: file ; ...
    bin: file ; ...
    tgt: file ; ...
    

    如果 app 和 bin 构建不同版本的文件,则无需将其作为目标。无论如何,Tgt 都不知道要构建哪一个:

    app: ; touch file
    bin: ; touch file
    tgt: file ; ...
    

    如果意图是 tgt 应该在文件丢失的情况下构建 app 和 bin(它充当“或”依赖项),您可以将其排除:

    app: ; touch file
    bin: ; touch file
    file: app bin
    tgt: file ; ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      • 1970-01-01
      • 2011-02-27
      • 1970-01-01
      相关资源
      最近更新 更多