TExcelTAD
Home › C/C++ Packages: Conan 2, vcpkg & CPM.cmake › Overlay Ports: Packaging Your Own Library

Overlay Ports: Packaging Your Own Library

An overlay port is a port folder that lives outside the vcpkg repository and takes priority over it. You use overlays to package an internal library, to add a library vcpkg doesn't have, or to patch an existing port without forking all of vcpkg. They are the vcpkg equivalent of a Conan recipe in your own repository.

Anatomy of a port

text
ports/greet/
├── vcpkg.json        metadata: name, version, dependencies, features
├── portfile.cmake    how to get the source, build it and install it
└── usage             (optional) the text printed after install

A typical portfile for a CMake project downloads the source and uses the helper functions from the vcpkg-cmake and vcpkg-cmake-config ports:

cmake
vcpkg_from_github(
    OUT_SOURCE_PATH SOURCE_PATH
    REPO acme/greet
    REF "v${VERSION}"
    SHA512 <sha512 of the archive>
    HEAD_REF main
)
vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}")
vcpkg_cmake_install()                       # builds and installs Debug and Release
vcpkg_cmake_config_fixup(CONFIG_PATH lib/cmake/greet)   # moves config files to share/greet and fixes paths
file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")
vcpkg_install_copyright(FILE_LIST "${SOURCE_PATH}/LICENSE")

Other source helpers: vcpkg_from_gitlab, vcpkg_from_git (any Git URL + commit), vcpkg_download_distfile + vcpkg_extract_source_archive (plain tarballs). The SHA512 makes the download tamper-evident: if upstream changes the archive, the build fails instead of silently using different code.

A local overlay port

For an in-house library whose source sits next to the port (handy for this offline example, and for monorepos), the portfile can point SOURCE_PATH at a local folder:

bash · try it yourself
mkdir -p app/ports/greet/src && cd app
cat > ports/greet/src/CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(greet VERSION 1.0.0 LANGUAGES CXX)
add_library(greet greet.cpp)
target_include_directories(greet PUBLIC 
lt;INSTALL_INTERFACE:include>) install(TARGETS greet EXPORT greetTargets) install(FILES greet.h DESTINATION include) install(EXPORT greetTargets NAMESPACE greet:: DESTINATION share/greet FILE greetConfig.cmake) TXT printf '#pragma once\n#include <string>\nstd::string greeting(const std::string& who);\n' > ports/greet/src/greet.h printf '#include "greet.h"\nstd::string greeting(const std::string& who) { return "Hello, " + who + "!"; }\n' > ports/greet/src/greet.cpp cat > ports/greet/vcpkg.json <<'JSON' { "name": "greet", "version": "1.0.0", "description": "A tiny in-house greeting library", "license": "MIT", "dependencies": [ { "name": "vcpkg-cmake", "host": true }, { "name": "vcpkg-cmake-config", "host": true } ] } JSON cat > ports/greet/portfile.cmake <<'TXT' set(SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}/src") vcpkg_cmake_configure(SOURCE_PATH "${SOURCE_PATH}") vcpkg_cmake_install() vcpkg_cmake_config_fixup() file(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include") file(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/copyright" "MIT\n") TXT cat > vcpkg.json <<'JSON' { "builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471", "dependencies": [ "greet", "fmt" ] } JSON cat > vcpkg-configuration.json <<'JSON' { "overlay-ports": [ "./ports" ] } JSON vcpkg install 2>&1 | grep -E '^ +[*]? *[a-z-]+:x64|overlay|^-- |Restored|completed successfully'
    fmt:x64-linux@12.2.0#1
    greet:x64-linux@1.0.0 -- /home/learner/app/./ports/greet
  * vcpkg-cmake:x64-linux@2025-08-07
  * vcpkg-cmake-config:x64-linux@2026-07-21
Restored 3 package(s) from /opt/vcpkg-cache in 21.5 ms. Use --debug to see more details.
/home/learner/app/./ports/greet: info: installing overlay port from here
-- Configuring x64-linux
-- Building x64-linux-dbg
-- Building x64-linux-rel
-- Performing post-build validation
All requested installations completed successfully in: 788 ms

Reading the output: greet is marked -- /home/learner/app/./ports/greet, so it came from the overlay; fmt came from the builtin registry and was restored from the binary cache; greet was built, Debug then Release, and the post-build validation passed. vcpkg then wrote the new package to the binary cache, so the next install restores it.

Using it from CMake

The overlay is configured in vcpkg-configuration.json, so the CMake toolchain picks it up with no extra flags:

bash · try it yourself
mkdir -p app/ports/greet/src && cd app
printf 'cmake_minimum_required(VERSION 3.25)\nproject(greet CXX)\nadd_library(greet greet.cpp)\ntarget_include_directories(greet PUBLIC 
lt;INSTALL_INTERFACE:include>)\ninstall(TARGETS greet EXPORT t)\ninstall(FILES greet.h DESTINATION include)\ninstall(EXPORT t NAMESPACE greet:: DESTINATION share/greet FILE greetConfig.cmake)\n' > ports/greet/src/CMakeLists.txt printf '#pragma once\n#include <string>\nstd::string greeting(const std::string& who);\n' > ports/greet/src/greet.h printf '#include "greet.h"\nstd::string greeting(const std::string& who) { return "Hello, " + who + "!"; }\n' > ports/greet/src/greet.cpp printf '{ "name": "greet", "version": "1.0.0", "dependencies": [ { "name": "vcpkg-cmake", "host": true }, { "name": "vcpkg-cmake-config", "host": true } ] }\n' > ports/greet/vcpkg.json printf 'vcpkg_cmake_configure(SOURCE_PATH "${CMAKE_CURRENT_LIST_DIR}/src")\nvcpkg_cmake_install()\nvcpkg_cmake_config_fixup()\nfile(REMOVE_RECURSE "${CURRENT_PACKAGES_DIR}/debug/include")\nfile(WRITE "${CURRENT_PACKAGES_DIR}/share/${PORT}/copyright" "MIT\\n")\n' > ports/greet/portfile.cmake echo '{ "builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471", "dependencies": [ "greet", "fmt" ] }' > vcpkg.json echo '{ "overlay-ports": [ "./ports" ] }' > vcpkg-configuration.json cat > CMakeLists.txt <<'TXT' cmake_minimum_required(VERSION 3.25) project(app CXX) find_package(greet CONFIG REQUIRED) find_package(fmt CONFIG REQUIRED) add_executable(app main.cpp) target_link_libraries(app PRIVATE greet::greet fmt::fmt) TXT printf '#include <fmt/core.h>\n#include "greet.h"\nint main() { fmt::print("{}\\n", greeting("vcpkg overlay")); }\n' > main.cpp cmake -B build -S . -G Ninja -DCMAKE_BUILD_TYPE=Release \ -DCMAKE_TOOLCHAIN_FILE="$VCPKG_ROOT/scripts/buildsystems/vcpkg.cmake" > /dev/null 2>&1 cmake --build build ./build/app
[1/2] Building CXX object CMakeFiles/app.dir/main.cpp.o
[2/2] Linking CXX executable app
Hello, vcpkg overlay!

Overriding a builtin port

An overlay port with the same name as a builtin port replaces it. That's how you apply an urgent patch to, say, zlib without waiting for upstream vcpkg: copy ports/zlib from the vcpkg repository into your overlay folder, add the patch to vcpkg_from_github(... PATCHES fix.patch), and bump port-version. Keep a note to delete the overlay once the fix lands upstream; forgotten overlays are a classic source of "why is our zlib so old?".

Checking a port

Command Purpose
vcpkg install <port> --overlay-ports=ports classic-mode test of the port
vcpkg format-manifest ports/greet/vcpkg.json canonical formatting
vcpkg x-add-version greet record the version in a registry's version database (Custom Registries)
--editable build from the existing source tree without re-extracting (port development)

Post-build validation checks for common mistakes: headers in debug/include, missing copyright file, absolute paths in config files, mismatched library linkage. Fix the portfile rather than disabling the checks.

Test yourself

An overlay port named zlib exists in your overlay folder. Which zlib does vcpkg use?

  1. The builtin one
  2. The overlay one: overlays take priority over registries
  3. Both
  4. It reports a conflict

What does vcpkg_cmake_config_fixup() do?

  1. Runs CMake tests
  2. Moves the installed CMake config files to share/<port> and fixes their paths for vcpkg's layout
  3. Updates the baseline
  4. Formats the portfile

Why does vcpkg_from_github require a SHA512?

  1. For faster downloads
  2. So a changed or tampered archive fails the build instead of silently being used
  3. GitHub requires it
  4. To compute the version