Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add Source in a subdirectory to a cmake project

Tags:

cmake

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.

like image 354
2NinerRomeo Avatar asked Jan 19 '12 22:01

2NinerRomeo


People also ask

What does Add_subdirectory do CMake?

Add a subdirectory to the build. Adds a subdirectory to the build. The source_dir specifies the directory in which the source CMakeLists.

What is Cmake_source_dir?

The path to the top level of the source tree. This is the full path to the top level of the current CMake source tree. For an in-source build, this would be the same as CMAKE_BINARY_DIR .

Where do I put CMakeLists txt?

CMakeLists. txt file contains a set of directives and instructions describing the project's source files and targets (executable, library, or both). When you create a new project, CLion generates CMakeLists. txt file automatically and places it in the project root directory.


1 Answers

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 ) 
like image 68
Christopher Bruns Avatar answered Sep 22 '22 15:09

Christopher Bruns