0Pricing
C++ Academy · Lesson

CMake Project Layout and CMakeLists.txt

Lay out source trees and write basic CMakeLists files.

CMake Project Layout and CMakeLists.txt is a free C++ Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why CMake?

CMake is the de facto standard build system generator for C++. It produces project files for Make, Ninja, Visual Studio, Xcode — from a single description.

Minimal CMakeLists.txt

The smallest useful CMakeLists.txt for a single executable.

cmake_minimum_required(VERSION 3.20)
project(MyApp LANGUAGES CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

add_executable(myapp main.cpp)

Out-of-Source Builds

Always build in a separate directory — keeps the source tree clean.

# from project root
cmake -B build
cmake --build build

Standard Project Layout

A clean structure:

  • CMakeLists.txt
  • src/ — source files
  • include/ — public headers
  • tests/ — tests
  • third_party/ — bundled dependencies

Adding Subdirectories

Recurse into subdirectories with their own CMakeLists.txt.

# top-level
add_subdirectory(src)
add_subdirectory(tests)

Variables and Properties

Set variables with set. Read them with ${var}. Common variables: CMAKE_BUILD_TYPE, CMAKE_CXX_FLAGS.

set(CMAKE_BUILD_TYPE Release)
set(SOURCES src/a.cpp src/b.cpp)

Find Files Dynamically

Use file(GLOB ...) for quick prototypes. For production, list sources explicitly so CMake re-runs when files are added.

# Discouraged for production
file(GLOB SOURCES "src/*.cpp")

# Preferred
add_executable(myapp src/main.cpp src/util.cpp)

Build Configurations

Common configurations: Debug, Release, RelWithDebInfo, MinSizeRel. Set with -DCMAKE_BUILD_TYPE=Release.

cmake -B build -DCMAKE_BUILD_TYPE=Release

Compiler Warnings

Enable warnings as target properties — portable across compilers.

target_compile_options(myapp PRIVATE
    $<$<CXX_COMPILER_ID:GNU,Clang>:-Wall -Wextra>
    $<$<CXX_COMPILER_ID:MSVC>:/W4>
)

Modern CMake Style

Prefer target-based commands (target_*) over global ones. They scope properties cleanly to specific targets.

CMakePresets.json (CMake 3.19+)

Define reusable configurations in CMakePresets.json — shared across team members.

Installing the Project

Use install commands to copy binaries, libraries, and headers to install paths.

install(TARGETS myapp DESTINATION bin)
install(DIRECTORY include/ DESTINATION include)

Quick Check

What is the recommended directory pattern when running CMake?

Recap

CMake is the standard C++ build system generator. Write a CMakeLists.txt, build out-of-source, and use target-based commands. Use presets for shared configurations and properties for compiler flags.

Frequently asked questions

Is the “CMake Project Layout and CMakeLists.txt” lesson free?

Yes — the full text of “CMake Project Layout and CMakeLists.txt” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

What will I learn in “CMake Project Layout and CMakeLists.txt”?

Lay out source trees and write basic CMakeLists files. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C++ Academy?

No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “CMake Project Layout and CMakeLists.txt” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C++ Academy lesson?

Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. CMake Project Layout and CMakeLists.txt
  2. Targets Linking and Visibility PUBLIC PRIVATE
  3. Using vcpkg and Conan for Dependencies
  4. Generating IDE Files and Cross-Platform Builds
← Back to C++ Academy