Makefile 变量中的尾随空格
Makefile:
#there is a whitespace after "/my/path/to"
FOO = "/my/path/to"
BAR = "dir"
INCLUDE_DIRS = $(FOO)/$(BAR) "/another/path"
INCLUDES = $(foreach dir,$(INCLUDE_DIRS),-I$(dir))
all:
@echo $(INCLUDES)
对于 Gnu make,我期望我的 $(INCLUDES) 为:
-I/my/path/to/dir -I/another/path
但是,如果该行
FOO = "/my/path/to"
以空格结尾(这是一个常见的“错误”),则变量 FOO 将包含空格,并且生成的 INCLUDES 将包含三个目录(两个第一被第一个分开):
-I/my/path/to -I/dir -I/another/path
我发现的唯一解决方案是使用 strip 函数:
FOO = $(strip "/my/path/to" )
但是没有更自然的语法,或者有任何方法可以避免这个陷阱吗?
The Makefile:
#there is a whitespace after "/my/path/to"
FOO = "/my/path/to"
BAR = "dir"
INCLUDE_DIRS = $(FOO)/$(BAR) "/another/path"
INCLUDES = $(foreach dir,$(INCLUDE_DIRS),-I$(dir))
all:
@echo $(INCLUDES)
With Gnu make I expect my $(INCLUDES) to be:
-I/my/path/to/dir -I/another/path
However, if the line
FOO = "/my/path/to"
ends with a whitespace (which is a common "mistake"), variable FOO will contains the whitespace, and the resulting INCLUDES will contains three directories (the two firsts beeing the first one splitted):
-I/my/path/to -I/dir -I/another/path
The only solution I found is to use the strip function:
FOO = $(strip "/my/path/to" )
But isn't there a more natural syntax, or any way to avoid this pitfall?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,请注意路径周围可能不应该有双引号。在您的示例中,我猜
$(FOO)/$(BAR)
的值将是"/my/path/to"/"dir"
而不是预期的/my/path/to/dir
。回答你的问题,一般来说,不会。连接两个值会保留空格,因此如果您想编写
$(FOO)/$(BAR)
,您可以保证$(FOO)
和>$(BAR)
是没有前导或尾随空格的单个单词。strip
函数足以删除后者(如果有)。但是,您可以将这些变量之一视为列表并编写类似
$(FOO:%=%/$(BAR))
的内容,这样就可以正常工作。但就我个人而言,我更愿意检查FOO
的值(如果不好则修复它或因错误而失败),然后照常使用它,例如:First of all, note that there probably shouldn't be double quotes around the paths. In your example I guess the value of
$(FOO)/$(BAR)
would be"/my/path/to"/"dir"
instead of expected/my/path/to/dir
.Answering to your question, generally speaking, no. Concatenating two values preserves whitespaces, so if you want to write
$(FOO)/$(BAR)
is up to you to guarantee that both$(FOO)
and$(BAR)
are single words with no leading or trailing whitespaces.strip
function is good enough to remove the latter (if any).However you can treat one of these variables as a list and write something like
$(FOO:%=%/$(BAR))
and this will work fine. But personally I would prefer to check the value ofFOO
(either fixing it or failing with an error if it is bad) and then use it as usual, for example:基于 Eldar Abusalimov 解决方案,这里有一个可以在循环中使用的函数
检查多个目录中是否有空格:
Based on Eldar Abusalimov solution, here is a function that can be used in a loop to
check multilple directories for whitespace: