Function vs. Macro in CMake

前端 未结 5 2009
执念已碎
执念已碎 2020-12-12 14:45

The official document of CMake 2.8.12 says about macro

When it is invoked, the commands recorded in the macro are first modified by rep

5条回答
  •  無奈伤痛
    2020-12-12 15:14

    I wrote a sample code below:

    set(var "ABC")
    
    macro(Moo arg)
      message("arg = ${arg}")
      set(arg "abc")
      message("# After change the value of arg.")
      message("arg = ${arg}")
    endmacro()
    message("=== Call macro ===")
    Moo(${var})
    
    function(Foo arg)
      message("arg = ${arg}")
      set(arg "abc")
      message("# After change the value of arg.")
      message("arg = ${arg}")
    endfunction()
    message("=== Call function ===")
    Foo(${var})
    

    and the output is:

    === Call macro ===
    arg = ABC
    # After change the value of arg.
    arg = ABC
    === Call function ===
    arg = ABC
    # After change the value of arg.
    arg = abc
    

    So it seems arg is assigned the value of var when calling Foo and ${arg} is just string replaced with ${var} when calling Moo.

    So I think the above two quotes are very easy to make one confused, although the official documents also said that:

    Note that the parameters to a macro and values such as ARGN are not variables in the usual CMake sense. They are string replacements much like the C preprocessor would do with a macro. If you want true CMake variables and/or better CMake scope control you should look at the function command.

提交回复
热议问题