TExcelTAD
Home › Game Engine Architecture in C++ › Toolkit: CMake, vcpkg and Conan

Toolkit: CMake, vcpkg and Conan

An engine is a dozen third-party libraries glued to your own code, built for several platforms and configurations. Getting the build right early saves weeks later. The near-universal C++ setup today is CMake to describe the build, a package manager (vcpkg or Conan) to fetch and build dependencies, and CMake presets so every developer and the CI machine configure the project the same way.

Project layout

text
tiny-engine/
  CMakeLists.txt
  CMakePresets.json
  vcpkg.json                 (or conanfile.txt)
  engine/                    the engine library: core, platform, renderer, …
    CMakeLists.txt
    include/engine/…
    src/…
  game/                      the game executable, links the engine
    CMakeLists.txt
    src/main.cpp
  tools/                     editor, asset cooker: also link the engine
  assets/                    source assets
  tests/

Keeping the engine a library that the game, the editor and the tests all link is the build-system half of "keep the editor and runtime separate" from the field notes.

CMakeLists.txt

Modern CMake is about targets: each library or executable declares its own sources, include directories, definitions and dependencies, and PUBLIC/PRIVATE decides what consumers inherit.

cmake
cmake_minimum_required(VERSION 3.25)
project(tiny_engine LANGUAGES C CXX)

set(CMAKE_CXX_STANDARD 20)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)        # compile_commands.json for clangd and clang-tidy

find_package(SDL3 CONFIG REQUIRED)
find_package(glad CONFIG REQUIRED)
find_package(glm CONFIG REQUIRED)
find_package(EnTT CONFIG REQUIRED)
find_package(imgui CONFIG REQUIRED)

add_library(engine STATIC
    engine/src/core/log.cpp
    engine/src/platform/window_sdl3.cpp
    engine/src/render/renderer_gl.cpp)
target_include_directories(engine PUBLIC engine/include)
target_link_libraries(engine
    PUBLIC  glm::glm EnTT::EnTT               # appear in engine headers: consumers need them too
    PRIVATE SDL3::SDL3 glad::glad imgui::imgui)
target_compile_definitions(engine PUBLIC 
lt;
lt;CONFIG:Debug>:ENGINE_DEBUG=1>) if(MSVC) target_compile_options(engine PRIVATE /W4 /permissive-) else() target_compile_options(engine PRIVATE -Wall -Wextra -Wpedantic) endif() add_executable(game game/src/main.cpp) target_link_libraries(game PRIVATE engine)

vcpkg in manifest mode

vcpkg (Microsoft, open source) reads a vcpkg.json next to your CMakeLists.txt and builds the listed ports from source the first time you configure, caching the binaries for next time.

json
{
  "name": "tiny-engine",
  "version": "0.1.0",
  "dependencies": [
    "sdl3",
    { "name": "glad", "features": ["gl-api-33"] },
    "glm",
    "entt",
    { "name": "imgui", "features": ["sdl3-binding", "opengl3-binding"] },
    "joltphysics",
    "miniaudio",
    "sol2",
    "tracy"
  ],
  "builtin-baseline": "<a vcpkg commit sha>"
}

The builtin-baseline pins every port to the versions of one vcpkg commit, so builds are reproducible. vcpkg x-update-baseline (run it in the project folder) moves it forward deliberately. Ports also print their CMake usage after installing (find_package(SDL3 CONFIG REQUIRED) and the target names); read that message rather than guessing target names.

CMakePresets.json

json
{
  "version": 6,
  "configurePresets": [
    {
      "name": "base",
      "hidden": true,
      "generator": "Ninja",
      "binaryDir": "${sourceDir}/build/${presetName}",
      "toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake"
    },
    { "name": "debug",   "inherits": "base", "cacheVariables": { "CMAKE_BUILD_TYPE": "Debug" } },
    { "name": "release", "inherits": "base", "cacheVariables": { "CMAKE_BUILD_TYPE": "RelWithDebInfo" } },
    { "name": "asan",    "inherits": "base", "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug",
        "CMAKE_CXX_FLAGS": "-fsanitize=address,undefined -fno-omit-frame-pointer" } }
  ],
  "buildPresets": [
    { "name": "debug", "configurePreset": "debug" },
    { "name": "release", "configurePreset": "release" }
  ]
}
bash
cmake --preset debug          # configure (vcpkg builds missing dependencies here)
cmake --build --preset debug  # build

Visual Studio, CLion and VS Code's CMake Tools all read CMakePresets.json, so the IDE and the command line agree. Personal tweaks go in an untracked CMakeUserPresets.json.

Conan 2

Conan (JFrog, open source) is the other big C++ package manager, with prebuilt binaries on Conan Center and good support for cross-compiling and private package servers. The equivalent of the manifest is conanfile.txt (or a Python conanfile.py for more control):

ini
[requires]
sdl/3.4.16
glm/1.0.3
entt/4.0.0
imgui/1.92.9b
joltphysics/5.2.0

[generators]
CMakeDeps
CMakeToolchain

[layout]
cmake_layout
bash
conan profile detect                                   # once per machine
conan install . --build=missing -s build_type=Release  # fetch or build packages, write the toolchain
cmake --preset conan-release                           # presets generated by CMakeToolchain
cmake --build --preset conan-release

Package names and versions on Conan Center differ from vcpkg's (sdl instead of sdl3, for example, and versions move often), so check conan search or the Conan Center web page for the current ones. With multi-configuration generators such as Visual Studio, the generated configure preset is called conan-default.

vcpkg Conan 2
Manifest vcpkg.json conanfile.txt / conanfile.py
Binaries built locally, binary cache optional prebuilt on Conan Center when available
Pinning builtin-baseline + overrides exact versions, lockfiles
Strength huge catalogue, simple CMake integration cross-compilation, profiles, private remotes

Either is fine. Pick one; mixing them in one project is pain.

Checking what you built

A build-info print at startup catches "I'm profiling a debug build" and "the define never reached this file" mistakes. Compiler Explorer builds this page's examples as C++20 at -O1 without NDEBUG, so assert is active:

cpp · try it yourself
#include <cassert>
#include <cstdint>
#include <iostream>

#ifndef ENGINE_DEBUG
#define ENGINE_DEBUG 0
#endif

int main() {
    std::cout << "C++ standard: " << __cplusplus << "\n";
#ifdef NDEBUG
    std::cout << "asserts: off (NDEBUG)\n";
#else
    std::cout << "asserts: on\n";
#endif
    std::cout << "ENGINE_DEBUG: " << ENGINE_DEBUG << "\n";
    std::cout << "pointer size: " << sizeof(void*) * 8 << "-bit\n";
    static_assert(sizeof(std::uint32_t) == 4, "engine assumes 32-bit uint32_t");
    static_assert(__cplusplus >= 202002L, "engine requires C++20");
}
C++ standard: 202002
asserts: on
ENGINE_DEBUG: 0
pointer size: 64-bit

IDEs

IDE Notes
Visual Studio (Windows) best debugger on Windows, "Open Folder" with CMake presets, built-in vcpkg integration, Graphics Diagnostics
CLion (JetBrains) CMake-native, cross-platform, good refactoring; free for non-commercial use (check current terms)
VS Code CMake Tools + clangd (reads compile_commands.json) + CodeLLDB or the C/C++ extension's debugger
Xcode needed for Apple platforms and Instruments; generate with cmake -G Xcode
Rider JetBrains' IDE for Unreal and C#, also handles plain CMake C++

Add clang-format (a .clang-format file at the root, format on save) and clang-tidy (checks such as bugprone-*, performance-*, modernize-*) to the project on day one; they are much harder to introduce into a large existing codebase.

Test yourself

What does builtin-baseline in vcpkg.json do?

  1. Chooses the compiler
  2. Pins all port versions to those of a specific vcpkg commit, making builds reproducible
  3. Enables binary caching
  4. Selects Debug or Release

Why link glm with PUBLIC but SDL3 with PRIVATE in the engine target?

  1. SDL3 is a C library
  2. glm types appear in the engine's public headers, so consumers need it; SDL3 is only used inside the engine's .cpp files
  3. PRIVATE links faster
  4. PUBLIC is required for header-only libraries

What are CMake presets mainly for?

  1. Speeding up compilation
  2. Sharing one set of configure and build settings (generator, toolchain, build type, flags) between developers, IDEs and CI
  3. Downloading packages
  4. Replacing CMakeLists.txt