使用OUT参数从函数返回列表 [英] Return a list from the function using OUT parameter
问题描述
我想创建一个CMake函数,如下所示:
I would like to create a CMake function as:
function(test src_list dst_list)
# do something
endfunction()
用法:
test(${my_list} chg_list)
这意味着,my_list
是具有多个字段的列表,并且chg_list
将收到在测试函数内部修改的列表.
It means, my_list
is a list with several fields, and chg_list
will receive a list modified inside test function.
我该如何在CMake中创建一个函数来做到这一点?
How can I create a function in CMake to do that?
推荐答案
在CMake中,函数具有自己的作用域,并且默认情况下,变量的所有修改都是局部的,除非您将CACHE
或PARENT_SCOPE
作为参数传递给set
.在函数内部,如果要在调用方范围内修改变量,则应使用:
In CMake, functions have their own scope, and by default, all modification of variables are local, unless you pass CACHE
or PARENT_SCOPE
as parameter to set
. Inside a function, if you want to modify a variable in the scope of the caller, you should use:
set(${dst_list} <something> PARENT_SCOPE)
请参见文档:
一个函数打开了一个新的作用域:请参见 set(var PARENT_SCOPE )以获取详细信息.
A function opens a new scope: see set(var PARENT_SCOPE) for details.
这篇关于使用OUT参数从函数返回列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!