
How CMake, vcpkg, and JSON Work Together: Explained for C++ Developers
Understand how CMake, vcpkg, and JSON connect in C++ projects. Learn about manifests, toolchains, presets, and how everything links together in practice.
This is Rambod and in this video we explain how CMake, vcpkg, and JSON files actually work together in a C++ project. Instead of just giving steps, the focus here is on understanding the logic so you can apply it to any project with confidence.
1) What is vcpkg?
Think of vcpkg as a package manager for C++ projects.
- It downloads, installs, and organizes libraries automatically.
- No more manually searching and linking.
- It works with a manifest file called
vcpkg.json
.
Example manifest:
{
"name": "my-physics-project",
"version-string": "0.1.0",
"dependencies": [ "raylib", "chipmunk" ]
}
When you run vcpkg install
, vcpkg fetches Raylib and Chipmunk, places them in the vcpkg_installed
folder, and prepares them for your build.
2) How CMake connects to vcpkg
CMake is not a compiler — it generates build instructions for compilers. To integrate vcpkg, you use a toolchain file so CMake knows where the libraries live.
This is often referenced in CMakePresets.json
. For example:
- Generator: Ninja for fast builds.
- Binary directory: defines where compiled files go.
- Toolchain file: tells CMake to use the vcpkg configuration.
Together this bridges CMake with the libraries vcpkg installed.
3) CMakePresets.json
Presets simplify how you build projects:
- Store compiler, generator, and toolchain settings.
- No need to retype commands every time.
- Makes builds consistent across machines and teams.
Example fields:
"generator": "Ninja"
"toolchainFile": "path/to/vcpkg.cmake"
"binaryDir": "out/build"
With presets, you can configure once and reuse it everywhere.
4) Understanding CMakeLists.txt
This is the central build file for your project. It typically includes:
cmake_minimum_required(VERSION 3.21)
project(MyPhysicsProject LANGUAGES C CXX)
find_package(raylib CONFIG REQUIRED)
find_package(chipmunk CONFIG REQUIRED)
add_executable(main main.cpp)
target_link_libraries(main PRIVATE raylib chipmunk)
Notice how simple this is — vcpkg and CMake handle all the linking logic for you.
5) How it comes together
At this point you have:
- A
vcpkg.json
that defines your dependencies. - A
CMakePresets.json
that points to your toolchain. - A
CMakeLists.txt
that defines your build. - And your source code that just includes the headers.
Because of this setup, you can just #include "raylib.h"
or #include "chipmunk.h"
, and everything works. No manual paths, no extra config — it is all wired through the manifest and CMake.
6) Why this matters
This workflow saves huge amounts of time:
- Add a new library? Just put it in
vcpkg.json
and runvcpkg install
. - Build on another machine? Just clone, configure with presets, and build.
- No dependency chaos — it is repeatable and clean.
Wrap up
Now you understand the big picture of how CMake, vcpkg, and JSON work together. You can confidently set up projects, manage dependencies, and know what each piece is doing behind the scenes.
For more C++ guides, check rambod.net, subscribe on YouTube, or watch this explanation here: Watch on YouTube.