cmake function, parameter and return

2019-01-19 10:52发布

I would like to create a cmake function as:

function(test src_list dst_list)
# do something
endfunction()

usage:

test(${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.

But it is not working, how can I create a function in cmake to do that?

2条回答
不美不萌又怎样
2楼-- · 2019-01-19 11:37

Check that inside your function you are set()ing not dst_list but ${dst_list}. You need this to return data to the parent scope.

查看更多
太酷不给撩
3楼-- · 2019-01-19 11:40

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)

See documentation:

A function opens a new scope: see set(var PARENT_SCOPE) for details.

查看更多
登录 后发表回答