makefile 中 Match-Anything 模式规则的行为
Behavior of Match-Anything Pattern Rules in makefile
假设我的来源是 test.c
:
#include <stdio.h>
int main()
{
printf("%s\n", "hi");
}
我希望在名为 bin
的文件夹中创建名为 test
的可执行文件。所以我的 makefile
是:
all:bin/test
当我执行 make
时,我预计会发生以下内置规则:
%: %.c
# recipe to execute (built-in):
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
因为目标 %
应该匹配 bin/test
并且因为 test.c
存在,它应该执行配方。但是,make
表示:No rule to make target 'bin/test', needed by 'all'. Stop.
为什么会这样?
如果%
匹配bin/test
,则前提是bin/test.c
,不存在。因此 Make 在搜索要构建 bin/test
的规则时拒绝该隐式规则。它找不到其他符合要求的规则,并告诉您。
如果您尝试构建 test
,或将 test.c
移动到 bin/
,则 Make 将使用此规则。
如果您想从工作目录中的源代码在 bin/
中构建二进制文件,您可以编写自己的模式规则,类似于:
bin/%: %.c
...
假设我的来源是 test.c
:
#include <stdio.h>
int main()
{
printf("%s\n", "hi");
}
我希望在名为 bin
的文件夹中创建名为 test
的可执行文件。所以我的 makefile
是:
all:bin/test
当我执行 make
时,我预计会发生以下内置规则:
%: %.c
# recipe to execute (built-in):
$(LINK.c) $^ $(LOADLIBES) $(LDLIBS) -o $@
因为目标 %
应该匹配 bin/test
并且因为 test.c
存在,它应该执行配方。但是,make
表示:No rule to make target 'bin/test', needed by 'all'. Stop.
为什么会这样?
如果%
匹配bin/test
,则前提是bin/test.c
,不存在。因此 Make 在搜索要构建 bin/test
的规则时拒绝该隐式规则。它找不到其他符合要求的规则,并告诉您。
如果您尝试构建 test
,或将 test.c
移动到 bin/
,则 Make 将使用此规则。
如果您想从工作目录中的源代码在 bin/
中构建二进制文件,您可以编写自己的模式规则,类似于:
bin/%: %.c
...