I have a CMakeLists.txt in my project root and one in my /src folder. The one in the /src folder only contains a variable with the .cpp files (set (SOURCEFILES main.cpp foo.cpp)) and in the root CMakeLists.txt I do add_subdirectory(src) and later I do add_executable(MyApp ${SOURCEFILES}).
But cmake gives me the error
add_executable called with incorrect number of arguments, no sources
provided
I read that CMake only knows global variables, but that’s obviously not the case… What am I missing about how variable scoping works in CMake that explains this? And knowing that, how do I get CMake to see the variable?
As mentioned in the documentation of the set command, each directory added with
add_subdirectoryor each function declared withfunctioncreates a new scope.The new child scope inherits all variable definitions from its parent scope. Variable assignments in the new child scope with the
setcommand will only be visible in the child scope unless thePARENT_SCOPEoption is used.To make the
SOURCEFILESassignment visible in the root folder of your project, try: