问题描述
我有一个包含多个文件的目录,其中一些文件的名称中有空格:
I have a directory containing several files, some of which have spaces in their names:
Test workspace/
Another directory/
file1.ext
file2.ext
demo 2012-03-23.odp
我在这个目录上使用 GNU 的 $(wildcard)
命令,然后使用 $(foreach)
迭代结果,打印出所有内容.代码如下:
I use GNU's $(wildcard)
command on this directory, and then iterate over the result using $(foreach)
, printing everything out. Here's the code:
FOO := $(wildcard *)
$(info FOO = $(FOO))
$(foreach PLACE,$(FOO),$(info PLACE = $(PLACE)))
这是我希望打印出来的内容:
Here's what I would expect to see printed out:
Test workspace
Another directory
file1.ext
file2.ext
demo 2012-03-23.odp
这是我实际得到的:
Test
workspace
Another
directory
file1.ext
file2.ext
demo
2012-03-23.odp
后者显然对我没有用.文档 $(wildcard)
flat-out 指出它返回一个以空格分隔的名称列表",但完全没有承认这引起的巨大问题.文档 也没有用于 $(foreach)
.
The latter is obviously of no use to me. The documentation for $(wildcard)
flat-out states that it returns a "space-separated list of names" but completely fails to acknowledge the huge problems this raises. Nor does the documentation for $(foreach)
.
是否可以解决这个问题?如果是这样,如何?重命名每个文件和目录以删除空格不是一种选择.
Is it possible to work around this? If so, how? Renaming every file and directory to remove the spaces is not an option.
推荐答案
问题 #712 表明 make 不处理带有空格的名称.无处,永远.
The bug #712 suggests that make does not handle names with spaces. Nowhere, never.
我发现了一个 博客文章说它是部分实现的 通过使用 转义空格(
\
似乎是拼写错误或格式错误),但是:
I found a blog post saying it's partially implemented by escaping the spaces with (
\
seems to be typo or formatting artefact), but:
- 它不适用于除
$(wildcard)
之外的任何函数. - 从变量扩展名称列表时不起作用,其中包括特殊变量
$?
、$^
和$+
以及任何用户定义的变量.这反过来意味着虽然$(wildcard)
将匹配正确的文件,但您无论如何都无法解释结果.
- It does not work in any functions except
$(wildcard)
. - It does not work when expanding lists of names from variables, which includes the special variables
$?
,$^
and$+
as well as any user-defined variable. Which in turn means that while$(wildcard)
will match correct files, you won't be able to interpret the result anyway.
因此,使用明确的或非常简单的模式规则,您可以使其工作,但除此之外,您就不走运了.您必须寻找其他支持空间的构建系统.我不确定 jam/bjam 可以,scons,waf, ant、nant 和 msbuild 一切正常.
So with explicit or very simple pattern rules you can get it to work, but beyond that you are out of luck. You'll have to look for some other build system that does support spaces. I am not sure whether jam/bjam does, scons, waf, ant, nant and msbuild all should work.
这篇关于GNU 可以处理带空格的文件名吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!