如何使用“隐式规则链"?用于Makefile [英] How to use "Chains of Implicit Rules" for Makefiles

查看:55
本文介绍了如何使用“隐式规则链"?用于Makefile的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

GNU Make支持隐式规则链".我要实现的是在Makefile的单个部分中编写代码以实现此目的:

GNU Make supports ``chains of implicit rules". What I want to achieve is to write in a single section of a Makefile to achieve this:

  1. 使用命令将A生成为B,例如:cmd1 A B
  2. 使用命令将B生成为C,例如:cmd2 B C
  3. 使用命令将C生成为D,例如:cmd3 C D

对于A,B,C和D,是四个具有不同扩展名的文件(例如,表示.p,.q,.r和.t).B和C是我不需要的中间文件,而A是源文件,D是输出.问题在于A实际上是一组非常大的文件.因此,我想使用Makefile的隐式规则链来实现这一点.但是我不知道如何知道中间文件B和C的文件名,如:

With A, B, C and D are four files with different extensions (denote .p, .q, .r and .t, for example). B and C are intermediate files which I do not need, while A is the source file and D is the output. The problem is that A is actually a very large set of files. So I want to use chain of implicit rules of Makefile to achieve this. But I don't know how to know the file names for intermediate files B and C, as in:

%.t : %.r : %.q : %.r
        cmd1 $< ?
        cmd2 ? ??
        cmd3 ?? $@

在哪里?和 ??是中间文件的名称.对于如何命名这些文件,我还没有找到非常具体的解释.GNU make手册页仅提供变量表示形式,例如$ <和$ @对我来说不是立即可用的.非常感谢.

where ? and ?? are names for intermediate files. I haven't found very specific explanation on how to name these files. The GNU make manual page only provides variable representations such as $< and $@ which are not of immediate use for me. Thanks a lot.

推荐答案

您应将规则分为不同的部分:

You should split the rules into separate parts:

%.t: %.r.tmp
    cmd1 $< $@

%.r.tmp: %.q
    cmd2 $< $@

%.q: %.r
    cmd3 $< $@

创建target.t时,将为您生成中间target.r.tmp,target.q,并在最后将其删除.

When you make target.t, the intermediate target.r.tmp, target.q would generated for you, and they are removed at the end.

编辑

如果除cmd2,cmd3之外的任何地方都没有使用中间文件* .r.tmp,*.q,我个人不希望创建隐式规则.

If the intermediate files *.r.tmp, *.q are not used anywhere except for cmd2, cmd3, personally I don't feel like to create implicit rules.

尽管隐式规则的链"是一个很好的模式,但对于IMO的Makefile,则不是必需的.同样,这只是个人感觉.

Though "Chains of implicit rules" is a good pattern, it's not necessary in case of Makefile, IMO. Again this is just a personal feel.

%.t: %.r
    cmd1 $<       $*.r.tmp
    cmd2 $*.r.tmp $*.q
    cmd3 $*.q     $@
    rm -f $*.r.tmp $*.q

这篇关于如何使用“隐式规则链"?用于Makefile的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆