CMake: difference between $ {} and "$ {}"

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 do I need to use this or that form?

For example, I'm trying to run the following mini-test

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

The result of this mini-test (cmake -P test.cmake) is an empty string (because 'a' is undefined). If I uncomment the first message, cmake gives an error message:

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

Why in the second case, he does not throw and not an error, but an empty string?

+7
cmake
Nov 27 '12 at 10:33
source share
1 answer

In scripts, CMake can be interpreted as lists. The rule is simple: to form a list, divide the line by a semicolon. 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 name of the command and a few words between parentheses. However, these words do not correspond to the arguments that the team receives individually. Each word becomes zero or more arguments, and all arguments are combined together. If the word is not quoted, it is considered as a list and expanded to a few arguments. The keyword always becomes the only argument.

For example, suppose X bound to one;two;three , Y bound to an empty string, and Z bound to foo . The next 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 quoted the words, the team would receive three arguments:

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

To return to the original question: the message command can take a different number of arguments. It takes all its arguments, combines them into one line, and then prints that line. For some unknown reason, it does not accept null arguments.

The behavior of message 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 

In addition, when set receives multiple arguments, it builds a string of arguments separated by semicolons. Then the variable is set to this line.

+8
Nov 27 '12 at 11:43
source share



All Articles