Mini Project, Part 2: The Same App with vcpkg
In Part 1 Conan supplied fmt, spdlog and Catch2 to wordstat. Now replace Conan with vcpkg in manifest mode. The goal is to change only the package-manager files: CMakeLists.txt, the sources and the tests stay byte-for-byte the same.
What changes
| Conan (Part 1) | vcpkg (Part 2) |
|---|---|
conanfile.py |
vcpkg.json |
conan.lock |
the builtin-baseline in vcpkg.json |
test_requires("catch2/...") |
a manifest feature tests that adds catch2 |
conan install + Conan's presets |
vcpkg's toolchain in a committed CMakePresets.json; CMake runs vcpkg install itself |
| profile (gcc 13, gnu17, Release) | triplet x64-linux + CMAKE_BUILD_TYPE |
| binaries from the Conan cache/remote | binaries from the vcpkg binary cache |
The manifest uses a feature for the test dependency, so a consumer that only wants the tool never installs Catch2. The preset turns that feature on with VCPKG_MANIFEST_FEATURES.
Build it
bash · try it yourself
mkdir -p wordstat/src wordstat/tests && cd wordstat
cat > vcpkg.json <<'JSON'
{
"name": "wordstat",
"version": "1.0.0",
"builtin-baseline": "5d4a3dc90c0b3773f3bac81bf41297b54328f471",
"dependencies": [ "fmt", "spdlog" ],
"features": {
"tests": { "description": "Build the unit tests", "dependencies": [ "catch2" ] }
}
}
JSON
cat > CMakePresets.json <<'JSON'
{
"version": 6,
"configurePresets": [
{
"name": "vcpkg-release",
"generator": "Ninja",
"binaryDir": "${sourceDir}/build",
"toolchainFile": "$env{VCPKG_ROOT}/scripts/buildsystems/vcpkg.cmake",
"cacheVariables": { "CMAKE_BUILD_TYPE": "Release", "VCPKG_MANIFEST_FEATURES": "tests" }
}
],
"buildPresets": [ { "name": "vcpkg-release", "configurePreset": "vcpkg-release" } ],
"testPresets": [
{ "name": "vcpkg-release", "configurePreset": "vcpkg-release", "execution": { "jobs": 1 } }
]
}
JSON
cat > CMakeLists.txt <<'TXT'
cmake_minimum_required(VERSION 3.25)
project(wordstat VERSION 1.0.0 LANGUAGES CXX)
find_package(fmt REQUIRED CONFIG)
find_package(spdlog REQUIRED CONFIG)
add_library(wordstat_core src/stats.cpp)
target_include_directories(wordstat_core PUBLIC src)
target_link_libraries(wordstat_core PUBLIC fmt::fmt)
add_executable(wordstat src/main.cpp)
target_link_libraries(wordstat PRIVATE wordstat_core spdlog::spdlog)
include(CTest)
if(BUILD_TESTING)
find_package(Catch2 3 REQUIRED CONFIG)
add_executable(wordstat_tests tests/test_stats.cpp)
target_link_libraries(wordstat_tests PRIVATE wordstat_core Catch2::Catch2WithMain)
include(Catch)
catch_discover_tests(wordstat_tests EXTRA_ARGS --rng-seed 1)
endif()
TXT
cat > src/stats.h <<'SRC'
#pragma once
#include <map>
#include <string>
struct Stats {
int words = 0;
int lines = 0;
std::map<std::string, int> counts;
};
Stats analyse(const std::string& text);
std::string report(const Stats& s, int top);
SRC
cat > src/stats.cpp <<'SRC'
#include "stats.h"
#include <algorithm>
#include <cctype>
#include <sstream>
#include <vector>
#include <fmt/format.h>
Stats analyse(const std::string& text) {
Stats s;
std::istringstream in(text);
std::string line;
while (std::getline(in, line)) {
++s.lines;
std::istringstream words(line);
std::string w;
while (words >> w) {
std::string clean;
for (char c : w)
if (std::isalpha(static_cast<unsigned char>(c)))
clean += static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
if (clean.empty()) continue;
++s.words;
++s.counts[clean];
}
}
return s;
}
std::string report(const Stats& s, int top) {
std::vector<std::pair<std::string, int>> v(s.counts.begin(), s.counts.end());
std::stable_sort(v.begin(), v.end(), [](auto& a, auto& b) { return a.second > b.second; });
std::string out = fmt::format("{} lines, {} words, {} distinct\n", s.lines, s.words, s.counts.size());
for (int i = 0; i < top && i < static_cast<int>(v.size()); ++i)
out += fmt::format("{:>2}. {:<8} {:>3}\n", i + 1, v[i].first, v[i].second);
return out;
}
SRC
cat > src/main.cpp <<'SRC'
#include <spdlog/spdlog.h>
#include <fmt/core.h>
#include "stats.h"
int main() {
spdlog::set_pattern("[%l] %v");
const std::string text =
"The build is green.\n"
"The cache is warm, the build is fast.\n"
"Ship the build!\n";
spdlog::info("built with fmt {}", FMT_VERSION);
spdlog::info("analysing {} bytes", text.size());
Stats s = analyse(text);
fmt::print("{}", report(s, 3));
if (s.words > 10) spdlog::warn("long text: {} words", s.words);
}
SRC
cat > tests/test_stats.cpp <<'SRC'
#include <catch2/catch_test_macros.hpp>
#include "stats.h"
TEST_CASE("counts words and lines") {
Stats s = analyse("one two\nthree\n");
REQUIRE(s.lines == 2);
REQUIRE(s.words == 3);
}
TEST_CASE("ignores case and punctuation") {
Stats s = analyse("Build, build! BUILD.");
REQUIRE(s.counts.size() == 1);
CHECK(s.counts.at("build") == 3);
}
TEST_CASE("report lists the most frequent word first") {
std::string r = report(analyse("b a b"), 1);
CHECK(r == "1 lines, 3 words, 2 distinct\n 1. b 2\n");
}
SRC
echo "=== 1. configure (runs vcpkg install)"
cmake --preset vcpkg-release 2>&1 | grep -E 'Running vcpkg|^ +[*]? *[a-z0-9-]+(\[.*\])?:x64-linux|Restored|Build files'
echo "=== 2. build"
cmake --build --preset vcpkg-release
echo "=== 3. run"
./build/wordstat
echo "=== 4. test"
ctest --preset vcpkg-release
./build/wordstat_tests --rng-seed 1=== 1. configure (runs vcpkg install)
-- Running vcpkg install
catch2:x64-linux@3.16.0
fmt:x64-linux@12.2.0#1
spdlog[core,fmt,tz-offset]:x64-linux@1.17.0#1
* vcpkg-cmake:x64-linux@2025-08-07
* vcpkg-cmake-config:x64-linux@2026-07-21
Restored 5 package(s) from /opt/vcpkg-cache in 225 ms. Use --debug to see more details.
-- Running vcpkg install - done
-- Build files have been written to: /home/learner/wordstat/build
=== 2. build
[1/6] Building CXX object CMakeFiles/wordstat_core.dir/src/stats.cpp.o
[2/6] Linking CXX static library libwordstat_core.a
[3/6] Building CXX object CMakeFiles/wordstat.dir/src/main.cpp.o
[4/6] Building CXX object CMakeFiles/wordstat_tests.dir/tests/test_stats.cpp.o
[5/6] Linking CXX executable wordstat
[6/6] Linking CXX executable wordstat_tests
=== 3. run
[info] built with fmt 120200
[info] analysing 74 bytes
3 lines, 15 words, 8 distinct
1. the 4
2. build 3
3. is 3
[warning] long text: 15 words
=== 4. test
Test project /home/learner/wordstat/build
Start 1: counts words and lines
1/3 Test #1: counts words and lines ...................... Passed 0.00 sec
Start 2: ignores case and punctuation
2/3 Test #2: ignores case and punctuation ................ Passed 0.00 sec
Start 3: report lists the most frequent word first
3/3 Test #3: report lists the most frequent word first ... Passed 0.00 sec
100% tests passed out of 3
Total Test time (real) = 0.01 sec
Randomness seeded to: 1
===============================================================================
All tests passed (5 assertions in 3 test cases)
What to check:
- Configure: CMake printed
Running vcpkg install; vcpkg planned five packages (the three libraries plus the two host helpers) and restored all of them from the binary cache.spdlog[core,fmt,tz-offset]shows the features in use. - Build: Ninja compiled only your three source files; nothing from the dependencies.
- Run: identical output to Part 1 except one line:
built with fmt 120200. vcpkg's baseline has fmt 12.2.0, while Conan resolved 12.1.0. Same code, sameCMakeLists.txt, a different but compatible fmt, which is exactly why each project pins its versions. - Test: the same three CTest tests and five assertions pass, with the same seed.
Conan or vcpkg for this project?
Both worked with no code changes, so the choice is about the surroundings:
| Question | Points to |
|---|---|
| Do you need exact control over versions (fmt 12.1.0 exactly) per project? | Conan (or a vcpkg override) |
| Do you want one consistent, tested set of library versions with little configuration? | vcpkg |
| Will you publish wordstat itself as a binary package to other teams? | Conan with a remote |
| Is the team on Visual Studio/MSBuild already? | vcpkg integrates there out of the box |
| Do you build for many configurations (Debug/Release × compilers × OSes)? | Conan profiles scale best; vcpkg needs a triplet per combination |
Extend it
- Add a
benchfeature withbenchmarkas a dependency and awordstat_benchtarget built only when the feature is on (on a machine with network access). - Write an overlay port for
wordstat_coreitself (Overlay Ports), so another project can depend on it. - Add the vcpkg job to the CI workflow next to the Conan one, both using the same
cteststep. - Try a third variant with CPM.cmake and
CPM_USE_LOCAL_PACKAGES, and count how many files it compiles (When CPM Is Enough).
Test yourself
Why did the vcpkg build print built with fmt 120200 while the Conan build printed 120100?
- A bug in fmt
- vcpkg's baseline has fmt 12.2.0; Conan's graph resolved 12.1.0
- vcpkg upgrades packages automatically
- FMT_VERSION is random
How does the vcpkg version keep Catch2 out of builds that don't need tests?
- It can't
- Catch2 is in a manifest feature
tests, enabled through VCPKG_MANIFEST_FEATURES - Catch2 is header-only
- With an override
Which files had to change to move from Conan to vcpkg?
- CMakeLists.txt and the sources
- Only the package-manager files: conanfile.py/conan.lock replaced by vcpkg.json and a CMakePresets.json
- Everything
- Only main.cpp