Makefile隐式规则匹配-前缀长度不影响匹配 [英] Makefile implicit rule matching - prefix length not affecting match

查看:61
本文介绍了Makefile隐式规则匹配-前缀长度不影响匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的gnu-make-3.81 Makefile文件中,我希望定义两个隐式规则,以便更具体的第一个规则优先于更通用的第二个规则:

In my gnu-make-3.81 Makefile, I wish to define two implicit rules, such that the more specific first one takes precedence over the more general second one:

src/%_bar.o : src/%_bar.c
    $(CC) -Wall -Wno-unused-but-set-variable -c $^ -o $@

src/%.o : src/%.c
    $(CC) -Wall -c $^ -o $@

我遇到的问题是foo_bar.c代表自动生成的代码,该代码会触发-Wall触发警告,因此我希望禁止它,因此第一个规则就是要捕获这种特殊情况.

The issue I have is that foo_bar.c represents autogenerated code that triggers a warning with -Wall that I'd prefer to suppress, so the first rule is meant to catch this special case.

根据制作手册:

一个以上的模式规则可能会满足这些条件.在这种情况下,make将选择词干最短的规则(即最匹配的模式).

It is possible that more than one pattern rule will meet these criteria. In that case, make will choose the rule with the shortest stem (that is, the pattern that matches most specifically).

我认为对于 src/foo_bar.o 的文件名,第一个规则将生成词干 foo ,第二个规则将生成 foo_bar .前者是最短的,因此我希望它适用.但是,似乎并非如此,第二条规则由make选择并执行.运行 make -d 甚至没有显示与茎 foo 匹配的尝试-仅考虑 foo_bar .

I thought that for a filename of src/foo_bar.o, the first rule would generate the stem foo, and the second would generate foo_bar. The former is the shortest so I'd expect it to apply. However this doesn't seem to be the case, the second rule is selected by make and executed. Running make -d doesn't even show an attempt to match with the stem foo - only foo_bar is considered.

如果我进行以下更改,则通过将前缀从 src/缩短为 sr 来故意延长第二条规则的词干,则仍然选择第二条规则:

If I make the following change, by making the second rule's stem deliberately longer by shortening the prefix from src/ to sr, the second rule is still selected:

src/%_bar.o : src/%_bar.c
    $(CC) -Wall -Wno-unused-but-set-variable -c $^ -o $@

sr%.o : sr%.c
    $(CC) -Wall -c $^ -o $@

我无法与make文档保持一致.

I can't reconcile this with the make documentation.

此外,如果我完全删除 src/前缀,则会选择第二个规则 :

In addition, if I remove the src/ prefix entirely, the second rule is selected:

src/%_bar.o : src/%_bar.c
    $(CC) -Wall -Wno-unused-but-set-variable -c $^ -o $@

%.o : %.c
    $(CC) -Wall -c $^ -o $@

尽管这解决了我的问题,但实际上不适合,因为它会覆盖/交互与我需要保留的当前目录上的另一个隐式规则:

Although this solves my problem, it's actually not suitable because it overrides/interacts with another implicit rule on the current directory that I need to keep:

%.o : %.c
    # do something else

我的问题是为什么在这种情况下 make 会表现出这种方式,这与文档一致吗?如果是这样,是否有更好的方法来指定更专门的隐式规则?

My question is why does make behave this way in this case, and is this consistent with the documentation? If so, is there a better way to specify a more specialised implicit rule?

推荐答案

首先,请注意,模式匹配的最短词干"方法是GNU make 3.82中引入的.如果您使用的是GNU make 3.81,则您的make版本使用的是首次匹配"的旧方法.如果可能的话,最好总是阅读发行版随附的文档,而不是Web文档,因为Web文档适用于最新版本的GNU make.GNU make 3.81于2006年4月发布...已经相当老了.

First, be aware that the "shortest stem" method of pattern matching was introduced in GNU make 3.82. If you're using GNU make 3.81 then your version of make uses the old method which was "first match". It's always best to read the documentation that comes with your distribution, if possible, rather than the web documentation because the web documentation is for the latest version of GNU make. GNU make 3.81 was released in April 2006... that's pretty old.

但是,您提供的示例确实可以按您希望的方式工作:

However, the example you provided actually DOES work the way you wanted it to:

src/%_bar.o : src/%_bar.c ; @echo shorter: $*

src/%.o : src/%.c ; @echo longer: $*

all: src/foo_bar.o

$ make-3.81
shorter: foo

$ make-3.82
shorter: foo

我怀疑当您在此处提出问题时,您没有使用实际环境中使用的相同代码.在这种环境下,您必须在较长的模式之后的 之后使用较短的模式,如下所示:

I suspect that when you asked your question here you didn't use the same code you're using in your real environment. In that environment you must have the shorter pattern after the longer pattern, like this:

src/%.o : src/%.c ; @echo longer: $*

src/%_bar.o : src/%_bar.c ; @echo shorter: $*

all: src/foo_bar.o

$ make-3.81
longer: foo_bar

$ make-3.82
shorter: foo

在提问时,确保问题中的简化示例准确反映实际情况非常重要.

When asking questions it's very important to be sure that the simplified examples in the question accurately reflect the real situation.

这篇关于Makefile隐式规则匹配-前缀长度不影响匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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