Makefile:返回字符串的前两个字符
给定 Makefile 中的字符串,是否可以使用 Makefile 语法(不使用 shell 调用)提取未知字符串的前两个字符?
例如,
VAR := UnKnown
VAR_TERSE = $(call get_first_two_chars, $(VAR))
define get_first_two_char
...
endef
Given a string in a Makefile, is it possible to extract the first two characters of an unknown string using Makefile syntax (without using shell calls)?
for instance,
VAR := UnKnown
VAR_TERSE = $(call get_first_two_chars, $(VAR))
define get_first_two_char
...
endef
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
嗯,这是可以做到的,但是手工实现起来相当混乱。最简单的方法是获取 GNU Make 标准库,它有一个内置的
substr< /代码> 函数。如果这太过分了,您可以从库中提取该函数,但正如我所说,它非常混乱。
本质上,您对字符串进行一系列替换,以在每个字符后插入一个空格:
接下来,您可以使用
$(wordlist)
函数来获取中间结果的前两个“单词”:最后,您再次使用
$(subst)
,现在删除您最初注入的空格:Well, it can be done, but it's pretty messy to implement by hand. The easiest thing to do is to get the GNU Make Standard Library, which has a built-in
substr
function. If that's overkill, you can extract just that function from the library, but like I said, it's surprisingly messy.Essentially you do a series of substitutions on the string to insert a space after each character:
Next you can use the
$(wordlist)
function to grab the first two "words" of the intermediate result:Finally, you use
$(subst)
again, now to strip out the space that you injected originally:我真的不赞成强迫Make做它显然不想做的事情,但是……我无法抗拒一个好的谜题。
I really don't approve of forcing Make to do things it clearly doesn't want to do, but... I can't resist a good puzzle.
使用 shell 和 sed 吗?
Use the shell and sed?