TExcelTAD
Home › Modern CMake (with CTest & CPack) › Subdirectories & Project Layout

Subdirectories & Project Layout

A real project has more than one CMakeLists.txt. Each directory that defines targets gets its own, and the top-level file stitches them together with add_subdirectory().

A typical layout

text
shapes/
├── CMakeLists.txt            project(), global settings, add_subdirectory() calls
├── cmake/                    helper modules (warnings, install helpers)
├── libs/
│   └── geometry/
│       ├── CMakeLists.txt    add_library(geometry ...)
│       ├── include/geometry/area.hpp
│       └── src/area.cpp
├── apps/
│   └── shapes-cli/
│       ├── CMakeLists.txt    add_executable(shapes-cli ...)
│       └── main.cpp
└── tests/
    ├── CMakeLists.txt        add_test(...)
    └── test_area.cpp

Each library owns its headers and sources; nothing outside libs/geometry/ mentions libs/geometry/include. Consumers just link Shapes::geometry and the include path follows.

Building it

bash · try it yourself
mkdir -p shapes/libs/geometry/include/geometry shapes/libs/geometry/src shapes/apps/shapes-cli && cd shapes
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Shapes VERSION 0.1.0 LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
add_subdirectory(libs/geometry)
add_subdirectory(apps/shapes-cli)
CM
cat > libs/geometry/CMakeLists.txt <<'CM'
add_library(geometry src/area.cpp)
add_library(Shapes::geometry ALIAS geometry)
target_include_directories(geometry PUBLIC include)
message("geometry: source dir ${CMAKE_CURRENT_SOURCE_DIR}")
message("geometry: binary dir ${CMAKE_CURRENT_BINARY_DIR}")
CM
cat > libs/geometry/include/geometry/area.hpp <<'SRC'
#pragma once
namespace geometry { double square_area(double side); }
SRC
cat > libs/geometry/src/area.cpp <<'SRC'
#include <geometry/area.hpp>
double geometry::square_area(double side) { return side * side; }
SRC
cat > apps/shapes-cli/CMakeLists.txt <<'CM'
add_executable(shapes-cli main.cpp)
target_link_libraries(shapes-cli PRIVATE Shapes::geometry)
CM
cat > apps/shapes-cli/main.cpp <<'SRC'
#include <cstdio>
#include <geometry/area.hpp>
int main() { std::printf("square_area(3) = %.1f\n", geometry::square_area(3)); }
SRC
cmake -S . -B build -G Ninja --log-level=NOTICE | grep -v '^--'
cmake --build build
./build/apps/shapes-cli/shapes-cli
geometry: source dir /home/learner/shapes/libs/geometry
geometry: binary dir /home/learner/shapes/build/libs/geometry
[1/4] Building CXX object libs/geometry/CMakeFiles/geometry.dir/src/area.cpp.o
[2/4] Linking CXX static library libs/geometry/libgeometry.a
[3/4] Building CXX object apps/shapes-cli/CMakeFiles/shapes-cli.dir/main.cpp.o
[4/4] Linking CXX executable apps/shapes-cli/shapes-cli
square_area(3) = 9.0

The build tree mirrors the source tree: the executable is in build/apps/shapes-cli/, the library in build/libs/geometry/.

Path variables

Variable Meaning
CMAKE_SOURCE_DIR / CMAKE_BINARY_DIR the top of the whole build (the outermost project)
PROJECT_SOURCE_DIR / PROJECT_BINARY_DIR the directory of the closest project() call
CMAKE_CURRENT_SOURCE_DIR / CMAKE_CURRENT_BINARY_DIR the directory of the CMakeLists.txt being processed
CMAKE_CURRENT_LIST_DIR the directory of the file being processed, which differs inside an include()d .cmake file

Use the CURRENT and PROJECT variants. CMAKE_SOURCE_DIR points at the wrong place as soon as your project is pulled into a bigger one with add_subdirectory or FetchContent: it is the other project's top level.

bash · try it yourself
mkdir -p outer/vendor/inner && cd outer
cat > CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Outer LANGUAGES NONE)
add_subdirectory(vendor/inner)
CM
cat > vendor/inner/CMakeLists.txt <<'CM'
cmake_minimum_required(VERSION 3.25)
project(Inner LANGUAGES NONE)
message("CMAKE_SOURCE_DIR         = ${CMAKE_SOURCE_DIR}")
message("PROJECT_SOURCE_DIR       = ${PROJECT_SOURCE_DIR}")
message("CMAKE_CURRENT_SOURCE_DIR = ${CMAKE_CURRENT_SOURCE_DIR}")
message("PROJECT_IS_TOP_LEVEL     = ${PROJECT_IS_TOP_LEVEL}")
CM
cmake -S . -B build --log-level=NOTICE | grep -v '^--'
CMAKE_SOURCE_DIR         = /home/learner/outer
PROJECT_SOURCE_DIR       = /home/learner/outer/vendor/inner
CMAKE_CURRENT_SOURCE_DIR = /home/learner/outer/vendor/inner
PROJECT_IS_TOP_LEVEL     = OFF

Inside Inner, CMAKE_SOURCE_DIR is Outer's directory. A library that wrote ${CMAKE_SOURCE_DIR}/include would look in the wrong place.

add_subdirectory with a directory outside the tree

add_subdirectory(../shared shared-build) works too, but a source directory outside the current tree needs an explicit binary directory as the second argument. For third-party code, prefer FetchContent or find_package.

How many CMakeLists.txt?

One per directory that defines targets is the usual rule. Don't create a CMakeLists.txt for every folder of headers, and don't define every target in the top-level file of a big project either. A useful check: you should be able to delete a library's directory and its one add_subdirectory line, and nothing else mentions it except the target_link_libraries lines of its users.

Test yourself

Inside a library that may be added to someone else's build, which variable points at the library's own top directory?

  1. CMAKE_SOURCE_DIR
  2. PROJECT_SOURCE_DIR (or CMAKE_CURRENT_SOURCE_DIR)
  3. CMAKE_BINARY_DIR
  4. CMAKE_HOME_DIRECTORY

Where does the executable from apps/shapes-cli/CMakeLists.txt end up by default?

  1. build/
  2. build/bin/
  3. build/apps/shapes-cli/
  4. Next to main.cpp

What is CMAKE_CURRENT_LIST_DIR useful for?

  1. Nothing; it equals CMAKE_CURRENT_SOURCE_DIR
  2. Finding files next to an included .cmake module, which may live elsewhere than the CMakeLists.txt that included it
  3. Listing directories
  4. Setting the output directory