【问题标题】:Makefile : contains stringMakefile : 包含字符串
【发布时间】:2011-02-14 01:31:59
【问题描述】:

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

需要比较给定的 var 包含位于任何位置的 NT-5.1

使用 cygwin 并希望与几乎任何 *nix 兼容。

【问题讨论】:

    标签: string makefile conditional-statements


    【解决方案1】:

    findstring 功能是您的心愿:

    $(findstring find,in)
    

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

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

    分别产生值"a"""(空字符串)。请参阅Testing Flags,了解findstring 的实际应用。

    类似:

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

    ifneq (,$(... 的逗号是什么?

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

    【讨论】:

    • findstring 区分大小写吗?如果是这样,是否有一种简单的方法来进行不区分大小写的匹配?手册不清楚...
    • @IsaacTurner:是的,make 函数总是区分大小写-敏感。遗憾的是,没有不区分大小写的变体,但作为一种解决方法,您可以使用 $(shell ...) 函数来使用 shell 命令进行大小写转换 - 很笨重,但它可以工作;例如:$(findstring $(shell echo 'BC' | tr '[:upper:]' '[:lower:]'), 'abcd')。如果您不介意指定 SHELL := bashmake 使用 bash 作为 shell,您可以利用 shopt -s nocasematch 并在单个 $(shell ...) 调用中不区分大小写地执行整个比较。
    • @Invictus 你应该把它作为一个新问题发布。
    【解决方案2】:
    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))"
    

    匹配给定的字符串并返回

    【讨论】:

    • @JavidJamae 这可能更具可读性,但您在重复“NT-5.1”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 1970-01-01
    • 2012-12-16
    • 1970-01-01
    相关资源
    最近更新 更多