Disclaimer: ChatGPT generated document.
Vcpkg is a C++ package manager that simplifies dependency management across different platforms and compilers. It integrates well with CMake, MSBuild, and Visual Studio, making it an excellent choice for both Windows and cross-platform development.
β Package Manager for C++/C β Simplifies dependency installation
β Cross-Platform β Works on Windows, Linux, macOS
β Integrates with CMake & MSBuild β Automates library linking
β Supports Hundreds of Libraries β OpenSSL, Boost, SDL2, etc.
β Uses Prebuilt Binaries β Faster builds
π Vcpkg vs Conan
| Feature | Vcpkg | Conan |
|---|---|---|
| Focus | C++ dependencies | C++ package management |
| Platforms | Windows, Linux, macOS | Windows, Linux, macOS, embedded |
| Integration | MSBuild, CMake | CMake, Make, Ninja |
| Binary Storage | Per-project | Centralized storage (Artifactory) |
| Use Case | Small to medium projects | Large-scale dependency management |
π Best For: Windows-based projects, small-to-medium C++ applications.
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.batgit clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.shβ This installs Vcpkg and sets it up.
π Ensure CMake and a compiler (GCC/Clang/MSVC) are installed!
Vcpkg provides precompiled libraries that integrate with CMake and MSBuild.
vcpkg install boostβ Downloads and installs Boost with all dependencies.
vcpkg install boost openssl fmtvcpkg install boost:x64-windowsβ :x64-windows β Installs both Debug & Release libraries
β :x64-windows-static β Installs static libraries
β :x64-linux β Installs Linux versions
vcpkg listTo use Vcpkg in a CMake project, add:
cmake_minimum_required(VERSION 3.10)
project(MyApp)
find_package(Boost REQUIRED)
add_executable(myapp main.cpp)
target_link_libraries(myapp PRIVATE Boost::Boost)Vcpkg provides a CMake toolchain file for automatic integration.
π CMake Build Command:
cmake .. -DCMAKE_TOOLCHAIN_FILE=path/to/vcpkg/scripts/buildsystems/vcpkg.cmake
cmake --build .β Automatically finds installed libraries!
π Example: Installing SDL2 and Using in CMake
vcpkg install sdl2:x64-windows
find_package(SDL2 REQUIRED)
target_link_libraries(myapp PRIVATE SDL2::SDL2)To integrate Vcpkg with MSBuild and Visual Studio:
vcpkg integrate installβ Automatically links Vcpkg libraries in Visual Studio projects.
β Works with .sln and .vcxproj files.
π Enable in Visual Studio:
- Open Project Settings β VC++ Directories
- Add
$(VCPKG_ROOT)\installed\x64-windows\include
vcpkg search jsonβ Lists available JSON libraries (e.g., nlohmann-json).
vcpkg info boostβ Shows version, dependencies, and supported platforms.
vcpkg remove boostvcpkg remove --allgit pull
vcpkg update
vcpkg upgradeβ Keeps dependencies up to date!
To use Vcpkg in GitHub Actions:
π .github/workflows/build.yml
name: Build C++ App
on: [push]
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Install Vcpkg
run: |
git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
./bootstrap-vcpkg.sh
./vcpkg install fmt
- name: Configure CMake
run: cmake -B build -DCMAKE_TOOLCHAIN_FILE=$GITHUB_WORKSPACE/vcpkg/scripts/buildsystems/vcpkg.cmake
- name: Build Project
run: cmake --build buildβ Automates Vcpkg setup in CI/CD
β Ensures dependencies are installed before building
If a library is not available in Vcpkg, you can create a custom package.
π Steps to Create a Custom Vcpkg Package 1οΈβ£ Navigate to Vcpkg ports/
cd vcpkg/ports
mkdir mylib2οΈβ£ Create CONTROL File:
Source: mylib
Version: 1.0
Description: My custom C++ library3οΈβ£ Create portfile.cmake:
vcpkg_from_github(
OUT_SOURCE_PATH SOURCE_PATH
REPO myuser/mylib
REF v1.0
SHA512 123456...
HEAD_REF master
)
vcpkg_install_cmake()
vcpkg_fixup_cmake_targets()4οΈβ£ Install Custom Package:
vcpkg install mylibβ Now mylib can be used like any other Vcpkg package!
To avoid rebuilding packages, enable caching:
vcpkg x-set-binarycache C:/vcpkg_cacheβ Saves prebuilt libraries for faster installations.
For private/internal packages, set up a registry:
vcpkg registry add myregistry https://myserver.com/vcpkgβ Helps manage in-house libraries.
| Feature | Details |
|---|---|
| What is Vcpkg? | A package manager for C++ dependencies |
| Best For? | Windows, small-to-medium projects |
| Installation? | vcpkg install <package> |
| Integration? | Works with CMake, MSBuild, CI/CD |
| Binary Caching? | Available for faster builds |
| Custom Packages? | Can create & host private packages |
Would you like:
1οΈβ£ A real-world project setup using Vcpkg and CMake?
2οΈβ£ A guide on optimizing Vcpkg for CI/CD pipelines?
3οΈβ£ A comparison of Vcpkg vs Conan for dependency management?
