如何在Makefile中将带引号的字符串转换为普通字符串? [英] How to Convert a Quoted String to a Normal One in Makefile?

查看:132
本文介绍了如何在Makefile中将带引号的字符串转换为普通字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我是否正确地描述了问题,但是目前我正在通过以下方式解决此问题

I am not sure whether I have described the question properly, but currently I am solving this problem in the following way

QUOTEDSTR := "hello world"
NORMALSTR := $(shell echo $(QUOTEDSTR))

是否存在一种更内置的方式,使得"make"可以在不调用shell的情况下执行此操作?谢谢

Is there a more built-in way that 'make' can do this without calling shell? Thanks

推荐答案

另一个选项:

NORMALSTR := $(patsubst "%",%,$(QUOTEDSTR))

Beta的答案将删除字符串中的所有引号.上面的解决方案将只删除出现在开头和结尾的引号.例如:

Beta's answer will remove every quote in the string. The above solution will ONLY remove quotes that appear at the beginning and end. For example:

QUOTEDSTR := -DTITLE=\"Title\"

Beta的答案将导致 -DTITLE = \ Title \ 的值,而使用patsubst解决方案时,该值将不会更改.

Beta's answer will result in a value of -DTITLE=\Title\ while using the patsubst solution this value will not be changed.

这取决于您想要的东西.

It depends on what you want.

编辑

如果你想处理空格并且仍然只按照@stefanct 的评论在变量的开头/结尾匹配引号,你将不得不玩一些技巧.首先,您需要找到一个非空格字符,您知道该字符永远不会出现在字符串中.让我们选择 ^ ,但如果需要,您可以选择其他选项.

If you want to handle whitespace and still only match quotes at the beginning/end of the variable as per @stefanct's comment, you'll have to play some tricks. First you need to find a non-whitespace character which you know will never appear in your string. Let's choose ^ but you can choose something else if you want.

算法是:将所有空格转换为该字符,然后从结果单个单词"中删除引号,然后将该字符的所有实例转换回空格,如下所示:

The algorithm is: convert all spaces to this character, then remove the quotes from the resulting single "word", then convert all instances of that character back to spaces, like this:

# Get a variable S that contains a single space
E :=
S := $E $E

NORMALSTR := $(subst ^,$S,$(patsubst "%",%,$(subst $S,^,$(QUOTEDSTR))))

当然还有并发症;例如,它仅处理空格,而不处理诸如TAB之类的其他空白字符.

Of course there are still complications; this handles only spaces for example, not other whitespace characters like TAB.

这篇关于如何在Makefile中将带引号的字符串转换为普通字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆