Cmake variable scope, add_subdirectory

I have CMakeLists.txt in the root of my project and one in the / src folder. The / src folder contains only the variable with .cpp files ( set (SOURCEFILES main.cpp foo.cpp) ), and in the root CMakeLists.txt I do add_subdirectory(src) , and later I add_executable(MyApp ${SOURCEFILES}) .

But cmake gives me an error

add_executable with the wrong number of arguments, no sources provided

How to get cmake to see a variable? I read that cmake only knows global variables, but this is obviously not the case ...

+64
scope cmake
Jul 31 '11 at 18:29
source share
1 answer

As mentioned in the set command documentation, every directory added with add_subdirectory or every function declared with function creates a new scope.

The new child region inherits all variable definitions from its parent region. Assignment variables in the new child region using the set command will only be visible in the child region unless the PARENT_SCOPE parameter is PARENT_SCOPE .

To make the SOURCEFILES visible in the root folder of your project, try:

 set (SOURCEFILES main.cpp foo.cpp PARENT_SCOPE) 
+98
Jul 31 '11 at 18:45
source share



All Articles