为什么我的 makefile 使用星号作为通配符,但不使用百分比?

why my makefile works with asterisk as wildcard, but do not with percentage?

在我的 makefile 中:

default: *.s
   echo $(basename $<)

回显基本名称,但是

default: %.s
   echo $(basename $<)

输出:

make: *** No rule to make target '%.s', needed by 'default'.  Stop.

我在 makefile 所在的目录中有 smf.s 文件。那么为什么 makefile 不使用它作为先决条件呢? (只有 shell-like * 会,但 % 不会),为什么?

你的第一条规则:

default: *.s
   echo $(basename $<)

如您所愿,因为 * 在 GNU Make 中是 wildcard character

另一方面,您的第二条规则:

default: %.s
   echo $(basename $<)

%.s 作为先决条件。由于没有名为 %.s 的文件,因此 Make 需要一个额外的规则来生成这个丢失的文件 %.s。这就是错误消息的内容:

make: *** No rule to make target '%.s', needed by 'default'.  Stop.

您可能认为 % 是通配符。实际上,它在 pattern rules 中的行为也是如此。但是,您的第二条规则不是模式规则。以下是文档的摘录:

A pattern rule looks like an ordinary rule, except that its target contains the character ‘%’ (exactly one of them).

你的第二条规则的目标——即default——不包含字符%。因此,它不能被限定为模式规则,所以先决条件 %.s 中的 % 字面意思是 % 字符。