CMake中的功能与宏 [英] Function vs. Macro in CMake

查看:143
本文介绍了CMake中的功能与宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CMake 2.8.12的正式文档表示 macro


调用该宏时,记录在宏中的命令首先被
修改,用传递的参数
替换形式参数($ {arg1}),然后作为普通命令调用。

When it is invoked, the commands recorded in the macro are first modified by replacing formal parameters (${arg1}) with the arguments passed, and then invoked as normal commands.

函数


调用该函数时,记录在该函数中的命令首先是$通过用传递的参数
替换形式参数($ {arg1})来修改b $ b,然后将其作为普通命令调用。

When it is invoked, the commands recorded in the function are first modified by replacing formal parameters (${arg1}) with the arguments passed, and then invoked as normal commands.

很明显,两个引号几乎相同,但使我感到困惑。

Obviously, two quotes are almost same but confusing me. Does replace parameters at first when calling a function just like macro?

推荐答案

我在下面编写了一个示例代码:

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})

,输出为:

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

所以看来 arg Foo $ {arg} <时,为$ c>赋值 var / code>只是在调用 Moo 时用 $ {var} 替换的字符串。

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:


请注意宏的参数和ARGN之类的不是通常的CMake变量。它们是字符串替换,就像C预处理器会使用宏执行
一样。如果您想要真正的CMake变量和/或更好的CMake
范围控制,则应查看函数命令。

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.

这篇关于CMake中的功能与宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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