CMake:$ {}和“$ {}”之间的差异 [英] CMake: difference between ${} and "${}"

查看:4726
本文介绍了CMake:$ {}和“$ {}”之间的差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在cmake之间有什么区别:

What is the difference, in cmake, between something like:

set(any_new_var ${old_var})

set(any_new_var "${old_var}")

什么时候使用一种或另一种形式?

Any important difference? When have I to use one or the other form?

例如,我尝试下一个小测试

For example, I try with the next mini test

# test.cmake

# Variable 'a' isn't defined.
set(hola "${a}")

# message(${hola})
message("${hola}")

这个小测试的输出(cmake -P test.cmake)是一个空行(因为'a' )。如果我取消注释第一条消息,cmake会抛出一个消息错误:

The output of this mini-test (cmake -P test.cmake) is a empty line (because 'a' isn't defined). If I uncomment the first message, cmake throws an message error:

CMake Error at prueba.cmake:6 (message):
  message called with incorrect number of arguments

为什么在第二种情况下不抛出错误但是空行?

Why in the second case it doesn't throw and error but an empty line?

推荐答案

在CMake字符串可以解释为列表。规则很简单:形成列表用分号分隔字符串。例如,字符串值 one; two; three 可以被认为是三个元素的列表: one 两个三个

In CMake strings can be interpreted as lists. The rule is simple: to form the list split the string at semicolons. For example, the string value one;two;three can be thought of as a list of three elements: one, two, and three.

你写的命令名称和一些单词之间的括号。但是,这些单词不是对应于命令以一对一方式接收的参数。每个单词变成零个或多个参数,所有的参数连接在一起。
除非单词被引用,否则它被视为一个列表,并扩展为多个参数。引用的单词总是成为一个单独的参数。

To invoke a command you write the command name and some words between parentheses. However, these words do not correspond to the arguments the command receive in a one-to-one fashion. Each word become zero or more arguments, and all the arguments get concatenated together. Unless a word is quoted, it is treated as a list and is expanded to multiple arguments. A quoted word always becomes a single argument.

例如,假设 X 绑定到 one; two; three Y 绑定到空字符串, Z 绑定到 foo 。以下命令调用有三个字,但命令接收四个参数:

For example, assume that X is bound to one;two;three, Y is bound to the empty string, and Z is bound to foo. The following command invocation has three words, but the command receives four arguments:

some_command(${X} ${Y} ${Z})
# The command receives four arguments:
# 1. one
# 2. two
# 3. three
# 4. foo

如果我们引用这些单词,命令将收到三个参数:

If we would have quoted the words, the command would have received three arguments:

some_command("${X}" "${Y}" "${Z}")
# The command receives three arguments:
# 1. one;two;three
# 2. (the empty list)
# 3. foo

要返回到原来的问题: message 命令可以接收不同数量的参数。它接受其所有参数,将它们连接在一起成一个字符串,然后打印该字符串。因为一些未知的原因,它不接受零参数,虽然。

To return to your original question: the message command can receive a varying number of arguments. It takes all its arguments, concatenates them together into one string, and then prints that string. For some unknown reason it does not accept zero arguments, though.

行为消息有多个参数不是非常有用,所以你倾向于使用单引号参数:

The behavior message has with multiple arguments is not very useful, so you tend to use a single quoted argument with it:

set(SOURCES foo.c hoo.h)
message(${SOURCES})   # prints foo.cfoo.h
message("${SOURCES}") # prints foo.c;foo.h

此外,当 set 接收到多个参数时,它将生成一个由分号隔开的参数字符串。然后将变量设置为该字符串。

Also, when set receives multiple arguments it builds a string of the arguments separated by semicolons. The variable is then set to that string.

这篇关于CMake:$ {}和“$ {}”之间的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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