CMake变量的作用域

You need to use set instead of list to affect the variable in the parent scope.

So replace your list command with:

(source_list ${source_list} ${file_path} PARENT_SCOPE)
share improve this answer
30k 4 4 gold badges 36 36 silver badges 68 68 bronze badges
answered   Apr 5 '12 at 16:25
53.9k 10 10 gold badges 176 176 silver badges 184 184 bronze badges
  • 1

    It's not global though, siblings will not see.  –  0xbaadf00d   Apr 4 '17 at 6:32


  • @JoachimW: Why incorporate two answers into the single one? You seems to misunderstand Question/Answer model on Stack Overflow. We do NOT tend to have all solutions in the single accepted answer. Instead, having one answer per solution is perfect. And an answer's quality is primarily measured by the voting, good answers needn't to be marked with the green accept mark. Please, revert this answers merging.  –  Tsyvarev   Oct 1 '18 at 10:37  
16

PARENT_SCOPE is only for parent, it won't work if you have other non-parent script that want to see it as well.

You need cache for the true "global-like" variable. In your case, use:

(source_list  "${source_list}" CACHE INTERNAL "source_list")
share improve this answer
answered   Aug 5 '14 at 3:05
1,382 17 17 silver badges 21 21 bronze badges
8

Another approach is to use global properties. Once you set it:

_property(GLOBAL PROPERTY source_list_property "${source_list}")

you can read it from everywhere:

get_property(source_list GLOBAL PROPERTY source_list_property)

I used in examples above the different names for property (source_list_property) and for variable (source_list). Maybe it is better to use the same name. But point is to use a property as global variables, and not about naming.

Such global properties aren't in cache.


转载于:https://www.cnblogs.com/lizhensheng/p/11117247.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值