string Makefile : 包含字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2741708/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 00:43:48  来源:igfitidea点击:

Makefile : contains string

stringmakefileconditional-statements

提问by Pablo

A variable returns MINGW32_NT-5.1or CYGWIN_NT-5.1.(yea, dot at the end)

变量返回MINGW32_NT-5.1CYGWIN_NT-5.1.(是的,末尾的点)

Need to compare that given var contains NT-5.1positioned anywhere.

需要比较给定的 var 包含NT-5.1定位在任何地方。

Using cygwin and would like to be compatible with pretty much any *nix.

使用 cygwin 并希望与几乎所有 *nix 兼容。

回答by John Kugelman

The findstringfunction is what your heart desires:

FINDSTRING功能是你的心脏的欲望是什么:

$(findstring find,in)

Searches infor an occurrence of find. If it occurs, the value is find; otherwise, the value is empty. You can use this function in a conditional to test for the presence of a specific substring in a given string. Thus, the two examples,

$(findstring a,a b c)
$(findstring a,b c)

produce the values "a"and ""(the empty string), respectively. See Testing Flags, for a practical application of findstring.

$(findstring find,in)

搜索为发生的发现。如果发生,则值为find;否则,该值为空。您可以在条件中使用此函数来测试给定字符串中是否存在特定子字符串。因此,这两个例子,

$(findstring a,a b c)
$(findstring a,b c)

分别产生值"a"""(空字符串)。有关的实际应用,请参阅测试标志findstring

Something like:

就像是:

ifneq (,$(findstring NT-5.1,$(VARIABLE)))
    # Found
else
    # Not found
endif

What is the comma here for ifneq (,$(...?

这里的逗号是什么意思ifneq (,$(...

Parse it as ifneq(A,B)where Ais the empty string and Bis $(findstring...). It looks odd because you don't quote strings in Makefiles.

将其解析为ifneq(A,B)其中A是空字符串而B$(findstring...)。这看起来很奇怪,因为您没有在 Makefile 中引用字符串。

回答by vimjet

VARIABLE=NT-5.1_Can_be_any_string
ifeq ($(findstring NT-5.1,$(VARIABLE)),NT-5.1)
    # Found
    RESULT=found
else
    # Not found
    RESULT=notfound
endif

all:
    @echo "RESULT=${RESULT} , output=$(findstring NT-5.1,$(VARIABLE))"

It matches the given string and returns

它匹配给定的字符串并返回