Issue
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.
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?
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)
Answered By - Fraser Answer Checked By - Mary Flores (WPSolving Volunteer)