← All languages

Servirtium for

C++

No shipped package: vendor the binding's source at a release tag, with the native library from the release. Release v2.0.0-alpha.20.

FFI mechanism
header-only RAII over the C client. servirtium.hpp (C++17, header-only) wraps the C client (servirtium.h + servirtium.c, compiled as C) and links libservirtium_vcr; there is no second FFI. Destructors stop the server, std::string replaces sv_str, and errors are servirtium::Error exceptions.
Toolchain
A C++17 compiler (c++/g++/clang++) plus a C compiler for the C client's object. No Aether toolchain.
Distribution
Vendor servirtium.hpp + servirtium.h + servirtium.c at the tag, plus the native library
Finding the library
Linked at build time: -L<dir> -lservirtium_vcr with an rpath to <dir>, where <dir> holds a file named exactly libservirtium_vcr.so / .dylib. On macOS the release dylib carries a CI install name, so rewrite it (see the install steps).
On GitHub
cpp/ · README

Getting it

There is no published package for C++. You vendor the binding's source into your project at the release tag v2.0.0-alpha.20 and take the native library from the same release.

  1. Fetch the native library for your OS/arch from the release, verify its checksum, and copy it to the plain name the linker wants (README step 1, collapsed; Linux x86_64 shown).
    TAG=v2.0.0-alpha.20
    REL=https://github.com/servirtium/servirtium-vcr/releases/download/$TAG
    LIBDIR="$HOME/.servirtium-vcr/$TAG"; mkdir -p "$LIBDIR"
    LIB=libservirtium_vcr-$TAG-linux-x86_64.so      # macos-arm64.dylib, linux-arm64-musl.so, freebsd-x86_64.so, ...
    curl -fsSL -o "$LIBDIR/$LIB" "$REL/$LIB"
    curl -fsSL -o "$LIBDIR/$LIB.sha256" "$REL/$LIB.sha256"
    ( cd "$LIBDIR" && sha256sum -c "$LIB.sha256" )   # macOS: shasum -a 256 -c
    cp "$LIBDIR/$LIB" "$LIBDIR/libservirtium_vcr.so"
    
  2. macOS only: give your copy of the dylib an rpath-relative install name (the release one is a Linux CI path) and re-sign it.
    install_name_tool -id @rpath/libservirtium_vcr.dylib "$LIBDIR/libservirtium_vcr.dylib"
    codesign -f -s - "$LIBDIR/libservirtium_vcr.dylib"
    
  3. Vendor the three binding files from the same tag.
    RAW=https://raw.githubusercontent.com/servirtium/servirtium-vcr/$TAG
    mkdir -p third_party/servirtium
    curl -fsSL -o third_party/servirtium/servirtium.h   $RAW/c/include/servirtium.h
    curl -fsSL -o third_party/servirtium/servirtium.c   $RAW/c/src/servirtium.c
    curl -fsSL -o third_party/servirtium/servirtium.hpp $RAW/cpp/include/servirtium.hpp
    
  4. Compile servirtium.c with the C compiler, then your code with the C++ compiler, and link both with the library.
    cc  -std=c99 -fPIC -Ithird_party/servirtium -c third_party/servirtium/servirtium.c -o servirtium_c.o
    c++ -std=c++17 -Ithird_party/servirtium yours.cpp servirtium_c.o \
        -L"$LIBDIR" -lservirtium_vcr -Wl,-rpath,"$LIBDIR" -o yours
    

Record

Point your client at the VCR's base URL; the VCR forwards to the real service and hands back the real response. Nothing is written until you commit.

#include <servirtium.hpp>

{
    auto rec = servirtium::Vcr::record("tapes/climate_api.md", "https://example.org");
    auto body = http_get(rec.base_url() + "/countries");   // your HTTP client
    rec.flush();   // commit: writes the tape; throws servirtium::Error on failure
}   // the destructor stops the server on every path

Playback

The same tape, replayed with no network.

#include <servirtium.hpp>
#include <cassert>

{
    auto vcr = servirtium::Vcr::playback("tapes/single_get.md");
    auto body = http_get(vcr.base_url() + "/ok");   // your HTTP client
    assert(body == "ok-body");
    assert(vcr.last_kind() == servirtium::Outcome::Ok);   // or vcr.matched_cleanly()
}   // <- destructor stops the server

Idiomatic extras

Drop a recording instead of writing it

close() on a recording that was neither flushed nor discarded writes nothing and throws; a destructor never throws, it drops the recording and reports on stderr. Call discard() on a failed test.

auto rec = servirtium::Vcr::record("tapes/api.md", upstream);
try {
    run_test(rec.base_url());
    rec.flush();
} catch (...) {
    rec.discard();          // nothing written
    throw;
}

Redact secrets before the tape is written

Every config call throws servirtium::Error carrying the core's message if it refuses. Register the same rules on the playback handle.

rec.redact_credentials();                       // Authorization, Cookie, X-Api-Key, ...
rec.redact_credential_header("X-Tenant-Token");
rec.redact_header("X-Request-Signature");

Volatile values and a rewritten upstream base

Name a value that changes every run with a {{placeholder}}; the tape holds a token and playback matches any live value.

rec.volatile_rule("request body", "<a:MessageID>urn:uuid:{{messageId}}</a:MessageID>");
rec.rewrite_upstream_base();

Gotchas

  • No package: vendor servirtium.hpp, servirtium.h and servirtium.c at the tag and link the downloaded native library.
  • servirtium.c is C: compile it with cc, not c++ (a C++ compiler mangles its symbols and the link fails with undefined reference to sv_...).
  • Move-only Vcr: one owner per handle, so the destructor runs once.
  • -lservirtium_vcr needs the plain file name; the library must be the same tag as the sources.
  • macOS: the release dylib's install name is a Linux CI path; run install_name_tool -id @rpath/... (and codesign -f -s -) on your copy before linking.
  • Windows: take the .dll asset with its .dll.lib import library by hand.

Prompt for your coding agent

Paste this into Claude, Codex, Gemini or any coding agent, in your C++ project. It wraps one of your existing integration tests with Servirtium. The general prompt has a slot for the language instead.

Hey Gemini or Claude or Codex,

I want a test in this C++ project to run without calling a real web service every time. Use Servirtium (https://servirtium.dev), which records HTTP conversations as readable Markdown "tapes" and replays them. Do these steps in order and tell me what you did at each one.

1. Find an existing integration test in this project that calls a web service as a side effect of what it is testing. Prefer one that is slow, flaky, or needs credentials or network because of that call. Tell me which test you chose and why.

2. Add the Servirtium binding for C++. Follow https://servirtium.dev/languages/cpp/ exactly. Servirtium ships no package for this language. Vendor the binding's source into the project at the release tag v2.0.0-alpha.20 and put the native library from the same release where the page says: Vendor servirtium.hpp + servirtium.h + servirtium.c at the tag, plus the native library.
   Toolchain and minimum version: A C++17 compiler (c++/g++/clang++) plus a C compiler for the C client's object. No Aether toolchain..
   How the binding finds the native library: Linked at build time: -L<dir> -lservirtium_vcr with an rpath to <dir>, where <dir> holds a file named exactly libservirtium_vcr.so / .dylib. On macOS the release dylib carries a CI install name, so rewrite it (see the install steps).

3. Route that test's HTTP client to the Servirtium VCR's base URL instead of the real service, using the idiomatic C++ way of setting a base URL or HTTP client. The VCR starts on its own port (use port 0, so it picks a free one) and gives you its base URL; give that URL to the client and never hard-code a port.

4. Run the test once in record mode against the real service. Commit the recording when the test passes and discard it when the test fails: a record VCR that is closed without a commit or a discard writes no tape and raises an error. Use redact_credentials (the spelling for C++ is on the page) so secrets become numbered tokens, and remove volatile request headers such as User-Agent and Date from the tape so playback does not depend on them.

5. Keep the tape beside the test as readable Markdown, in version control. Open it and check there are no secrets or personal data in it before committing.

6. Make the test play back by default, with an explicit switch (for example an environment variable such as SERVIRTIUM_RECORD=1) that re-records.

7. Prove it works offline: make the real service unreachable (network off, or a wrong host) and show the test still passes in playback.

Traps to avoid:
- A record VCR needs Commit() or Discard(); closing it without either writes nothing and raises.
- The VCR runs in your test process on its own port; the system under test must use the VCR's base URL, not the real one.
- No package: vendor servirtium.hpp, servirtium.h and servirtium.c at the tag and link the downloaded native library.
- servirtium.c is C: compile it with cc, not c++ (a C++ compiler mangles its symbols and the link fails with undefined reference to sv_...).
- Move-only Vcr: one owner per handle, so the destructor runs once.
- -lservirtium_vcr needs the plain file name; the library must be the same tag as the sources.
- macOS: the release dylib's install name is a Linux CI path; run install_name_tool -id @rpath/... (and codesign -f -s -) on your copy before linking.
- Windows: take the .dll asset with its .dll.lib import library by hand.

References:
- This language's page: https://servirtium.dev/languages/cpp/
- The binding's directory: https://github.com/servirtium/servirtium-vcr/tree/main/cpp
- Concepts, features and every language's quick start: https://servirtium.dev/llms-full.txt (index: https://servirtium.dev/llms.txt)

How these snippets were checked

macOS arm64 (Apple clang, C++17): release dylib v2.0.0-alpha.20 (sha256 OK, install name rewritten); vendored the three files at v2.0.0-alpha.20; compiled and ran a record-then-replay program (playback single_get.md as the stand-in upstream, Vcr::record + redact_credentials + note + flush(), then Vcr::playback of the new tape: live ok-body, replay ok-body, matched_cleanly true); close() without flush threw the 'without commit or discard' Error and wrote nothing; discard() wrote nothing; the extras (redact_header, redact_credential_header, volatile_rule, rewrite_upstream_base) ran.

They mirror these files in the repository:

See also the feature reference, llms.txt and the cpp/ directory.