cmake:什么时候引用变量? [英] cmake: when to quote variables?

查看:154
本文介绍了cmake:什么时候引用变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我第一次编写CMake宏,我很难理解变量如何工作。更具体地,$ {a}似乎与$ {a}具有不同的含义。



例如:



将列表传递到cmake宏

我无法理解我应该添加引号,以及更大的基本原则是什么。

解决方案

CMake的两个原则,你必须记住:


  1. 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>



    您应该考虑一些经验法则:


    1. p>当您的变量包含文本时 - 尤其是可以包含空格或分号(并且包含路径/文件名)的文本 - 您应该添加引号。



      :空格是参数之间的分隔符,分号是CMake中列表元素的分隔符。所以把引号放在一个应该是一个文本。


    2. 如果您的变数包含清单,通常不要加引号。



      推理:如果你给一个文件列表给一个CMake命令通常期望一个字符串列表,而不是一个字符串包含一个列表。您可以看到的差异在 foreach() 命令接受 ITEMS LISTS


    3. 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() / add_custom_command c $ c> / execute_process()



      您使用 COMMAND 调用中的变量:


      1. :如上所述,它可以包含空格


      2. 如果您要将某些内容连接到要传递给可执行文件的单一参数,请使用引号。



        推理:变量可能包含参数列表,当使用引号时 - 不会正确提取(分号而不是空格)

        >
      3. 始终使用 add_custom_target()添加 VERBATIM 选项 / add_custom_command()



        推理:否则跨平台行为未定义,


        $ b

        I 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:

        1. CMake is a script language and arguments are evaluated after the variables are expanded
        2. CMake differentiates between normal strings and list variables (strings with semicolon delimiters)

        Examples

        • set(_my_text "A B C") with message("${_my_text}") would give A B C
        • set(_my_list A B C) with message("${_my_list}") would give A;B;C
        • set(_my_list "A" "B" "C") with message("${_my_list}") would give A;B;C
        • set(_my_list "A" "B" "C") with message(${_my_list}) would give ABC

        Some Rules of Thumb

        There are some rules of thumb you should consider:

        1. 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.

        2. 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 accepting ITEMS or LISTS.

        3. 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 Examples

        • set(_my_text "A B C") with COMMAND "${CMAKE_COMMAND}" -E echo "${_my_text}" would
          • call cmake.exe -E echo "A B C"
          • give A B C
        • set(_my_list A B C) with COMMAND "${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
        • set(_my_list A B C) with COMMAND "${CMAKE_COMMAND}" -E echo "${_my_list}" VERBATIM would
          • call cmake.exe -E echo "A;B;C"
          • give A;B;C
        • set(_my_list "A" "B" "C") with COMMAND "${CMAKE_COMMAND}" -E echo "${_my_list}" VERBATIM would
          • call cmake.exe -E echo "A;B;C"
          • give A;B;C
        • set(_my_list "A" "B" "C") with COMMAND "${CMAKE_COMMAND}" -E echo ${_my_list} VERBATIM would
          • call cmake.exe -E echo A B C
          • give A B C
        • set(_my_list "A + B" "=" "C") with COMMAND "${CMAKE_COMMAND}" -E echo ${_my_list} VERBATIM would
          • call cmake.exe -E echo "A + B" = C
          • give A + B = C

        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:

        1. Use quotes for the arguments that contain file paths (like the first argument containing the executable itself).

          Reasoning: See above, it could contain spaces

        2. 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)

        3. Always add the VERBATIM option with add_custom_target()/add_custom_command()

          Reasoning: Otherwise the cross-platform behavior is undefined and you could get surprises with your quoted strings.

        References

        这篇关于cmake:什么时候引用变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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