Search code examples
c++c++11cmakecmake-gui

create one CmakeList.txt file for multiple subdirectories each containg its own src(.cpp files) and include(.h files)


How to write one Cmake file that compile all my sub-directories each with its own executable?

       /Project
            Cmakelist.txt
            /project 1
                /src
                  .cpp
                /include
                  .h
            /project 2
                /src
                  .cpp
                /include
                  .h

Solution

  • Include this in every CmakeList.txt subfolder

    cmake_minimum_required(VERSION 3.5.2)
    project(project1)
    set(SRC "src/name.cpp" "include/name2.h")

    add_executable(project1 ${SRC})
    target_include_directories (project1 PRIVATE include)

    And this in the CmakeList.txt root directory

    cmake_minimum_required(VERSION 3.5.2)
    project(project)
    add_subdirectory(project1)
    add_subdirectory(project2)

    ...add more if you want

    you can also delete the header files from set, it will also work