█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/C++/Build Systems: CMake
50 minIntermediate

Build Systems: CMake

After this lesson, you will be able to: Author a CMakeLists.txt for a multi-file C++ project; pull in dependencies with FetchContent or vcpkg.

CMake is the de facto C++ build system. Ugly, but every cross-platform C++ project uses it. Learn enough to build, link, and manage dependencies.

Prerequisites:Concurrency in C++

Why CMake won

C++ has no built-in build system. Every OS has different tooling (Visual Studio on Windows, Makefiles on Linux, Xcode on Mac). CMake generates the native build files for each. One CMakeLists.txt → all platforms. Alternatives (Bazel, Meson, Buck2) exist but CMake is what every library ships with.

Minimal CMakeLists.txt

Single-executable project.

tsx
cmake_minimum_required(VERSION 3.20)
project(myapp LANGUAGES CXX)
set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
# Add the executable
add_executable(myapp
src/main.cpp
src/user.cpp
)
target_include_directories(myapp PRIVATE include)
target_compile_options(myapp PRIVATE -Wall -Wextra -Wpedantic)
# Configure + build
# cmake -B build -DCMAKE_BUILD_TYPE=Release
# cmake --build build -j$(nproc)
# ./build/myapp

Library + dependencies via FetchContent

Pull a header-only or source dependency from GitHub.

tsx
include(FetchContent)
FetchContent_Declare(
fmt
GIT_REPOSITORY https://github.com/fmtlib/fmt.git
GIT_TAG 10.1.1
)
FetchContent_MakeAvailable(fmt)
add_library(mylib STATIC src/mylib.cpp)
target_link_libraries(mylib PUBLIC fmt::fmt)
add_executable(myapp src/main.cpp)
target_link_libraries(myapp PRIVATE mylib)

vcpkg or Conan for big dependency graphs

Like npm for C++. vcpkg is the most-used in 2026.

json
# Install vcpkg (one-time)
# git clone https://github.com/microsoft/vcpkg
# ./vcpkg/bootstrap-vcpkg.sh
# Add a vcpkg.json manifest
{
"name": "myapp",
"version": "0.1.0",
"dependencies": ["fmt", "boost", "sqlite3"]
}
# Configure with the vcpkg toolchain
# cmake -B build -DCMAKE_TOOLCHAIN_FILE=$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake
# In CMakeLists.txt:
# find_package(fmt CONFIG REQUIRED)
# target_link_libraries(myapp PRIVATE fmt::fmt)

💡 CMake learning curve

CMake's syntax is bizarre (`target_link_libraries(myapp PRIVATE foo)`). Start from existing CMakeLists.txt files of small open-source projects and adapt. Skip the older `add_libraries` / `include_directories` global commands, they pollute and don't compose. Always use `target_*` versions.

Common mistakes

Global `include_directories()` / `link_directories()` (use target_* commands). Hardcoding compiler flags in CMakeLists (use presets + CMakePresets.json). Forgetting `find_package` REQUIRED, missing dep produces cryptic failure later. Skipping `CMAKE_BUILD_TYPE` (defaults to no optimization).

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Concurrency in C++
Back to C++
Passion Project: C++ Data Structure Library→