Skip to content

Instantly share code, notes, and snippets.

@MangaD
Created March 7, 2025 17:46
Show Gist options
  • Select an option

  • Save MangaD/458c6d538d65b7d3e71e410a2be08a81 to your computer and use it in GitHub Desktop.

Select an option

Save MangaD/458c6d538d65b7d3e71e410a2be08a81 to your computer and use it in GitHub Desktop.
Comprehensive Guide to Vcpkg for C++ Package Management

πŸ“Œ Comprehensive Guide to Vcpkg for C++ Package Management

CC0

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.


πŸ”Ή 1. What is Vcpkg?

βœ” 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.


πŸ”Ή 2. Installing Vcpkg

βœ… Windows Installation

git clone https://github.com/microsoft/vcpkg.git
cd vcpkg
.\bootstrap-vcpkg.bat

βœ… Linux/macOS Installation

git 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!


πŸ”Ή 3. Installing a C++ Library

Vcpkg provides precompiled libraries that integrate with CMake and MSBuild.

βœ… Install a Single Package

vcpkg install boost

βœ” Downloads and installs Boost with all dependencies.

βœ… Install Multiple Packages

vcpkg install boost openssl fmt

βœ… Install Debug + Release Versions

vcpkg install boost:x64-windows

βœ” :x64-windows β†’ Installs both Debug & Release libraries
βœ” :x64-windows-static β†’ Installs static libraries
βœ” :x64-linux β†’ Installs Linux versions

βœ… List Installed Packages

vcpkg list

πŸ”Ή 4. Integrating Vcpkg with CMake

To 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)

βœ… Using the Vcpkg Toolchain

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)

πŸ”Ή 5. Using Vcpkg in MSBuild (Visual Studio)

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

πŸ”Ή 6. Searching for Packages

βœ… Find Available Libraries

vcpkg search json

βœ” Lists available JSON libraries (e.g., nlohmann-json).

βœ… Get Package Information

vcpkg info boost

βœ” Shows version, dependencies, and supported platforms.


πŸ”Ή 7. Removing and Updating Packages

βœ… Uninstall a Library

vcpkg remove boost

βœ… Remove All Installed Packages

vcpkg remove --all

βœ… Update Vcpkg and All Packages

git pull
vcpkg update
vcpkg upgrade

βœ” Keeps dependencies up to date!


πŸ”Ή 8. Using Vcpkg in CI/CD (GitHub Actions)

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


πŸ”Ή 9. Creating Custom Packages in Vcpkg

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 mylib

2️⃣ Create CONTROL File:

Source: mylib
Version: 1.0
Description: My custom C++ library

3️⃣ 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!


πŸ”Ή 10. Optimizing Vcpkg for Large C++ Projects

βœ… Enable Binary Caching

To avoid rebuilding packages, enable caching:

vcpkg x-set-binarycache C:/vcpkg_cache

βœ” Saves prebuilt libraries for faster installations.

βœ… Use a Custom Vcpkg Registry

For private/internal packages, set up a registry:

vcpkg registry add myregistry https://myserver.com/vcpkg

βœ” Helps manage in-house libraries.


πŸ“Œ Summary

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

πŸš€ Next Steps

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment