使用 OUT 参数从函数返回一个列表 [英] Return a list from the function using OUT parameter

查看:23
本文介绍了使用 OUT 参数从函数返回一个列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个 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 中,函数有自己的作用域,默认情况下,所有对变量的修改都是局部的,除非你通过 CACHEPARENT_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屋!

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