在 GNU Make 中连接列表的元素
在我的 makefile 中,我有一个带有目录列表的变量,如下所示:
DIRS = /usr /usr/share/ /lib
现在,我需要从中创建 PATH 变量,它基本上相同,但使用分号作为分隔符:
PATH = /usr:/usr/share/:/lib
我该如何做?我的意思是,如何用分号而不是空格连接 DIRS 列表的元素?
In my makefile I have a variable with a list of directories, like this:
DIRS = /usr /usr/share/ /lib
Now, I need to create PATH variable from it, which is basically the same, but uses semicolon as a separator:
PATH = /usr:/usr/share/:/lib
How do I do that? I mean, how do I join elements of DIRS list with semicolons, instead of spaces?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您使用“:”作为分隔符,因此您使用的是 Linux。考虑使用bash工具链用单个冒号替换连续空格
You use ':' as separator, so you are on Linux. Consider using bash tool chain to replace continuous spaces by single colon
您可以使用
$(subst)
命令并结合一些小技巧来获取值为单个空格的变量:You can use the
$(subst)
command, combined with a little trick to get a variable that has a value of a single space:最干净的形式(我能找到的):
注释:
eval
后的尾随空格:$(eval )
。如果没有这个,make 会将$(eval)
解释为变量,而不是调用eval
函数。如果省略尾随空格,您可以将eval
替换为some_undefined_variable
,文本替换仍然会发生。但是,如果您使用--warn-undefined-variable
标志运行 make,您将收到一条警告,指出eval
或some_undefined_variable
未定义。Cleanest Form (that I can find):
Notes:
eval
:$(eval )
. Without this, make interprets$(eval)
as a variable rather than calling theeval
function. If the trailing space is omitted, you could replaceeval
withsome_undefined_variable
and the text replacement will still happen. However, if you run make with the--warn-undefined-variable
flag, you'll get a warning thateval
orsome_undefined_variable
is undefined.