set(source_list ${source_list} ${file_path} PARENT_SCOPE)
You need to use set
instead of list
to affect the variable in the parent scope.app
So replace your list
command with:ide
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
- 1It'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.post
You need cache for the true "global-like" variable. In your case, use:ui
SET(source_list "${source_list}" CACHE INTERNAL "source_list")
answered
Aug 5 '14 at 3:05
1,382
17
17 silver badges
21
21 bronze badges
- 1
It doesn't work for second run of cmake, because value is already in cache. You must clean before build. – Maxim Suslov Feb 25 '16 at 7:23 - 1
INTERNAL
impliedFORCE
, which means: Use theFORCE
option to overwrite existing entries. – Ding-Yi Chen Feb 25 '16 at 22:49
8
Another approach is to use global properties. Once you set it:this
set_property(GLOBAL PROPERTY source_list_property "${source_list}")
you can read it from everywhere:spa
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.code
Such global properties aren't in cache.ip