首页 > 解决方案 > 使用多个规则制作单个文件的 Makefile

问题描述

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

.PHONY: app bin tgt

app file: app-dependency ; touch file

bin file: bin-dependency ; touch file

tgt: file ; touch tgt

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

当我写类似上面的东西时,我收到以下警告:

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

标签: makefile

解决方案


您的规范不是 100% 完整的,所以让我们发明缺少的内容:如果app-dependency存在,则使用app-ruleelse,如果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

推荐阅读