如何获取模式规则来匹配 Makefile 中的文件名和空格?
在 GNU make 文档中,'%' 被记录为匹配“任何非空子字符串”。然而,它似乎实际上只匹配不包含空格的非空子字符串。例如,假设您这样做:
mkdir /tmp/foo
cd /tmp/foo
echo 'int main() { return 0; }' > "test.c"
echo 'int main() { return 0; }' > "test space.c"
现在,您应该能够使用 GNU Make 的内置模式规则构建这些:
anthony@Zia:/tmp/foo$ make "test"
cc test.c -o test
anthony@Zia:/tmp/foo$ make "test space"
make: *** No rule to make target `test space'. Stop.
当您编写 makefile 时,也会发生同样的问题。
anthony@Zia:/tmp/foo$ rm test
anthony@Zia:/tmp/foo$ echo 'all: test test\ space' > Makefile
anthony@Zia:/tmp/foo$ make
cc test.c -o test
make: *** No rule to make target `test space', needed by `all'. Stop.
即使您显式添加 %: %.c
规则,结果也是相同的。但是,如果您在 Makefile 中添加显式规则,如下所示,它就会起作用:
test\ space: test\ space.c
$(CC) -o "$@" "$<" # first char is tab, of course.
是否有一个技巧可以让空格与隐式规则一起使用?
编辑
我已经发送了错误报告: http:// lists.gnu.org/archive/html/bug-make/2011-06/msg00002.html
In the GNU make docs, '%' is documented to match "any nonempty substring". However, it seems it actually only matches non-empty substrings that do not contain whitespace. For example, say you do this:
mkdir /tmp/foo
cd /tmp/foo
echo 'int main() { return 0; }' > "test.c"
echo 'int main() { return 0; }' > "test space.c"
Now, you should be able to build these using GNU Make's built-in pattern rules:
anthony@Zia:/tmp/foo$ make "test"
cc test.c -o test
anthony@Zia:/tmp/foo$ make "test space"
make: *** No rule to make target `test space'. Stop.
The same problem happens when you write a makefile.
anthony@Zia:/tmp/foo$ rm test
anthony@Zia:/tmp/foo$ echo 'all: test test\ space' > Makefile
anthony@Zia:/tmp/foo$ make
cc test.c -o test
make: *** No rule to make target `test space', needed by `all'. Stop.
Even if you explicitly add in a %: %.c
rule, the result is the same. But if you add in an explicit rule to the Makefile, like this, it works:
test\ space: test\ space.c
$(CC) -o "$@" "lt;" # first char is tab, of course.
Is there a trick to get spaces to work with implicit rules?
edit
I've sent a bug report: http://lists.gnu.org/archive/html/bug-make/2011-06/msg00002.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不相信是这样。将空格分隔的标记列表作为字符串传递的概念在 make 中根深蒂固。这些列表被解析和重新解析。在 UNIX 世界中,目录和文件名中的空格被认为是不好的做法是有原因的。
I don't believe so. The notion of a list of whitespace-separated tokens being passed around as a string is pretty deeply ingrained in make. Those lists are parsed and reparsed. There's a reason why spaces in directory and file names is considered bad practice in the UNIX world.
这是一个拼凑,但截至今天,人们有时仍然会得到带有空格的路径。
无论如何,建立链接而不是直接访问 % 规则中的目录是可以的。
This is a kludge, but as of today, people still get given paths with spaces in sometimes.
Anyway, making a link instead of directly accessing the directory in the % rule works OK.