保存$^的先决顺序

发布于 2025-01-23 14:22:38 字数 554 浏览 5 评论 0原文

鉴于此makefile

.SUFFIXES: .mmd .tmp .html

a.html : h.tmp a.tmp
b.html : h.tmp b.tmp
c.html : h.tmp c.tmp C.tmp

.tmp.html:
        cat $^ > $@

.mmd.tmp:
        Markdown.pl $< > $@

,其中h.mmd是每个HTML文件开始时要插入的标题。

命令make?.html产生该

$ touch h.mmd
$ make ?.html
Markdown.pl h.mmd > h.tmp
cat a.tmp h.tmp > a.html
cat b.tmp h.tmp > b.html
cat c.tmp h.tmp C.tmp > c.html

先决条件已按字母顺序排序。我可以保留依赖项中指定的顺序,以便cat命令在指定的顺序中包含每个部分吗?

Given this makefile

.SUFFIXES: .mmd .tmp .html

a.html : h.tmp a.tmp
b.html : h.tmp b.tmp
c.html : h.tmp c.tmp C.tmp

.tmp.html:
        cat $^ > $@

.mmd.tmp:
        Markdown.pl 
lt; > $@

where h.mmd is a header to be inserted at the start of each html file.

The command make ?.html produces this

$ touch h.mmd
$ make ?.html
Markdown.pl h.mmd > h.tmp
cat a.tmp h.tmp > a.html
cat b.tmp h.tmp > b.html
cat c.tmp h.tmp C.tmp > c.html

The prerequisites have been sorted alphabetically. Can I preserve the order specified in the dependency lines so that the cat command includes each part in the specified order?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

说好的呢 2025-01-30 14:22:38

它与字母顺序或排序无关。先决条件未分类,$^的值也没有重新安排,除了一个例外:

在所有隐式规则中,隐式依赖性始终是先决条件列表中的第一个元素,无论您以后是否稍后是否将其添加为其他不同的先决条件地点。

查看是否将后缀规则转换为模式规则(但两者都相同)可能会更简单:一个后缀规则类似:

.tmp.html: ; cmd

与模式规则相同:

%.html: %.tmp ; cmd

因此,如果您有:

%.html: %.tmp ; @echo $^

c.html : d.tmp b.tmp c.tmp a.tmp

因为目标c.htmlc,匹配的先决条件c.tmp即使以不同的顺序列出了它们,即使它们以不同的顺序列出明确的先决条件列表。因此,其输出将是:

c.tmp d.tmp b.dmp a.tmp

注意:未排序,但匹配的先决条件首先出现。

没有办法“关闭此”。如@redgrittybrick所建议的那样,唯一的解决方案是根本不列出先决条件模式。您可以使用他们的想法:

%.html: ; @echo $^

c.html : d.tmp b.tmp c.tmp a.tmp

或继续使用后缀规则:

.html: ; @echo $^

c.html : d.tmp b.tmp c.tmp a.tmp

两者都会给出相同的结果:

d.tmp b.tmp c.tmp a.tmp

It doesn't have anything to do with alphabetical order or sorting. The prerequisites are not sorted and the value of $^ is not rearranged, with one exception:

In all implicit rules the implicit dependency is always the first element in the prerequisite list, regardless of whether you added it later as an explicit prerequisite in a different location.

It might be simpler to see if you convert your suffix rule into a pattern rule (but both work the same): A suffix rule like:

.tmp.html: ; cmd

is the same thing as a pattern rule like:

%.html: %.tmp ; cmd

So if you have:

%.html: %.tmp ; @echo $^

c.html : d.tmp b.tmp c.tmp a.tmp

because the pattern match for a target c.html is c, the matching prerequisite c.tmp will always be the first prerequisite even though they're listed in different order in the explicit prerequisites list. So the output of this will be:

c.tmp d.tmp b.dmp a.tmp

Note: not sorted, but the matching prerequisite appears first.

There is no way to "turn this off". The only solution, as suggested by @RedGrittyBrick, is to not list the prerequisite pattern at all. You can use their idea of:

%.html: ; @echo $^

c.html : d.tmp b.tmp c.tmp a.tmp

Or continue to use a suffix rule like:

.html: ; @echo $^

c.html : d.tmp b.tmp c.tmp a.tmp

and both will give the same result:

d.tmp b.tmp c.tmp a.tmp
别念他 2025-01-30 14:22:38

我不正确地理解这一点,但是对我有用的一种方法是进行单行更改以替换后缀规则.tmp.html:使用模式规则%。html:html:

.SUFFIXES: .mmd .tmp .html

a.html : h.tmp a.tmp
b.html : h.tmp b.tmp
c.html : h.tmp c.tmp C.tmp

%.html:
        cat $^ > $@

.mmd.tmp:
        Markdown.pl 
lt; > $@

保留订单的

$ touch h.mmd
$ make ?.html
Markdown.pl h.mmd > h.tmp
cat h.tmp a.tmp > a.html
cat h.tmp b.tmp > b.html
cat h.tmp c.tmp C.tmp > c.html

这是一个答案,尽管不完整,因为...

我理解:

  • 目标可以具有多个规则。
  • $^是目标所有规则的所有先决条件的组合集。

我不明白的是:

  • 为什么这会影响同一命令中$^元素的顺序。

I don't properly understand this but one way that worked for me was to make a one-line change to replace the suffix rule .tmp.html: with a pattern rule %.html:

.SUFFIXES: .mmd .tmp .html

a.html : h.tmp a.tmp
b.html : h.tmp b.tmp
c.html : h.tmp c.tmp C.tmp

%.html:
        cat $^ > $@

.mmd.tmp:
        Markdown.pl 
lt; > $@

which preserves order

$ touch h.mmd
$ make ?.html
Markdown.pl h.mmd > h.tmp
cat h.tmp a.tmp > a.html
cat h.tmp b.tmp > b.html
cat h.tmp c.tmp C.tmp > c.html

So that is an answer, albeit incomplete because ...

What I understand:

  • A target can have multiple rules.
  • $^ is the combined set of all prerequisites from all rules for a target.

What I don't understand:

  • Why this affects the order of the elements of $^ in the same command.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文