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

Andrew Fenn andrewfenn at gmail.com
Sun Sep 30 03:26:47 EDT 2012


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


More information about the CMake mailing list