cmake:什么时候引用变量? [英] cmake: when to quote variables?
问题描述
我第一次编写CMake宏,我很难理解变量如何工作。更具体地,$ {a}似乎与$ {a}具有不同的含义。
例如:
我无法理解我应该添加引号,以及更大的基本原则是什么。
CMake的两个原则,你必须记住:
- CMake是一个脚本语言,
$ c>与
消息($ {_ my_text})
会给予 ABC
设置(_my_list ABC)
与消息($ {_ my_list})
会给予 A; B; C
设置(_my_listABC)
与消息($ {_ my_list})
会给予 A; B; C
设置(_my_listABC)
与消息($ {_ my_list})
会给予 ABC
strong>
您应该考虑一些经验法则:
- p>当您的变量包含文本时 - 尤其是可以包含空格或分号(并且包含路径/文件名)的文本 - 您应该添加引号。
:空格是参数之间的分隔符,分号是CMake中列表元素的分隔符。所以把引号放在一个应该是一个文本。
-
如果您的变数包含清单,通常不要加引号。
推理:如果你给一个文件列表给一个CMake命令通常期望一个字符串列表,而不是一个字符串包含一个列表。您可以看到的差异在
foreach()
命令接受ITEMS
或LISTS
。 -
if()
语句是一种特殊情况,通常你甚至不用大括号。
推理:字符串可以在展开后再次计算变量名称。为了防止出现这种情况,建议您只是命名要比较的内容的变量(例如
if(_my_text STREQUALABC)
)。
COMMAND
示例
-
set(_my_textABC)
COMMAND$ {CMAKE_COMMAND}-E echo$ {_ my_text}将
- 调用
.exe -E echoABC
- 给予
ABC
- 调用
-
设置(_my_list ABC)
与COMMAND$ {CMAKE_COMMAND}-E echo cake> / code>
- 给予
A
,B:未找到命令
,C:command not found
(_my_list ABC)
与 COMMAND$ {CMAKE_COMMAND}-E echo$ {_ my_list}VERBATIM
将- 调用
cmake.exe -E echoA; B; C
-
A; B; C
ABC)
与 COMMAND$ {CMAKE_COMMAND}-E echo$ {_ my_list}VERBATIM
will - 调用
cmake.exe -E echoA; B; C
- 给予
A; B; C
set(_my_listABC)
与 COMMAND$ {CMAKE_COMMAND}-E echo $ {_ my_list}
VERBATIM
将- 调用
cmake.exe -E echo ABC
- 给予
ABC
set(_my_listA + B=C)
与 COMMAND$ {CMAKE_COMMAND}-E echo $ {_ my_list} VERBATIM
将- 调用
cmake.exe -E echoA + B= C
li>
- 给予
A + B = C
-
-
如果您要将某些内容连接到要传递给可执行文件的单一参数,请使用引号。
推理:变量可能包含参数列表,当使用引号时 - 不会正确提取(分号而不是空格)
>
-
始终使用
add_custom_target()添加
/VERBATIM
选项add_custom_command()
推理:否则跨平台行为未定义,
$ bI am writing CMake macros for the first time, and I have a hard time understanding how variable works. Most specifically, ${a} seems to have a different meaning than "${a}".
For example here:
Passing a list to a cmake macro
I fail to understand when I am supposed to add quotes, and what are the bigger underlying principle.
解决方案Two principles of CMake you have to keep in mind:
- CMake is a script language and arguments are evaluated after the variables are expanded
- CMake differentiates between normal strings and list variables (strings with semicolon delimiters)
Examples
set(_my_text "A B C")
withmessage("${_my_text}")
would giveA B C
set(_my_list A B C)
withmessage("${_my_list}")
would giveA;B;C
set(_my_list "A" "B" "C")
withmessage("${_my_list}")
would giveA;B;C
set(_my_list "A" "B" "C")
withmessage(${_my_list})
would giveABC
Some Rules of Thumb
There are some rules of thumb you should consider:
When your variable contains text - especially one that could contain spaces or semicolons (and that does include paths/filenames) - you should add quotes.
Reasoning: A space is a delimiter between arguments and a semicolon is a delimiter for list elements in CMake. So put quotes around a text that is supposed to be one.
If your variable contains a list you normally don't add quotes.
Reasoning: If you give something like a file list to an CMake command it normally expect a list of strings and not one string containing a list. The difference you can see e.g. in the
foreach()
command acceptingITEMS
orLISTS
.if()
statements are a special case where you normally don't even put the braces.Reasoning: A string could - after expansion - evaluate again to a variable name. To prevent this it's recommended to just name the variable whose content you want to compare (e.g.
if (_my_text STREQUAL "A B C")
).
COMMAND
Examplesset(_my_text "A B C")
withCOMMAND "${CMAKE_COMMAND}" -E echo "${_my_text}"
would- call
cmake.exe -E echo "A B C"
- give
A B C
- call
set(_my_list A B C)
withCOMMAND "${CMAKE_COMMAND}" -E echo "${_my_list}"
would- call
cmake.exe -E echo A;B;C
- give
A
,B: command not found
,C: command not found
- call
set(_my_list A B C)
withCOMMAND "${CMAKE_COMMAND}" -E echo "${_my_list}" VERBATIM
would- call
cmake.exe -E echo "A;B;C"
- give
A;B;C
- call
set(_my_list "A" "B" "C")
withCOMMAND "${CMAKE_COMMAND}" -E echo "${_my_list}" VERBATIM
would- call
cmake.exe -E echo "A;B;C"
- give
A;B;C
- call
set(_my_list "A" "B" "C")
withCOMMAND "${CMAKE_COMMAND}" -E echo ${_my_list} VERBATIM
would- call
cmake.exe -E echo A B C
- give
A B C
- call
set(_my_list "A + B" "=" "C")
withCOMMAND "${CMAKE_COMMAND}" -E echo ${_my_list} VERBATIM
would- call
cmake.exe -E echo "A + B" = C
- give
A + B = C
- call
Some Rules of Thumb with
add_custom_target()
/add_custom_command()
/execute_process()
There are some rules of thumb you should consider when you use variables in
COMMAND
calls:Use quotes for the arguments that contain file paths (like the first argument containing the executable itself).
Reasoning: See above, it could contain spaces
Use quotes only if you want to concatenate something into a single parameter to be passed to executable that is called.
Reasoning: A variable could contain a list of parameters which - when using quotes - won't be correctly extracted (semicolons instead of spaces)
Always add the
VERBATIM
option withadd_custom_target()
/add_custom_command()
Reasoning: Otherwise the cross-platform behavior is undefined and you could get surprises with your quoted strings.
References
- CMake: difference between ${} and "${}"
- What's the CMake syntax to set and use variables?
- Looping over a string list
- CMake compare to empty string with STREQUAL failed
这篇关于cmake:什么时候引用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
add_custom_target()
/ add_custom_command c $ c> /
execute_process()
您使用 COMMAND
调用中的变量: