如何获取模式规则以匹配文件名与Makefile中的空格?

der*_*ert 6 filenames whitespace design-patterns makefile rule

在GNU make docs中,'%'被记录为匹配"any nonempty substring".但是,它似乎实际上只匹配不包含空格的非空子串.例如,假设你这样做:

mkdir /tmp/foo
cd /tmp/foo
echo 'int main() { return 0; }' > "test.c"
echo 'int main() { return 0; }' > "test space.c"
Run Code Online (Sandbox Code Playgroud)

现在,您应该能够使用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.
Run Code Online (Sandbox Code Playgroud)

编写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.
Run Code Online (Sandbox Code Playgroud)

即使您明确添加%: %.c规则,结果也是一样的.但是如果你向Makefile添加一个显式规则,就像这样,它可以工作:

test\ space: test\ space.c
    $(CC) -o "$@" "$<"     # first char is tab, of course.
Run Code Online (Sandbox Code Playgroud)

是否有一个技巧可以让空格与隐式规则一起使用?

编辑

我发送了一个错误报告:http://lists.gnu.org/archive/html/bug-make/2011-06/msg00002.html

Ern*_*ill 5

我不相信.作为字符串传递的以空格分隔的标记列表的概念在make中非常根深蒂固.这些列表被解析和重新分析.在UNIX世界中,目录和文件名中的空格被认为是不好的做法是有原因的.

  • 这不是一个糟糕的做法,而是必须的,因为像make这样的工具或写得不好的shell脚本的限制. (4认同)
  • 我发送了一个错误报告:http://lists.gnu.org/archive/html/bug-make/2011-06/msg00002.html (2认同)