转义"[]"CMake中以分号分隔的列表中的字符 [英] Escape "[]" characters in a semicolon-separated list in CMake

查看:125
本文介绍了转义"[]"CMake中以分号分隔的列表中的字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现在 CMake 中用分号分隔的列表中,"[]"和]"可能具有特殊含义.当我在 CMakeLists.txt 中尝试此代码时:

I found "[" and "]" might have special meanings in a semicolon-separated list in CMake. When I try this code in CMakeLists.txt:

set(XX "a" "b" "[" "]")
message("${XX}")

foreach(x ${XX})
        message("--> ${x}")
endforeach()

我希望得到结果:

a;b;[;]
--> a
--> b
--> [
--> ]

但是我得到了:

a;b;[;]
--> a
--> b
--> [;]

我没有找到有关"["和]"用法的任何文档.是否可以转义这些字符,以便获得预期的结果?我正在使用 CMake 2.8.12.2 .感谢您的帮助:)

I didn't find any documentation for the usage of "[" and "]". Is it possible to escape these characters so that I can get the expected result? I am using CMake 2.8.12.2. Thanks for any help :)

推荐答案

根据

According to the documentation opening square bracket definitely has a special meaning:

注意::3.0之前的CMake版本不支持括号参数.他们将左括号解释为无引号的参数"的开头.

Note: CMake versions prior to 3.0 do not support bracket arguments. They interpret the opening bracket as the start of an Unquoted Argument.

因此问题在于混合了带引号和不带引号的参数.在您的情况下,可能的解决方法是在初始化时将方括号替换为其他内容,然后再将其替换回去,如下所示:

So the problem is mixing Quoted and Unquoted arguments. Possible workaround in your case is to replace opening square bracket to something else in initialization and later replace it back, like this:

CMAKE_MINIMUM_REQUIRED (VERSION 2.8.11)
PROJECT (HELLO NONE)

SET(XX a b BRACKET ])

MESSAGE("${XX}")

FOREACH(x ${XX})
    STRING(REGEX REPLACE "BRACKET" "[" x ${x})
    MESSAGE("--> ${x}")
ENDFOREACH()

这篇关于转义"[]"CMake中以分号分隔的列表中的字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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