The vcpkg CMake Toolchain
vcpkg's CMake integration is a single toolchain file, $VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake. Point CMake at it, and in manifest mode it does two things during configure: runs vcpkg install for your vcpkg.json, and adds vcpkg_installed/<triplet> to CMake's search paths so find_package() finds the installed packages. Your CMakeLists.txt stays free of vcpkg-specific code.
Configure with the toolchain
mkdir -p app && cd app
cat > vcpkg.json <<'JSON'
{
"builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471",
"dependencies": [ "fmt", "spdlog" ]
}
JSON
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(app CXX)
find_package(spdlog CONFIG REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE spdlog::spdlog)
TXT
cat > main.cpp <<'SRC'
#include <spdlog/spdlog.h>
int main() {
spdlog::set_pattern("[%l] %v");
spdlog::info("built with vcpkg's spdlog {}.{}.{}", SPDLOG_VER_MAJOR, SPDLOG_VER_MINOR, SPDLOG_VER_PATCH);
}
SRC
cmake -B build -S . -DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" 2>&1 \
| grep -E 'Running vcpkg|Restored|Configuring done|Build files'
cmake --build build
./build/app-- Running vcpkg install
Restored 4 package(s) from /opt/vcpkg-cache in 96.2 ms. Use --debug to see more details.
-- Running vcpkg install - done
-- Configuring done (1.0s)
-- Build files have been written to: /home/learner/app/build
[ 50%] Building CXX object CMakeFiles/app.dir/main.cpp.o
[100%] Linking CXX executable app
[100%] Built target app
[info] built with vcpkg's spdlog 1.17.0
-- Running vcpkg install is the toolchain at work: CMake ran vcpkg for you, before it processed find_package. The next configure only re-runs it if vcpkg.json changed.
Presets: the usual setup
Typing the toolchain path is error-prone; commit a CMakePresets.json that uses the VCPKG_ROOT environment variable instead:
mkdir -p app && cd app
cat > vcpkg.json <<'JSON'
{ "builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471", "dependencies": [ "fmt" ] }
JSON
cat > CMakePresets.json <<'JSON'
{
"version": 6,
"configurePresets": [
{
"name": "vcpkg",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"cacheVariables": { "CMAKE_BUILD_TYPE": "Release" }
}
],
"buildPresets": [ { "name": "vcpkg", "configurePreset": "vcpkg" } ]
}
JSON
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(app CXX)
find_package(fmt CONFIG REQUIRED)
add_executable(app main.cpp)
target_link_libraries(app PRIVATE fmt::fmt)
TXT
printf '#include <fmt/core.h>\nint main() { fmt::print("fmt {} via presets\\n", FMT_VERSION); }\n' > main.cpp
cmake --preset vcpkg > /dev/null
grep -E '^(VCPKG_TARGET_TRIPLET|VCPKG_INSTALLED_DIR|VCPKG_MANIFEST_MODE|_VCPKG_INSTALLED_DIR)[:=]' build/CMakeCache.txt
cmake --build --preset vcpkg
./build/appVCPKG_INSTALLED_DIR:PATH=/home/learner/app/build/vcpkg_installed
VCPKG_MANIFEST_MODE:BOOL=ON
VCPKG_TARGET_TRIPLET:STRING=x64-linux
_VCPKG_INSTALLED_DIR:PATH=/home/learner/app/build/vcpkg_installed
[1/2] Building CXX object CMakeFiles/app.dir/main.cpp.o
[2/2] Linking CXX executable app
fmt 120200 via presets
The cache shows the toolchain's decisions: the target triplet x64-linux (detected from the host) and the install folder. Note that when CMake runs vcpkg, packages go into build/vcpkg_installed, not the project folder as with a manual vcpkg install. Both are variables you can set.
Variables you can set
| Variable | Effect |
|---|---|
VCPKG_TARGET_TRIPLET |
build for another triplet, e.g. x64-linux-dynamic, arm64-osx |
VCPKG_HOST_TRIPLET |
triplet for host tools (code generators that must run on the build machine) |
VCPKG_MANIFEST_FEATURES |
;-separated manifest features to enable, e.g. tests |
VCPKG_MANIFEST_INSTALL |
OFF to skip the automatic vcpkg install (if CI installs separately) |
VCPKG_INSTALLED_DIR |
where to install (default ${CMAKE_BINARY_DIR}/vcpkg_installed) |
VCPKG_OVERLAY_PORTS / VCPKG_OVERLAY_TRIPLETS |
extra port or triplet folders |
VCPKG_INSTALL_OPTIONS |
extra arguments for vcpkg install, e.g. --clean-after-build |
VCPKG_CHAINLOAD_TOOLCHAIN_FILE |
your own toolchain file (cross compiler, Android NDK), loaded by vcpkg's |
Chainloading matters because CMake accepts only one CMAKE_TOOLCHAIN_FILE. If you cross-compile with a toolchain file of your own, keep vcpkg's as CMAKE_TOOLCHAIN_FILE and put yours in VCPKG_CHAINLOAD_TOOLCHAIN_FILE (or in the triplet's VCPKG_CHAINLOAD_TOOLCHAIN_FILE). Combining vcpkg with Conan's conan_toolchain.cmake in one build is not supported; pick one package manager per project.
Usage hints
After installing, vcpkg prints how to use each package (find_package(fmt CONFIG REQUIRED) …). These come from the port's usage file, or are heuristically generated from the CMake config files it installed. When a target name is unclear, look in vcpkg_installed/<triplet>/share/<port>/.
Test yourself
What does vcpkg's toolchain file do in manifest mode during CMake configure?
- Nothing until you call a vcpkg CMake function
- Runs vcpkg install for vcpkg.json and adds the installed folder to CMake's search paths
- Downloads CMake
- Builds your project
You need both vcpkg and a cross-compiler toolchain file. What do you do?
- Pass two CMAKE_TOOLCHAIN_FILE values
- Use vcpkg.cmake as CMAKE_TOOLCHAIN_FILE and your file as VCPKG_CHAINLOAD_TOOLCHAIN_FILE
- Merge them by hand
- Use classic mode
How do you enable the manifest feature tests from CMake?
- -DTESTS=ON
- -DVCPKG_MANIFEST_FEATURES=tests
- -DVCPKG_FEATURE_FLAGS=tests
- Edit portfile.cmake