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.
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.
Single-executable project.
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 executableadd_executable(myappsrc/main.cppsrc/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
Pull a header-only or source dependency from GitHub.
include(FetchContent)FetchContent_Declare(fmtGIT_REPOSITORY https://github.com/fmtlib/fmt.gitGIT_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)
Like npm for C++. vcpkg is the most-used in 2026.
# 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)
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.