<div dir="ltr">Using a cache variable seems not required. A function creates a new scope and inherit a copy of all variables defined in the upper scope.<div>So, by adding a set command using PARENT_SCOPE in your function you can update the variable in the parent scope:</div><div><span style="color:rgb(33,33,33)"> function(addTest targetName)</span><br style="color:rgb(33,33,33)"><span style="color:rgb(33,33,33)"> # create the executable with all the souces</span><br style="color:rgb(33,33,33)"><span style="color:rgb(33,33,33)"> add_executable(${targetName} ${ARGN})</span><br style="color:rgb(33,33,33)"><span style="color:rgb(33,33,33)"> list(APPEND allTestsList ${targetName})</span></div><div> <b>set (<span style="color:rgb(33,33,33)">allTestsList </span> ${<span style="color:rgb(33,33,33)">allTestsList</span><span style="color:rgb(33,33,33)">} PARENT_SCOPE)</span> </b></div><div><span style="color:rgb(33,33,33)"> message("inside addTestFunction. allTestsList: " ${allTestsList})</span><br style="color:rgb(33,33,33)"><span style="color:rgb(33,33,33)"> endfunction()</span> <br></div></div><br><div class="gmail_quote"><div dir="ltr">Le jeu. 27 sept. 2018 à 12:43, Romain LEGUAY <<a href="mailto:romain.leguay@gmail.com">romain.leguay@gmail.com</a>> a écrit :<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello everyone,<br>
<br>
I try to append a string (target name) on a list inside a function called in other CMakeLists.<br>
<br>
I have the following project’s tree:<br>
<br>
├── CMakeLists.txt<br>
├── test<br>
├── CMakeLists.txt<br>
└── app<br>
├── CMakeLists.txt<br>
├── appA<br>
│ ├── CMakeLists.txt<br>
│ └── main.cpp<br>
└── appB<br>
├── CMakeLists.txt<br>
└── main.cpp<br>
<br>
Inside the test/CMakeLists.txt, I defined an internal variable like this:<br>
set(allTestsList "" CACHE INTERNAL "All executable tests.'')<br>
And my function:<br>
function(addTest targetName)<br>
# create the executable with all the souces<br>
add_executable(${targetName} ${ARGN})<br>
list(APPEND allTestsList ${targetName})<br>
message("inside addTestFunction. allTestsList: " ${allTestsList})<br>
endfunction()<br>
<br>
I call this function inside the test/app/appA and test/app/appB CMakeLists.txt like this:<br>
addTest(appA main.cpp)<br>
addTest(appB main.cpp)<br>
<br>
I expected to have as final result:<br>
inside addTestFunction. allTestsList: appA;appB<br>
<br>
But I only have:<br>
inside addTestFunction. allTestsList: appB<br>
<br>
I suspect that a new variable is created at each call of the function.<br>
<br>
Is it possible to use a global variable? (I tried to use PARENT_SCOPE with no success).<br>
<br>
Thank you,<br>
<br>
Romain<br>
<br>
<br>
-- <br>
<br>
Powered by <a href="http://www.kitware.com" rel="noreferrer" target="_blank">www.kitware.com</a><br>
<br>
Please keep messages on-topic and check the CMake FAQ at: <a href="http://www.cmake.org/Wiki/CMake_FAQ" rel="noreferrer" target="_blank">http://www.cmake.org/Wiki/CMake_FAQ</a><br>
<br>
Kitware offers various services to support the CMake community. For more information on each offering, please visit:<br>
<br>
CMake Support: <a href="http://cmake.org/cmake/help/support.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/support.html</a><br>
CMake Consulting: <a href="http://cmake.org/cmake/help/consulting.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/consulting.html</a><br>
CMake Training Courses: <a href="http://cmake.org/cmake/help/training.html" rel="noreferrer" target="_blank">http://cmake.org/cmake/help/training.html</a><br>
<br>
Visit other Kitware open-source projects at <a href="http://www.kitware.com/opensource/opensource.html" rel="noreferrer" target="_blank">http://www.kitware.com/opensource/opensource.html</a><br>
<br>
Follow this link to subscribe/unsubscribe:<br>
<a href="https://cmake.org/mailman/listinfo/cmake" rel="noreferrer" target="_blank">https://cmake.org/mailman/listinfo/cmake</a><br>
</blockquote></div>