[CMake] add_subdirectory not working as I want, need some guidance

Petr Kmoch petr.kmoch at gmail.com
Mon Oct 1 02:34:37 EDT 2012


Hi Andrew.

Just a clarification: do you actually need the source-gathering file
(currently /project_src/code/client/core/CMakeLists.txt) to be a CMakeList?
If all it does is set up some variables, you might be best off renaming it
e.g. FileSetup.cmake and then including it using include(...) in both
CMakeLists.

Petr

On Sun, Sep 30, 2012 at 9:26 AM, Andrew Fenn <andrewfenn at gmail.com> wrote:

> Hello all,
>
> I'm a little stuck on how to get this working exactly how I want it to
> and I'm hoping someone here will be able to help me figure it out.
>
> My project is structured as so:
>
> /project_src/tests/client/CMakeLists.txt  -  Client Unit Test Project
> /project_src/code/client/CMakeLists.txt  -  Client Project
> /project_src/code/client/core/src  - *.cpp files
> /project_src/code/client/core/include  - *.h files
> /project_src/code/client/core/CMakeLists.txt - Gets the source for the
> above dirs like so..
>
> -----
> set(client_source
>     src/GameRoot.cpp
>     src/GameManager.cpp
>     src/RootGameState.cpp
>     src/GameState.cpp
> )
>
> include_directories("include")
>
> ----
>
> So how my "Client" Project works is that I can just do the following
> and it works nicely:
> add_subdirectory("core/")
>
> I want to be able to do the same in my unit test too that way I don't
> have to edit two CMakeLists.txt every time I add a file to the core
> subfolder however I'm having a lot of difficultly doing so.
>
> My first issue is that I have to change the command to the following
> in my CMakeLists.txt in my unit test:
> add_subdirectory("${CMAKE_SOURCE_DIR}/code/client/core/"
> "${CMAKE_CURRENT_BINARY_DIR}/code/client/core/")
>
> I don't actually fully understand what's going on in the second
> argument here but it seems to add the folder; however now I have a new
> problem which is that the ${client_source} variable that you can see
> above in my /project_src/code/client/core/CMakeLists.txt goes out of
> scope. This is weird because it works fine in my client project.
>
> In order to resolve that problem I found that you can add PARENT_SCOPE
> to the set function but that doesn't work either because it won't give
> me the whole file location, just the relative "src/GameRoot.cpp" for
> example which is kind of useless along with the fact that I don't know
> if "include_directories" will work either.
>
> So my question for the list after this long explanation is how can I
> set my unit test's CMakeLists.txt up so that I can just add the
> following and have it "just work":
> add_subdirectory("${CMAKE_SOURCE_DIR}/code/client/core/")
>
> Thanks,
> Andrew
> --
>
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Please keep messages on-topic and check the CMake FAQ at:
> http://www.cmake.org/Wiki/CMake_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.cmake.org/mailman/listinfo/cmake
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.cmake.org/pipermail/cmake/attachments/20121001/29b324ab/attachment.htm>


More information about the CMake mailing list