introduction
vcpkg is a cross-platform package manager for C and C++ libraries, developed and maintained by Microsoft.
It serves a similar purpose to:
- apt on Ubuntu
- brew on macOS
- pip for Python
- npm for Node.js
but specifically for C/C++ libraries.
install vcpkg
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
bootstrap-vcpkg.bat
terminal
it’s better to run “vcpkg install” in Developer PowerShell(or x64 Native Tools Command Prompt) for VS 2022 rather than a common powershell terminal if you want the libraries to be built with the VS2022 MSVC toolset.
version control of libraries
Use vcpkg.json manifest with version constraint
{
"name": "my-project",
"version": "1.0.0",
"dependencies": [
{
"name": "eigen3",
"version>=": "3.4.0"
},
{
"name": "ceres",
"version=": "2.2.0"
}
]
}
Use exact version with baseline (recommended for reproducibility)
{
"name": "my-project",
"version": "1.0.0",
"builtin-baseline": "7f0b1c8c6e0d4f9c...",
"dependencies": [
"eigen3"
]
}
transfer libraries
| Factor | A → B dependency copy | B build | C run |
|---|---|---|---|
| CPU architecture | must match target | must support target | must support target |
| MSVC version | must match for cache reuse | yes | no |
| AVX/SSE support | no | maybe | yes |
| GPU | no | only if CUDA | only if CUDA |
| RAM/disk | performance only | performance only | no |
Binary caching
On computer A:
vcpkg install --binarycaching
Enable caching:
set VCPKG_FEATURE_FLAGS=binarycaching
Then copy the binary cache folder to computer B. On computer B:
- Use same vcpkg commit
- Enable binary caching
- Run
set VCPKG_ROOT=C:\dev\vcpkg
set VCPKG_DEFAULT_BINARY_CACHE=D:\vcpkg-cache
cd C:\path\to\your\project
%VCPKG_ROOT%\vcpkg install --triplet x64-windows
vcpkg install
It will reuse prebuilt binaries instead of rebuilding.
completely offline machine
On machine A:
vcpkg export --raw
or
vcpkg export --zip
or
vcpkg export --triplet x64-windows --zip --output=vcpkg-export
Best practice: zip the whole C:\vcpkg\ and unpack it on B to the same path (or any path, but then you must update paths accordingly).
configuration
# PowerShell
$env:VCPKG_DOWNLOAD_RETRY_COUNT=10
# cmd
set VCPKG_DOWNLOAD_RETRY_COUNT=10
# configure delay between retries:
set VCPKG_DOWNLOAD_RETRY_DELAY_SECONDS=5
common commands
| Triplet | Result |
|---|---|
| x64-windows | Uses .dll |
| x64-windows-static | Fully static .lib |
| x64-linux | ELF .so |
# install a library
vcpkg install yaml-cpp
# specify target build configuration
vcpkg install glog:x64-windows
vcpkg install --triplet x64-windows
# enables user-wide integration of vcpkg with your build environment so that installed libraries are discovered automatically, without manually specifying the toolchain file each time.
vcpkg integrate install
# restore environment to manual mode
vcpkg integrate remove
# list installed libraries, filter that contain specific string "yaml-cpp"
vcpkg list | findstr yaml-cpp
# remove a library
vcpkg remove yaml-cpp
#
vcpkg remove yaml-cpp --recurse
#
vcpkg remove yaml-cpp --recurse --triplet x86-windows
#
vcpkg update
vcpkg upgrade --no-dry-run
# check available versions
vcpkg search eigen3