gpt4 book ai didi

cmake - CMake 中的函数与宏

转载 作者:行者123 更新时间:2023-12-03 05:17:18 25 4
gpt4 key购买 nike

The official document of CMake 2.8.12关于

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

以及关于功能

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

显然,这两个引用几乎相同,但令人困惑。参数替换在函数和宏中的行为是否相同?

最佳答案

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

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

因此,在调用 Foo 时,arg 似乎被分配了 var 的值,并且 ${arg} 是只是在调用 Moo 时将字符串替换为 ${var}

所以我认为上面两句话很容易让人混淆,尽管 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 dowith a macro. If you want true CMake variables and/or better CMakescope control you should look at the function command.

更新(2021 年 1 月 29 日)

在语句Moo(${var})后面添加以下语句,使宏和函数之间的区别更加清晰。

message(${arg})

该语句将打印出abc

关于cmake - CMake 中的函数与宏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24297999/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com