How to set the global variable in a function for cmake?

cmake

Solution

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

So replace your `list` command with:

set(source_list ${source_list} ${file_path} PARENT_SCOPE)

Problem

I'm writing a CMakeLists.txt to generate files and compile the generated files. I create a function to add some file path strings to a global list variable. My CMakeLists.txt: ``` set(source_list "nothing") function(test file_path) list(APPEND source_list ${file_path}) endfunction(test) test(abc.txt) test(def.txt) message("At last, the source_list is:\"${source_list}\"") ``` The cmake output: ``` At last, the source_list is:"nothing" ``` Someone suggested that to use macro instead of function, but I do need use local variable, so I need to use the function instead of macro. How can I correctly set the global variable source_list in the function test()? Can't cmake do it in a simple and normal way?

Original source