What is the difference, in cmake, between something like:
set(any_new_var ${old_var})
and
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}")
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?
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;threecan be thought of as a list of three elements:one,two, andthree.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.
For example, assume that
Xis bound toone;two;three,Yis bound to the empty string, andZis bound tofoo. The following command invocation has three words, but the command receives four arguments:If we would have quoted the words, the command would have received three arguments:
To return to your original question: the
messagecommand 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
messagehas with multiple arguments is not very useful, so you tend to use a single quoted argument with it:Also, when
setreceives multiple arguments it builds a string of the arguments separated by semicolons. The variable is then set to that string.