Add Source in a subdirectory to a cmake project

Cmake

Cmake Problem Overview


I have project which has not been divided into libraries, but the source is organized in a directory tree. I do not know how to tell cmake to go down a directory, then add the source in that directory to project defined in the parent directory. I have attempted the following:

in project/source/CMakelists.txt:

set(SOURCE
    ${CMAKE_CURRENT_SOURCE_DIR}/unitTest/main.cpp
  )
add_subdirectory("${PROJECT_SOURCE_DIR}/folder1")
add_executable(UnitTestRNG ${SOURCE} ${HEADERS})

then in project/source/folder1/CMakeLists.txt:

set(SOURCE
   ${SOURCE}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
)
set(HEADERS
   ${HEADERS}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
)

using some message() statements, I have found that the the child folder will get the contents of the SOURCE variable, but it's new assignment to that variable will not persist on returning to the parent CMakeLists.txt

Looking for examples and at the cmake tutorial has led me to the conclusion that:

  • Source file structures are usually flat within a project
  • If code is divided into folders, it is usually is divided into corresponding libraries.

I wonder if there is some "best practice" from which I am deviating by attempting this structure.

Cmake Solutions


Solution 1 - Cmake

Like the second part of arrowdodger's answer says: in project/source/folder1/CMakeLists.txt:

set(SOURCE
   ${SOURCE}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.cpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.cpp
   PARENT_SCOPE
)
set(HEADERS
   ${HEADERS}
   ${CMAKE_CURRENT_SOURCE_DIR}/file1.hpp
   ${CMAKE_CURRENT_SOURCE_DIR}/file2.hpp
   PARENT_SCOPE
)

Solution 2 - Cmake

Since CMake 3.1 there is a new way to add source from subdirectories: target_sources

Say you have root_dir and root_dir/sub_dir and source files in both. With target_sources you can do this:

In root_dir/CMakeLists.txt define the target

add_library(some_target main.cpp)
add_subdirectory(sub_dir)

In root_dir/sub_dir/CMakeLists.txt add sources:

target_sources(some_target PRIVATE more_cool_stuff.cpp)

some_target will now contain both source files.

It is also possible to use other commands in root_dir/sub_dir/CMakeLists.txt using some_target, for example target_compile_definitions which is quite convenient to add compilation definitions.

I learned about target_sources here, check it out if you want more explanation and examples

Solution 3 - Cmake

Can't you just set all your sources in project/source/CMakelists.txt then?

Anyway, what you need is PARENT_SCOPE or CACHE option on set command.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question2NinerRomeoView Question on Stackoverflow
Solution 1 - CmakeChristopher BrunsView Answer on Stackoverflow
Solution 2 - CmakeooakView Answer on Stackoverflow
Solution 3 - CmakearrowdView Answer on Stackoverflow