CMake: difference between ${} and “${}”

前端 未结 1 363
春和景丽
春和景丽 2021-01-02 03:18

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

set(any_new_var ${old_var})

and

set(any_new_var \"${old_var}\")
<         


        
相关标签:
1条回答
  • 2021-01-02 03:47

    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.

    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
    

    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
    

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

    0 讨论(0)
提交回复
热议问题