C++ Academy · Lesson

CMake Project Layout and CMakeLists.txt

Lay out source trees and write basic CMakeLists files.

Lesson 1 of 414 steps

CMake Project Layout and CMakeLists.txt is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.

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.

Free to start

Learn C++ with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

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

Yes — any 3 lessons of the C++ Academy learning path, including “CMake Project Layout and CMakeLists.txt”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.

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