TExcelTAD
Home › C/C++ Packages: Conan 2, vcpkg & CPM.cmake › The vcpkg CMake Toolchain

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

bash · try it yourself
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:

bash · try it yourself
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/app
VCPKG_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?

  1. Nothing until you call a vcpkg CMake function
  2. Runs vcpkg install for vcpkg.json and adds the installed folder to CMake's search paths
  3. Downloads CMake
  4. Builds your project

You need both vcpkg and a cross-compiler toolchain file. What do you do?

  1. Pass two CMAKE_TOOLCHAIN_FILE values
  2. Use vcpkg.cmake as CMAKE_TOOLCHAIN_FILE and your file as VCPKG_CHAINLOAD_TOOLCHAIN_FILE
  3. Merge them by hand
  4. Use classic mode

How do you enable the manifest feature tests from CMake?

  1. -DTESTS=ON
  2. -DVCPKG_MANIFEST_FEATURES=tests
  3. -DVCPKG_FEATURE_FLAGS=tests
  4. Edit portfile.cmake