CMake變量的做用域

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

So replace your list command with:ide

set(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.post

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

SET(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: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

相關文章
相關標籤/搜索