制作文件 "%.o: %.c" 给出 "No rule to make target"

Make file "%.o: %.c" gives "No rule to make target"

我正在使用以下指南学习 makefile:makefiletutorial

到目前为止,一切都很好,直到这个使用“%”的例子:

objects = foo.o bar.o all.o
all: $(objects)

# These files compile via implicit rules
# Syntax - targets ...: target-pattern: prereq-patterns ...
# In the case of the first target, foo.o, the target-pattern matches foo.o and sets the "stem" to be "foo".
# It then replaces the '%' in prereq-patterns with that stem
$(objects): %.o: %.c

all.c:
    echo "int main() { return 0; }" > all.c

%.c:
    touch $@

clean:
    rm -f *.c *.o all

示例运行良好。但是,当我用 %.o: %.c 和 运行 make allmake -r all 命令替换 $(objects): %.o: %.c 部分时,我得到 make: *** No rule to make target 'all.o', needed by 'all'. Stop.

%.o: %.c 规则中的“%.o”目标不应该允许制作任何“.o”文件吗?为什么我必须对列表的文件使用静态模式匹配而不是直接对所有“.o”文件使用规则?

这是完整的非工作 makefile:

objects = foo.o bar.o all.o
all: $(objects)

# These files compile via implicit rules
# Syntax - targets ...: target-pattern: prereq-patterns ...
# In the case of the first target, foo.o, the target-pattern matches foo.o and sets the "stem" to be "foo".
# It then replaces the '%' in prereq-patterns with that stem
#$(objects): %.o: %.c
%.o: %.c

all.c:
    echo "int main() { return 0; }" > all.c

%.c:
    touch $@


clean:
    rm -f *.c *.o all

这不是规则:

%.o: %.c

这是一个取消的规则。参见 Canceling Rules。在 GNU make 中不可能使用模式规则创建没有配方的先决条件。