TExcelTAD
Home › C/C++ Packages: Conan 2, vcpkg & CPM.cmake › Source, Binaries & ABI

Source, Binaries & ABI

A package manager for C++ is really two things: a way to get source code and a way to reuse binaries built from it. Building from source is always possible but slow (Boost or Qt take many minutes); reusing binaries is fast but only safe when the binary is ABI-compatible with your program. This lesson shows what ABI means in practice, so the settings and options in the rest of the course make sense.

API vs ABI

  • The API (application programming interface) is what the compiler checks: function names, parameter types, classes in the headers. Break it and your code doesn't compile.
  • The ABI (application binary interface) is what the linker and the CPU rely on: mangled symbol names, the size and layout of classes, calling conventions, how exceptions unwind, which runtime library allocates memory. Break it and your code may not link, or worse, it links and crashes.

Two builds of the same library from the same source can have different ABIs, because the ABI also depends on the compiler, the standard library and the flags.

A real ABI mismatch

GCC's libstdc++ has two std::string implementations: the C++11 one (the default since GCC 5) and the old copy-on-write one, selected with _GLIBCXX_USE_CXX11_ABI=0. Conan models this as the setting compiler.libcxx=libstdc++11 vs libstdc++. Watch what happens when a library and an app disagree:

bash · try it yourself
mkdir -p abi && cd abi
cat > name.h <<'SRC'
#include <string>
std::string user_name();
SRC
cat > name.cpp <<'SRC'
#include "name.h"
std::string user_name() { return "ada"; }
SRC
cat > main.cpp <<'SRC'
#include <cstdio>
#include "name.h"
int main() { std::printf("user: %s\n", user_name().c_str()); }
SRC
# the "package": built with the old string ABI
g++ -c -D_GLIBCXX_USE_CXX11_ABI=0 name.cpp -o name_old.o
nm -C name_old.o | grep user_name
# the "package": built with the new (default) ABI
g++ -c name.cpp -o name_new.o
nm -C name_new.o | grep user_name
echo "--- app (default ABI) + old package"
g++ main.cpp name_old.o -o app 2>&1 | grep -o 'undefined reference to .*' 
echo "--- app (default ABI) + matching package"
g++ main.cpp name_new.o -o app && ./app
0000000000000000 T user_name()
0000000000000000 T user_name[abi:cxx11]()
--- app (default ABI) + old package
undefined reference to `user_name[abi:cxx11]()'
--- app (default ABI) + matching package
user: ada

The two object files export different symbols for the same function: the new ABI's std::string is std::__cxx11::basic_string, so the mangled name changes and the linker can't match them. Here the failure is loud. Other ABI breaks are silent: a struct whose layout changes with a #define, or memory allocated by one C runtime and freed by another on Windows.

What decides whether a binary fits

Input Example values Conan name vcpkg
OS, architecture Linux, Windows, macOS; x86_64, armv8 os, arch settings the triplet (x64-linux)
Compiler + version gcc 13, clang 18, msvc 194 compiler, compiler.version compiler hash in the ABI hash
Standard library libstdc++11, libc++ compiler.libcxx toolchain / triplet
Language standard 17, gnu17, 20 compiler.cppstd toolchain flags
Build type Debug, Release build_type both built per triplet (-dbg, -rel)
Windows C runtime dynamic (/MD) or static (/MT) compiler.runtime VCPKG_CRT_LINKAGE
Library choices shared/static, header-only, features options VCPKG_LIBRARY_LINKAGE, features
Dependencies the exact versions it was built against the requirements dependencies' ABI hashes

Both tools reduce this table to a key, and a binary is reused only when the key matches. Conan hashes the values into a package ID; vcpkg hashes the port files, the triplet, the compiler and the dependencies' hashes into an ABI hash.

A runnable model: from configuration to binary key

This model works like Conan's package ID: write the configuration in a fixed text format, hash it, and look the hash up. A change to any relevant value gives a different ID; a different order of the same values does not.

python · try it yourself
import hashlib

def package_id(settings, options):
    lines = ["[settings]"] + [f"{k}={v}" for k, v in sorted(settings.items())]
    lines += ["[options]"] + [f"{k}={v}" for k, v in sorted(options.items())]
    return hashlib.sha1("\n".join(lines).encode()).hexdigest()[:12]

release = {"os": "Linux", "arch": "x86_64", "compiler": "gcc", "compiler.version": "13",
           "compiler.libcxx": "libstdc++11", "compiler.cppstd": "gnu17", "build_type": "Release"}
opts = {"shared": False, "header_only": False}

cache = {package_id(release, opts): "libfmt.a (Release)"}

def lookup(label, settings, options):
    pid = package_id(settings, options)
    print(f"{label:<22} {pid}  {cache.get(pid, 'MISSING -> build from source')}")

lookup("same config", release, opts)
lookup("same, other order", dict(reversed(list(release.items()))), opts)
lookup("Debug", {**release, "build_type": "Debug"}, opts)
lookup("old string ABI", {**release, "compiler.libcxx": "libstdc++"}, opts)
lookup("shared=True", release, {**opts, "shared": True})
same config            3f5db83ac535  libfmt.a (Release)
same, other order      3f5db83ac535  libfmt.a (Release)
Debug                  b8a2e42f6609  MISSING -> build from source
old string ABI         e8c0f124a7b5  MISSING -> build from source
shared=True            0753deff644e  MISSING -> build from source

The hashes are made up by this model, not Conan's real IDs, but the mechanism is the same. The rest of the course is mostly about controlling these inputs: profiles set the settings, options pick library variants, and package IDs explain when Conan accepts a "close enough" binary.

Header-only libraries

A header-only library ships no compiled code, so most of the table doesn't matter: the same headers work for every compiler and build type. That's why header-only builds are popular (and why fmt and spdlog offer them as an option). The cost is compile time: every file that includes the library compiles its code again.

Test yourself

What does the ABI cover that the API does not?

  1. Function names in the documentation
  2. Mangled symbol names, object layout, calling conventions and runtime libraries
  3. Which headers you include
  4. The library's license

Why did linking the app with name_old.o fail?

  1. The file was empty
  2. The old-ABI build exports user_name[abi:cxx11]
  3. The two builds mangle std::string differently, so the symbol the app needs doesn't exist in the object file
  4. g++ can't link .o files

Which change gives a different binary key for a compiled library?

  1. Listing the same settings in another order
  2. Changing build_type from Release to Debug
  3. Renaming your app
  4. Adding a comment to your CMakeLists.txt