Skip to content

PImpl, ABI, versioning, and compatibility

Difficulty: Advanced

Key concepts

  • application binary interface
  • source compatibility
  • binary compatibility
  • PImpl
  • shared library
  • SONAME
  • symbol visibility
  • name mangling
  • inline namespace
  • deprecation
  • The difference between source compatibility, where old code still compiles, and binary compatibility, where old programs still run, and why a library shipped as a shared library needs both.
  • What a compiled program has copied out of a header: sizes, offsets, inline code, and function names. This is its ABI, and changing any part of it can break programs that were not rebuilt.
  • How PImpl moves a class’s fields into the library, so that they can change.
  • How a Linux shared library is versioned: the SONAME, the file version, exported and hidden symbols.
  • Which common changes are safe, which break old programs, and which break them silently, and how a library announces and retires old features.

The problem: a library other programs are built against

Section titled “The problem: a library other programs are built against”

A team maintains widgets, a small library that draws text progress bars such as [#####.....] 50%. It is installed as a shared library: one file, libwidgets.so.1, that several programs load when they start. Those programs belong to other teams. Some are rebuilt every week; some were built once, two years ago, and nobody plans to rebuild them.

Version 1.1 needs one new feature, a label in front of the bar (upload [####........] 33%), and the label has to be stored somewhere inside each progress bar. The question is what the team may change without breaking the programs that were built against 1.0.

A program that is rebuilt sees the new header and is compiled again, so source compatibility is enough for it. A program that is not rebuilt keeps what its compiler copied from the 1.0 header, and now runs with the 1.1 library. If the two disagree, nothing warns anyone: the program loads and runs with wrong assumptions.

Kind The promise Broken when
Source compatibility Code written for 1.0 still compiles against the 1.1 header, and means the same A function is removed or renamed, a parameter changes to a type existing calls cannot convert to, an overload becomes ambiguous
Binary compatibility A program compiled against 1.0 still runs, unchanged, with the 1.1 library Anything the program copied from the 1.0 header no longer matches the 1.1 library
Behavioural compatibility Old programs see the same results, apart from documented fixes The output, an error, or a default changes in a way programs depend on

The three are independent. A change can keep old programs running and still stop old code from compiling, or the reverse. The rest of this page is mostly about binary compatibility, because it is the one that fails without a compiler message.

# Requirement Design consequence
R1 A program built against 1.0 runs unchanged, with the same output, with 1.1 No change to anything a compiled program depends on: sizes, offsets, names
R2 Code written for 1.0 still compiles against 1.1 Only additions in the public header
R3 1.1 adds a label, which changes what each progress bar stores The stored fields are not in the public header (PImpl)
R4 A program can tell which library it runs with; one that needs 1.1 fails clearly with 1.0 A version query in the library, compared with constants in the header
R5 Only the intended interface can be called from outside Symbols hidden by default; the public class exported on purpose

Step 1: what a compiled program knows about a class

Section titled “Step 1: what a compiled program knows about a class”

When a program is compiled, the compiler reads the library’s header and copies some of what it finds into the program. After that the header is gone; only the copies remain. For a class, the program contains:

  • Its size. A ProgressBar declared as a local variable takes sizeof(ProgressBar) bytes of the program’s stack, and new ProgressBar asks for that many bytes. The number is compiled in.
  • The offset of every field it touches. An inline member function, or any code that reads a public field, is compiled into the program as “read the 4 bytes at offset 4 of this object”.
  • The bodies of inline functions and templates from the header, compiled into the program as they were then.
  • The order of virtual functions. A virtual call is compiled as “call slot 3 of the object’s virtual table”.
  • Constants and default arguments. header_minor and the default value of a parameter are copied into each call site.
  • The name of every library function it calls. Calls to functions defined in the library are left as names, which the system’s dynamic loader looks up in the library, either when the program starts or at the first call (Step 5 shows both). A C++ name includes its namespace, its class, and its parameter types, encoded into one string; this is name mangling.

That list is the class’s part of the library’s application binary interface (ABI): the agreement, below the level of source code, that a compiled program and the library must share. The rules for how classes are laid out and names are mangled come from the platform’s C++ ABI, the Itanium C++ ABI for GCC and Clang on Linux. Everything in the list is the library’s to keep, from the day a program is built against it.

The fix is not to be careful about which fields to add. It is to take the fields out of what programs can see.

Step 2: PImpl keeps the layout in the library

Section titled “Step 2: PImpl keeps the layout in the library”

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

progress_bar.hpp
// A text progress bar such as "[#####.....] 50%". Programs that use it
// see one pointer: the fields live in Impl, which only the library
// defines, so the library can change them without changing the size or
// layout of ProgressBar.
//
// WIDGETS_API, placed between "class" and the name, exports every member
// function of the class from the shared library (see export.hpp).
class WIDGETS_API ProgressBar {
public:
// explicit: an int is never silently turned into a ProgressBar.
// Throws std::invalid_argument if total is zero or less.
explicit ProgressBar(int total); // total must be positive
// Declared here and defined in the library, where Impl is complete.
// The destructor and both assignments delete an Impl, which needs its
// full definition, and only progress_bar.cpp has it. Keeping all five
// in the library also keeps the code that copies, moves, and destroys
// an Impl there, where it changes along with Impl.
~ProgressBar();
ProgressBar(const ProgressBar& other); // deep copy: a new Impl
ProgressBar& operator=(const ProgressBar& other); // deep copy as well
ProgressBar(ProgressBar&& other) noexcept; // takes the pointer; other is left empty
ProgressBar& operator=(ProgressBar&& other) noexcept; // the same, by assignment
void advance(int steps); // steps must not be negative; stops at total
// [[nodiscard]]: the compiler warns if a call's result is ignored.
[[nodiscard]] int done() const;
[[nodiscard]] int total() const;
[[nodiscard]] std::string render(int width) const; // width: cells between [ and ]
private:
// Declared, not defined: Impl is an incomplete type here. Programs can
// hold a pointer to it but cannot know its size or read its fields.
struct Impl;
std::unique_ptr<Impl> impl_; // null only in a moved-from ProgressBar
};

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

progress_bar.cpp
// The definition the header left out. Only this file sees it, so a later
// release may add, remove, or reorder these fields.
struct ProgressBar::Impl {
int total; // no default: the constructor always sets it
int done = 0;
};
// The braces build a Version with major 1 and minor 0.
Version library_version() noexcept { return {1, 0}; }
// Checks the argument before allocating anything. Impl{total} sets total
// and leaves done at its default, 0; make_unique puts that Impl on the
// heap, the one allocation a ProgressBar makes. If anything throws, no
// ProgressBar exists and nothing leaks.
ProgressBar::ProgressBar(int total) {
if (total <= 0) throw std::invalid_argument("a progress bar needs a positive total");
impl_ = std::make_unique<Impl>(Impl{total});
}
// "= default" here, not in the header: unique_ptr deletes the Impl, and
// this is a place where Impl is complete.
ProgressBar::~ProgressBar() = default;
// A deep copy: a new Impl that copies other's fields, so the two bars
// change independently. A moved-from other has no Impl, and its copy
// has none either.
ProgressBar::ProgressBar(const ProgressBar& other)
: impl_(other.impl_ ? std::make_unique<Impl>(*other.impl_) : nullptr) {}
// Copy assignment. Assigning the new unique_ptr to impl_ deletes the old
// Impl only after the new one exists.
ProgressBar& ProgressBar::operator=(const ProgressBar& other) {
if (this != &other) { // bar = bar: nothing to do
// The copy is made before impl_ changes: if it throws, *this is unchanged.
impl_ = other.impl_ ? std::make_unique<Impl>(*other.impl_) : nullptr;
}
return *this;
}
// Moving a unique_ptr copies the address and sets the source to null: no
// allocation, so it cannot throw. Move assignment also deletes the Impl
// the target had. The moved-from bar has no Impl, so calling advance,
// done, total, or render on it would follow a null pointer.
ProgressBar::ProgressBar(ProgressBar&&) noexcept = default;
ProgressBar& ProgressBar::operator=(ProgressBar&&) noexcept = default;
// Adds steps, but never past total: it adds the smaller of steps and what
// is left. Comparing with left first also means done + steps is never
// computed, so a huge steps cannot overflow.
void ProgressBar::advance(int steps) {
if (steps < 0) throw std::invalid_argument("a progress bar cannot go back");
const int left = impl_->total - impl_->done;
impl_->done += steps < left ? steps : left;
}
int ProgressBar::done() const { return impl_->done; }
int ProgressBar::total() const { return impl_->total; }
// Builds "[", then filled '#' cells, then '.' for the rest of width,
// then "] " and the percentage. Both divisions are integer divisions, so
// they round down: 1 of 3 is 33%. Because done never passes total,
// filled is never more than width.
std::string ProgressBar::render(int width) const {
if (width <= 0) throw std::invalid_argument("a progress bar needs a positive width");
// long long, because done * width may not fit in an int.
const auto filled = static_cast<std::size_t>(static_cast<long long>(impl_->done) * width /
impl_->total);
const long long percent = static_cast<long long>(impl_->done) * 100 / impl_->total;
std::string text = "[";
// append(count, character) adds that many copies of the character.
text.append(filled, '#');
text.append(static_cast<std::size_t>(width) - filled, '.'); // the empty cells
// "] " + a std::string gives a std::string, so the + chain works.
text += "] " + std::to_string(percent) + '%';
return text;
}
How this code works: advance and render, with real values

advance never lets done pass total. It first works out left, the steps still missing, and adds whichever is smaller, steps or left. With a total of 3 and done at 1, advance(50) finds left is 2 and adds 2, so done stops at 3.

render(width) makes two integer divisions, each of which rounds down:

  • filled = done * width / total, the number of # cells;
  • percent = done * 100 / total.

The rest of the width is filled with .. Here is the old client from Step 5, with a total of 8 and a width of 20, after each advance(2):

done filled = done * 20 / 8 . cells percent Text
2 40 / 8 = 5 15 25 [#####...............] 25%
4 80 / 8 = 10 10 50 [##########..........] 50%
6 120 / 8 = 15 5 75 [###############.....] 75%
8 160 / 8 = 20 0 100 [####################] 100%

Rounding down shows up when the division is not exact: 1 of 3 at a width of 10 gives 10 / 3 = 3 cells and 100 / 3 = 33 percent, so [###.......] 33%, which one of the tests checks. Because done is at most total, filled is at most width, so width - filled can never go below zero.

Each choice in the header is about what programs must not copy:

  • The only field is one pointer. ProgressBar::Impl is declared but not defined in the header, so a program cannot know its size or layout, and cannot depend on them. sizeof(ProgressBar) is the size of one std::unique_ptr, whatever Impl holds.
  • No inline member functions. Even done() and total(), one line each, are defined in the library. An inline body that reads impl_->done would not even compile while Impl is incomplete; making it compile means defining Impl in the header, and then its layout is part of every program again.
  • All five special members are declared in the header and defined in the library, where Impl is complete. That is required for std::unique_ptr<Impl> to delete an Impl, as C++ idioms explains, and it also matters here: the code that copies, moves, and destroys an Impl stays in the library, where it changes with Impl.
  • Copies are deep. Copying a ProgressBar copies its Impl, so copies are independent. Copy assignment makes the new Impl before it replaces the old one, which gives the strong guarantee: if make_unique throws, *this is unchanged.
  • Moves are noexcept and only move the pointer. A moved-from ProgressBar holds no Impl; it may be assigned to or destroyed, and nothing else.
  • The constructor rejects a total of zero or less, and render rejects a width of zero or less; advance stops at the total. render computes in long long, because done * width may not fit in an int.

Two caveats are easy to miss. const does not reach Impl: inside render() const, impl_ is a const std::unique_ptr<Impl>, but impl_-> still gives a non-const Impl, so a const function could change the bar’s state without a compiler error. A private impl() accessor with const and non-const overloads closes the gap; std::experimental::propagate_const, from a technical specification rather than the standard, does the same. Exceptions and type information cross the boundary too: an exception class thrown out of the library, and any polymorphic class whose type is checked or caught in programs, must be exported like ProgressBar, or a catch in the program may not recognise it. (This is the GCC visibility guidance; the example throws only standard exceptions and does not test it.)

PImpl’s costs, which C++ idioms lists, are real: a heap allocation per object, an indirection on every call, and no inlining across the library boundary. For a library that must stay binary compatible, they buy the right to change the class. A second benefit is a compile firewall: changing Impl recompiles only progress_bar.cpp, not every file that includes the header.

Programs depend on the exported functions of libwidgets.so.1 by name. Impl is inside the library, so it can change between releases.
Text description of the diagram

Three groups. The old client, built with the 1.0 header, has copied the size of one pointer and header_minor 0. The new client, built with the 1.1 header, has copied the size of one pointer and header_minor 1. The library, libwidgets.so.1, is either the 1.0.0 or the 1.1.0 file. Inside it, an exported part holds the ProgressBar functions and library_version, plus set_label in 1.1; a hidden part holds ProgressBar::Impl, with fields total and done in 1.0, and label, total, and done in 1.1. Both clients call the exported part by mangled name, the new client including set_label. The exported part uses Impl.

Diagram source (Mermaid)
flowchart LR
subgraph Old["old_client, built with the 1.0 header"]
  OC["main<br/>copied: one-pointer size,<br/>header_minor = 0"]
end
subgraph New["new_client, built with the 1.1 header"]
  NC["main<br/>copied: one-pointer size,<br/>header_minor = 1"]
end
subgraph Lib["libwidgets.so.1: file 1.0.0 or 1.1.0"]
  API["exported: ProgressBar functions,<br/>library_version, and in 1.1 set_label"]
  IMPL["hidden: ProgressBar::Impl<br/>1.0 total, done<br/>1.1 label, total, done"]
end
OC -->|"calls by mangled name"| API
NC -->|"calls by mangled name, including set_label"| API
API --> IMPL

The 1.1 header adds one function and changes one constant:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

progress_bar.hpp
// The same class as in 1.0, line for line, plus set_label. Each function
// of 1.0 keeps its exact parameters, so its mangled name, the name old
// programs ask the library for, is unchanged. The private part is the
// same too: still one pointer to an Impl that programs cannot see.
class WIDGETS_API ProgressBar {
public:
explicit ProgressBar(int total); // total must be positive
// All five still defined in the library, where Impl is complete.
~ProgressBar();
ProgressBar(const ProgressBar& other);
ProgressBar& operator=(const ProgressBar& other);
ProgressBar(ProgressBar&& other) noexcept;
ProgressBar& operator=(ProgressBar&& other) noexcept;
void advance(int steps); // steps must not be negative; stops at total
[[nodiscard]] int done() const;
[[nodiscard]] int total() const;
[[nodiscard]] std::string render(int width) const; // width: cells between [ and ]
// Added in 1.1: text shown before the bar. A new non-virtual member
// function adds a symbol to the library and changes nothing that
// programs built with 1.0 depend on.
void set_label(std::string label); // an empty label means none
private:
struct Impl; // still only declared; 1.1 changes its fields
std::unique_ptr<Impl> impl_; // null only in a moved-from ProgressBar
};

The library changes Impl and render:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

progress_bar.cpp
// 1.1 adds a field and puts it first. In a class whose fields were in
// its header, that would change the size and every offset that programs
// were compiled with. Here no program knows Impl's size or layout.
struct ProgressBar::Impl {
std::string label; // empty means "no label"
int total;
int done = 0;
};
// The one place the release number lives at run time: 1.1 here.
Version library_version() noexcept { return {1, 1}; }
ProgressBar::ProgressBar(int total) {
if (total <= 0) throw std::invalid_argument("a progress bar needs a positive total");
// The fields are set in order: {} makes label an empty string, then
// total; done keeps its default, 0. Only this line had to change,
// because label now comes first.
impl_ = std::make_unique<Impl>(Impl{{}, total});
}

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

progress_bar.cpp
// The same arithmetic as 1.0; only the start of the text is new.
std::string ProgressBar::render(int width) const {
if (width <= 0) throw std::invalid_argument("a progress bar needs a positive width");
// long long, because done * width may not fit in an int.
const auto filled = static_cast<std::size_t>(static_cast<long long>(impl_->done) * width /
impl_->total);
const long long percent = static_cast<long long>(impl_->done) * 100 / impl_->total;
// Without a label, the text is exactly what 1.0 produced, so programs
// written for 1.0 see no change in behaviour either.
// Both sides of ?: become a std::string: "[" alone, or the label, a
// space, and "[".
std::string text = impl_->label.empty() ? "[" : impl_->label + " [";
text.append(filled, '#');
text.append(static_cast<std::size_t>(width) - filled, '.');
text += "] " + std::to_string(percent) + '%';
return text;
}
// The new function of 1.1, and its new exported symbol. It takes the
// string by value: a call with a literal, such as set_label("upload"),
// builds the parameter directly, and a call with a named string copies
// it. Then std::move moves the parameter into Impl, which is cheap. Not
// const: it changes the bar.
void ProgressBar::set_label(std::string label) { impl_->label = std::move(label); }

Checked against Step 1’s list, one item at a time:

  • Size and offsets: unchanged. ProgressBar is still one pointer. The new label field is first in Impl, which in a header would have moved every other field, and here moves nothing a program can see.
  • Inline code: there is none to go stale.
  • Virtual functions: there are none.
  • Names: every function of 1.0 still exists with the same parameters, so the same mangled names. set_label is a new name, which old programs do not ask for.
  • Behaviour: without a label, render produces exactly the 1.0 text, so old programs see the same output (R1), and the old code still compiles, because nothing was removed (R2).
  • The version: header_minor becomes 1, so programs built with this header know they need 1.1.

Step 4: the shared library, its name, and what it exports

Section titled “Step 4: the shared library, its name, and what it exports”

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

CMakeLists.txt
# One shared library, released twice. Both releases are built as
# libwidgets.so.1: the "1" is the SONAME, the name a program records when
# it is linked, and it stays the same while releases remain compatible.
# They go to separate directories, so a test can choose which one a
# program loads by setting LD_LIBRARY_PATH.
#
# widgets_release builds one release. It is a CMake function: target,
# dir, and version are its parameters, read below as ${target} and so on.
function(widgets_release target dir version)
# SHARED: a shared library (.so), loaded when a program starts, instead of
# code copied into each program. Each release has its own source file.
add_library(${target} SHARED ${dir}/progress_bar.cpp)
# PUBLIC: the release's header folder and export/ are also used by every
# program that links with this target, so each client gets the right
# widgets/progress_bar.hpp.
target_include_directories(${target} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/${dir} ${CMAKE_CURRENT_SOURCE_DIR}/export)
# OUTPUT_NAME names the library "widgets", so its file is libwidgets.so.
# VERSION gives the real file its full number, as in libwidgets.so.1.0.0.
# SOVERSION is the SONAME's "1". The two visibility settings hide
# everything not marked WIDGETS_API, and the last line puts each release
# in its own folder of the build tree.
set_target_properties(${target} PROPERTIES
OUTPUT_NAME widgets
VERSION ${version}
SOVERSION 1
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN ON
LIBRARY_OUTPUT_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR}/${dir})
endfunction()
# The two releases: 1.0.0 from v1/ and 1.1.0 from v2/.
widgets_release(adv_widgets_1_0 v1 1.0.0)
widgets_release(adv_widgets_1_1 v2 1.1.0)
# The programs do not record where the library is, as an installed
# program would not; each test below says which release to load.
# Each client is compiled with the header of the release it links with.
add_executable(adv_widgets_old_client old_client.cpp)
target_link_libraries(adv_widgets_old_client PRIVATE adv_widgets_1_0)
add_executable(adv_widgets_new_client new_client.cpp)
target_link_libraries(adv_widgets_new_client PRIVATE adv_widgets_1_1)
set_target_properties(adv_widgets_old_client adv_widgets_new_client
PROPERTIES SKIP_BUILD_RPATH ON)
# widgets_run registers a test that runs a client with one release and
# compares its output with a file. The test runs CMake in script mode
# (-P) on the site's compare script, which fails unless the program exits
# with status 0 and prints exactly the expected text. $<TARGET_FILE:...>
# becomes the path of the built program. LD_LIBRARY_PATH tells the loader
# which folder, and so which release, to load libwidgets.so.1 from.
function(widgets_run name client dir expected)
add_test(NAME ${name}
COMMAND ${CMAKE_COMMAND}
-DEXE=$<TARGET_FILE:${client}>
-DEXPECTED=${CMAKE_CURRENT_SOURCE_DIR}/${expected}
-P ${LLD_COMPARE_SCRIPT})
set_tests_properties(${name} PROPERTIES LABELS "example" TIMEOUT 120
ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/${dir}")
endfunction()
# The compatibility promise, tested: a program built with 1.0 gives the
# same output with 1.1, apart from the version it reports.
widgets_run(adv_widgets_old_client_with_1_0.output adv_widgets_old_client v1
expected_old_client_with_1_0.txt)
widgets_run(adv_widgets_old_client_with_1_1.output adv_widgets_old_client v2
expected_old_client_with_1_1.txt)
widgets_run(adv_widgets_new_client_with_1_1.output adv_widgets_new_client v2
expected_new_client_with_1_1.txt)
# And its limit: a program that uses set_label cannot run with 1.0.
# With PASS_REGULAR_EXPRESSION, CTest ignores the exit status and passes
# the test if the output matches: either the loader's message or the
# program's own. Output that matches neither, such as a normal run,
# fails it.
add_test(NAME adv_widgets_new_client_with_1_0 COMMAND adv_widgets_new_client)
set_tests_properties(adv_widgets_new_client_with_1_0 PROPERTIES LABELS "example" TIMEOUT 120
ENVIRONMENT "LD_LIBRARY_PATH=${CMAKE_CURRENT_BINARY_DIR}/v1"
PASS_REGULAR_EXPRESSION "undefined symbol|needs widgets 1.1")
# The unit tests, built against the 1.1 header and linked with 1.1.
# lld_add_test is the site's helper that builds and registers a test.
lld_add_test(adv_widgets_tests SOURCES tests/test_progress_bar.cpp)
target_link_libraries(adv_widgets_tests PRIVATE adv_widgets_1_1)

The build file makes both releases, so that the tests can run old and new programs against each. Three settings carry the design; the fourth, SKIP_BUILD_RPATH, only stops the test programs from recording where the build put the library, so that each test can choose a release.

The SONAME. Both releases are built with SOVERSION 1, so both files carry the name libwidgets.so.1. When a program is linked, the linker records that name, not the file name. On the GCC debug build, readelf -d printed, for the 1.1 library and for the new client:

(SONAME) Library soname: [libwidgets.so.1]
(NEEDED) Shared library: [libwidgets.so.1]

The real files are libwidgets.so.1.0.0 and libwidgets.so.1.1.0, with libwidgets.so.1 as a link to whichever is installed. Replacing 1.0.0 with 1.1.0 changes what every program loads, without relinking any of them. That is the promise the SONAME makes: while the number in it stays the same, the library stays binary compatible. A change that breaks compatibility must change it, to libwidgets.so.2. Then old programs keep loading libwidgets.so.1, both can be installed side by side, and a program built against 2.0 cannot load 1.x by mistake.

Hidden by default. CXX_VISIBILITY_PRESET hidden compiles the library with -fvisibility=hidden: nothing it defines is visible to programs unless it is marked, apart from instantiations of standard-library templates, which libstdc++ declares visible. The public class is marked with WIDGETS_API:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

export.hpp
// Marks what the shared library makes visible to programs. The library
// is compiled with hidden visibility, so everything not marked stays
// inside it: no program can call it, so the library may change it freely.
// This is the GCC and Clang spelling for ELF systems such as Linux;
// Windows DLLs use __declspec(dllexport) and __declspec(dllimport).
// The preprocessor replaces each WIDGETS_API with this attribute, and
// visibility("default") means "export this", overriding the hidden
// default that the build sets for the library.
#define WIDGETS_API __attribute__((visibility("default")))

Without that setting, every non-static function in every file of the library would be exported, and each one would become something a program could call, and so something that could never change. Hidden symbols also give the loader fewer names to process, and let the compiler call them directly inside the library.

Inline functions hidden too. VISIBILITY_INLINES_HIDDEN adds -fvisibility-inlines-hidden: inline member functions that the library’s own code uses are not exported, even from an exported class. That is safe only because the header has no inline members a program could need from the library, and it is a reminder that an inline function is never part of what the library promises. nm -D --defined-only -C printed these exported names from 1.1, and the same list without set_label from 1.0:

widgets::library_version()
widgets::ProgressBar::ProgressBar(int)
widgets::ProgressBar::ProgressBar(widgets::ProgressBar const&)
widgets::ProgressBar::ProgressBar(widgets::ProgressBar&&)
widgets::ProgressBar::~ProgressBar()
widgets::ProgressBar::operator=(widgets::ProgressBar const&)
widgets::ProgressBar::operator=(widgets::ProgressBar&&)
widgets::ProgressBar::advance(int)
widgets::ProgressBar::done() const
widgets::ProgressBar::total() const
widgets::ProgressBar::render[abi:cxx11](int) const
widgets::ProgressBar::set_label(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)

The list is sorted and shortened. Each constructor and the destructor appear twice in the real output, because the Itanium ABI gives them two entry points. The real output also lists weak std:: symbols, such as members of std::basic_string that the library instantiated: a few with GCC, about twenty with Clang. They come from the standard library’s visibility, not from WIDGETS_API. A linker version script (see the symbol-versioning callout below) gives an exact export list where that matters. Two details in it come from the standard library:

  • std::__cxx11::basic_string is std::string. GCC 5 changed the layout of std::string, and libstdc++ put the new one in a namespace called __cxx11, so that its mangled name differs from the old one. For functions whose signatures mention std::string, a program built for the old string and a library built for the new one cannot be linked by mistake; they fail with an undefined symbol instead of corrupting each other’s strings. The tag does not protect a user type that merely contains a std::string and is passed by reference: its mangled name does not change, so that mismatch is silent.
  • [abi:cxx11] on render is the same mark, for a function whose return type is the new std::string; return types are not part of an ordinary function’s mangled name, so the compiler adds a tag.

Every type in a function’s signature is part of the ABI, standard types included. A C++ class interface across a shared library assumes that programs and library use the same compiler family and standard library. Where that cannot be assumed, the boundary is a plain C interface, as Plugin architecture describes for plugins.

Step 5: old and new programs, each library

Section titled “Step 5: old and new programs, each library”

The version check, in both headers:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

progress_bar.hpp
// The version of the header a program was compiled with. These constants
// are copied into the program when it is compiled; library_version()
// asks the library that is actually loaded when the program runs.
inline constexpr int header_major = 1;
inline constexpr int header_minor = 0;
// A plain value type with two public fields. Programs see its layout, so
// it is frozen for every 1.x release: it can never gain a field.
struct Version {
int major;
int minor;
};
// Defined in the library, so it reports the release that is loaded, not
// the one this header came from. WIDGETS_API (from export.hpp) makes it
// visible to programs; noexcept promises it never throws.
WIDGETS_API Version library_version() noexcept;

header_major and header_minor are copied into a program when it is compiled; library_version() runs in the library that is loaded. Comparing the two tells a program what it was built for and what it got (R4).

Version is the one type in the header whose fields programs see, so its layout is frozen: it can never gain a patch field in 1.x. That is acceptable only because it is a tiny value type that will not need to change, as the small-value-types row of the table in “When to care about the ABI” below describes. Anything likely to grow belongs behind a pointer.

A program written for 1.0:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

old_client.cpp
// Prints the two versions, fills a bar of 8 steps in four calls, then
// copies the bar and replaces it. Every ProgressBar call below goes into
// whichever library is loaded; only the header constants and the size of
// a ProgressBar (one pointer) were fixed when this program was compiled.
int main() {
const widgets::Version running = widgets::library_version(); // asks the loaded library
// header_major and header_minor were copied from the 1.0 header, so this
// program always prints 1.0 for them.
std::cout << "compiled with widgets " << widgets::header_major << '.'
<< widgets::header_minor << ", running with " << running.major << '.'
<< running.minor << '\n';
widgets::ProgressBar bar(8); // the constructor allocates the Impl
for (int step = 0; step < 4; ++step) {
bar.advance(2); // 2, 4, 6, then 8 of 8
std::cout << bar.render(20) << '\n';
}
widgets::ProgressBar copy = bar; // copy constructor: its own Impl
// The temporary ProgressBar(5) is move-assigned into bar: bar takes the
// temporary's Impl and deletes its old one, and the temporary, now
// empty, is destroyed. copy is untouched.
bar = widgets::ProgressBar(5);
bar.advance(1);
std::cout << "copy " << copy.done() << " of " << copy.total() << ", new bar "
<< bar.render(10) << '\n';
}
How this code works: Every Impl the old program creates

The program itself only ever holds pointers; each Impl is made and deleted by code inside the library.

  1. ProgressBar bar(8) reserves one pointer on the stack, which is all the program knows about a ProgressBar. The library’s constructor checks that 8 is positive, allocates an Impl with total 8 and done 0, and stores its address in bar.impl_.
  2. The loop calls advance(2) four times, so done goes 2, 4, 6, 8, and prints a line after each.
  3. ProgressBar copy = bar is a copy constructor, not an assignment. The library allocates a second Impl, a copy of the first: 8 of 8. There are now two, one for each bar.
  4. bar = widgets::ProgressBar(5) first builds a temporary with a third Impl, 0 of 5. Move assignment then gives bar that Impl and deletes the old one, 8 of 8, that bar had. The temporary is left with a null impl_, and at the end of the line its destructor runs and has nothing to delete.
  5. bar.advance(1) makes it 1 of 5, so render(10) gives 10 / 5 = 2 cells and 20 percent: [##........] 20%. copy still says 8 of 8.
  6. At the end of main, the local variables are destroyed in the reverse order of their creation: copy first, then bar. Each destructor deletes the Impl it owns, so all three are gone.

Every step above runs library code, through exported functions, so the same program works when 1.1 is loaded, where each Impl also holds an empty label.

Built against 1.0, it was run twice, without rebuilding: once with the 1.0 library,

Expected output
compiled with widgets 1.0, running with 1.0
[#####...............] 25%
[##########..........] 50%
[###############.....] 75%
[####################] 100%
copy 8 of 8, new bar [##........] 20%

and once with the 1.1 library:

Expected output
compiled with widgets 1.0, running with 1.1
[#####...............] 25%
[##########..........] 50%
[###############.....] 75%
[####################] 100%
copy 8 of 8, new bar [##........] 20%

The two outputs differ only in the version line. That is R1, tested: each run is a CTest test that compares the real output with these files, and the build chooses the library with LD_LIBRARY_PATH.

A program written for 1.1, which uses the label:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

new_client.cpp
int main() {
// set_label arrived in 1.1. Checking the version first turns "the
// library is too old" into a clear message, if the program gets this
// far: see the page for when the system's loader stops it earlier.
const widgets::Version running = widgets::library_version();
// Compatible means the same major version and at least the minor version
// this program was built with: 1.1 or 1.2 would do, 1.0 or 2.0 would not.
if (running.major != widgets::header_major || running.minor < widgets::header_minor) {
std::cerr << "needs widgets " << widgets::header_major << '.' << widgets::header_minor
<< " or later, found " << running.major << '.' << running.minor << '\n';
return 1; // a non-zero exit status reports failure
}
widgets::ProgressBar bar(3);
bar.set_label("upload"); // needs the 1.1 library
for (int step = 0; step < 3; ++step) {
bar.advance(1);
std::cout << bar.render(12) << '\n';
}
}
Expected output
upload [####........] 33%
upload [########....] 66%
upload [############] 100%

Run with the 1.0 library, what happens depends on how the program was linked. The GCC build never reached its version check. The system’s loader stopped it before main:

adv_widgets_new_client: symbol lookup error: adv_widgets_new_client:
undefined symbol: _ZN7widgets11ProgressBar9set_labelENSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEE

The exit status was 127. Ubuntu’s GCC links programs with immediate binding by default (readelf -d shows FLAGS BIND_NOW), so the loader resolves every function a program uses before it starts, and a missing one stops it. This site’s Clang 14 does not: its builds use lazy binding, where a function is looked up on its first call. The Clang builds of the same program, including the sanitizer builds, got as far as their own message, “needs widgets 1.1 or later, found 1.0”, and exit status 1, before anything called set_label. A scratch GCC build linked with -Wl,-z,lazy did the same, and setting LD_BIND_NOW=1 brought back the loader’s error. The test accepts either outcome, since both are a clear failure.

With immediate binding, the missing symbol protects the program before the version check can. With lazy binding, the check is what stops it: without the check, the program would have run until its first call to set_label, and the loader would have ended it there, in the middle of its work. The check is also the only protection when a newer release fixed a bug the program depends on, with no new function to miss.

The rules below are for GCC and Clang on Linux; other platforms have their own ABI with similar rules. The rows marked “observed” were seen on this site’s machine, in the example or a scratch program; the rest follow from the Itanium C++ ABI and were not separately tested.

Change in a public header or exported function Programs not rebuilt Programs rebuilt
Add a new class or free function Safe Safe, unless a name now clashes
Add a non-virtual member function Safe (observed: set_label) Safe, unless a new overload changes which one a call picks
Add, remove, or reorder fields of a class whose fields are in the header Breaks: sizes and offsets differ (observed: a crash) Safe, unless code used a removed public field
Add or change fields of a PImpl Impl Safe (observed: label) Safe
Remove or rename an exported function Breaks: undefined symbol, at load or at the first call Breaks: does not compile
Change a parameter’s type, or add a parameter with a default Breaks: the mangled name changes, so the old one is undefined Usually safe
Change only a function’s return type Breaks silently: same mangled name, wrong value (observed) Usually safe
Add, remove, or reorder virtual functions Breaks: virtual-table slots move; adding one at the end still breaks classes that programs derive from Safe
Change an inline function, a template, or a constant in the header Old programs keep the old code; mixing both in one program breaks the one definition rule Safe
Change a default argument’s value Old programs keep the old default Takes the new default
Change the values of an enum that crosses the boundary Breaks silently: old numbers now mean different things Safe, unless numbers were stored
Build the library with another compiler or standard library ABI May break: standard types such as std::string may differ Rebuild everything with one toolchain

The silent rows are the dangerous ones. In a scratch library whose 1.1 changed only int percent_done() to double percent_done(), both releases exported the same name, _Z12percent_donev, and an old program loaded 1.1 without complaint. It printed 0 with the GCC build and a large meaningless number with the Clang build, where 1.0 had given 50, and exited normally:

// The 1.0 library: int percent_done() { return 50; }
// The 1.1 library: double percent_done() { return 0.5; }
// The mangled name, _Z12percent_donev, encodes the name and the (empty)
// parameter list, but not the return type, so the loader finds the 1.1
// function under the old name. On x86-64, an int and a double are also
// returned in different registers, so the old program reads whatever
// happens to be left in the int register, not the 0.5.
int percent_done(); // what the old program was compiled with
int main() { std::cout << percent_done() << '\n'; }

This fragment was compiled in a scratch program, not in the example.

Version numbers mean promises. A common scheme, often called semantic versioning, gives each part of major.minor.patch one meaning, and the SONAME follows the major number:

  • Patch (1.1.0 to 1.1.1): fixes, with no change to the interface.
  • Minor (1.0 to 1.1): additions only; old programs still run and old code still compiles. Same SONAME.
  • Major (1.x to 2.0): anything that breaks either promise. New SONAME, libwidgets.so.2, so old programs keep the old library.

Inline namespaces make a break loud. If 2.0 must change ProgressBar in a way that breaks the ABI, its header can put the class in an inline namespace:

namespace widgets {
// "inline" makes v2's members usable as widgets::ProgressBar, but every
// mangled name still includes v2, so 1.x names and 2.0 names never match.
inline namespace v2 {
class WIDGETS_API ProgressBar { /* the 2.0 class */ };
} // namespace v2
} // namespace widgets

Source code still says widgets::ProgressBar: an inline namespace’s members are found as if they were in the enclosing namespace. But the mangled names change. In a scratch program, the constructor was exported as _ZN7widgets2v211ProgressBarC1Ei, with v2 in it, by both compilers. A program built for 1.x cannot pick up 2.0’s functions by accident; it fails with an undefined symbol, like the new client above, instead of running with the wrong layout. This is the technique libstdc++ used for std::__cxx11::basic_string. The fragment above is illustrative; a reduced form of it, without the export macro, was the one compiled.

Deprecation gives users time. A function that will be removed in the next major version is first marked:

struct ProgressBar {
// Still declared and still working; a call to it compiles with a
// warning that includes the text in quotes.
[[deprecated("use set_label")]] void set_title(std::string title);
void set_label(std::string label); // the replacement
};

This fragment was compiled in a scratch program, not in the example. Code that calls set_title gets a warning that names the replacement; programs already built are unaffected, because the function is still there. One trap: in a scratch build with this site’s flags, which include -Werror, both GCC 11.4 and Clang 14 turned the warning into an error; both messages said that set_title is deprecated and included the “use set_label” text. For users who build with warnings as errors, a deprecation is a source break. Say so in the release notes, and keep the deprecated function, exported and working, until the next major version.

The example’s tests check the promises, not only the behaviour:

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

test_progress_bar.cpp
// static_assert(condition) is checked by the compiler: if the condition
// is false, this file does not compile. These three cost nothing at run
// time.
//
// A ProgressBar is one pointer, whatever Impl holds; adding a field to
// Impl cannot change this.
static_assert(sizeof(ProgressBar) == sizeof(std::unique_ptr<int>)); // any unique_ptr will do
// The two traits are true when moving is declared noexcept. When a
// std::vector grows, it moves a copyable element such as this one only if
// moving cannot throw; otherwise it copies it.
static_assert(std::is_nothrow_move_constructible_v<ProgressBar>);
static_assert(std::is_nothrow_move_assignable_v<ProgressBar>);
// LLD_TEST(name) defines one test. LLD_CHECK(condition) records a failure
// if the condition is false; LLD_CHECK_EQ(actual, expected) does the same
// for an equality and prints both values when they differ; and
// LLD_CHECK_THROWS(expression, type) fails unless the expression throws
// that type of exception. They come from the site's small test header,
// lld_test.hpp.
//
// Sets up: nothing. Proves: the constants in the 1.1 header and the
// library this test is linked with report the same version.
// Catches a build that compiles this header against another release's
// library.
LLD_TEST(header_and_library_versions_match) {
const widgets::Version running = widgets::library_version();
LLD_CHECK_EQ(running.major, widgets::header_major);
LLD_CHECK_EQ(running.minor, widgets::header_minor);
}

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

test_progress_bar.cpp
// Sets up: a bar of 4 steps, drawn 8 cells wide. Proves: each step fills
// 8 / 4 = 2 cells, from empty to full, with the matching percentage.
LLD_TEST(renders_the_fraction_done) {
ProgressBar bar(4);
LLD_CHECK_EQ(bar.render(8), std::string("[........] 0%"));
bar.advance(1);
LLD_CHECK_EQ(bar.render(8), std::string("[##......] 25%"));
bar.advance(3);
LLD_CHECK_EQ(bar.render(8), std::string("[########] 100%"));
}
// Sets up: a bar of 3 steps, drawn 10 cells wide. Proves: 1 of 3 shows
// 10 / 3 = 3 cells and 33%, both rounded down, and advancing by 50 stops
// at 3 instead of going past the total.
LLD_TEST(rounds_down_and_stops_at_the_total) {
ProgressBar bar(3);
bar.advance(1);
LLD_CHECK_EQ(bar.render(10), std::string("[###.......] 33%"));
bar.advance(50);
LLD_CHECK_EQ(bar.done(), 3);
LLD_CHECK_EQ(bar.render(10), std::string("[##########] 100%"));
}
// Proves: a total of 0, a negative step, and a width of 0 each throw
// std::invalid_argument, and the failed advance left done at 0.
LLD_TEST(rejects_what_it_cannot_show) {
LLD_CHECK_THROWS(ProgressBar(0), std::invalid_argument);
ProgressBar bar(2);
LLD_CHECK_THROWS(bar.advance(-1), std::invalid_argument);
LLD_CHECK_THROWS((void)bar.render(0), std::invalid_argument); // (void) silences [[nodiscard]]
LLD_CHECK_EQ(bar.done(), 0);
}
// Proves: set_label, new in 1.1, puts the label and a space before the
// bar: 1 of 2 steps fills 2 of 4 cells.
LLD_TEST(a_label_comes_before_the_bar) {
ProgressBar bar(2);
bar.set_label("copy");
bar.advance(1);
LLD_CHECK_EQ(bar.render(4), std::string("copy [##..] 50%"));
}

VerifiedCompiled and tested with GNU 11.4.0 and Clang 14.0.0 (C++20, 2026-12-01).

test_progress_bar.cpp
// Sets up: a labelled bar and a copy of it. Proves: changing the original
// afterwards does not change the copy (each has its own Impl), and copy
// assignment then brings the copy up to date with the original.
LLD_TEST(a_copy_is_independent) {
ProgressBar bar(4);
bar.set_label("a");
ProgressBar copy = bar; // the copy constructor
bar.advance(4);
bar.set_label("b");
LLD_CHECK_EQ(copy.render(4), std::string("a [....] 0%"));
copy = bar; // copy assignment: replaces the copy's Impl
LLD_CHECK_EQ(copy.render(4), std::string("b [####] 100%"));
}
// Proves: a move hands the state to the new bar (done is still 1), and
// the moved-from bar, which now holds no Impl, can be given a new value
// by assignment and then used normally.
LLD_TEST(a_moved_from_bar_can_be_assigned_to) {
ProgressBar bar(2);
bar.advance(1);
ProgressBar moved = std::move(bar); // bar keeps a null impl_
LLD_CHECK_EQ(moved.done(), 1);
bar = ProgressBar(5); // the only safe uses: assign to it, or destroy it
LLD_CHECK_EQ(bar.total(), 5);
}
  • The static_asserts fail the build if someone adds a field to ProgressBar itself instead of Impl, or makes a move throw. They are guards on the header, not a proof of compatibility.
  • header_and_library_versions_match catches a build that compiles the header of one release against the library of another.
  • The behaviour and value tests check rendering, rounding, the limits, the label, deep copies, and moved-from objects.
  • The four client runs are the compatibility tests: the old program with both releases, the new one with 1.1, and the new one with 1.0, which must fail clearly. They are the only tests that run a program built against a different release than the one it loads.
  • What is not tested here: a full comparison of two library builds. Tools such as libabigail’s abidiff read the debugging information of two builds and report changed types and removed symbols; they were not run for this page.

All of them run with GCC and Clang and under AddressSanitizer, UndefinedBehaviorSanitizer, and ThreadSanitizer.

class widgets::ProgressBar

exported with WIDGETS_API; sizeof is one pointer

Data members

  • std::unique_ptr<Impl> impl_; Impl is defined only in the library

Member functions

  • explicit ProgressBar(int total); throws std::invalid_argument if total <= 0
  • ~ProgressBar(); defined in the library, where Impl is complete
  • ProgressBar(const ProgressBar& other); deep copy
  • ProgressBar& operator=(const ProgressBar& other); strong guarantee
  • ProgressBar(ProgressBar&& other) noexcept; leaves other with no Impl
  • ProgressBar& operator=(ProgressBar&& other) noexcept;
  • void advance(int steps); stops at total
  • int done() const;
  • int total() const;
  • std::string render(int width) const;
  • void set_label(std::string label); added in 1.1

class widgets::Version

returned by library_version()

Member functions

  • int major;
  • int minor;
ProgressBar in widgets 1.1: one pointer in the header, every function in the library
You are writing ABI stability Why
A shared library that programs you do not rebuild depend on Needed: PImpl, hidden symbols, a SONAME, versions Old programs run against new releases
A plugin interface loaded at run time Needed, often as a C interface Plugins are built separately, sometimes by other compilers
A library linked statically, or built with the program from source Not needed; source compatibility still matters Everything is compiled together; PImpl may still help build times
A header-only or template library Not possible in the same way All code is compiled into each program; every change needs a rebuild
Small value types, such as a Point or Money Usually not worth PImpl An allocation per value costs more than the stability is worth; freeze the layout instead

ABI stability is a promise with a price: the library gives up inline functions, public fields, and freedom to change signatures, and pays an allocation per object. Make the promise only for an interface that other people build against, and say in the documentation what it covers. Inside one program that is always rebuilt as a whole, ordinary classes are simpler and faster.

  • Inline accessors in a PImpl class. One inline done() in the header puts Impl’s layout back into every program.
  • A destructor that is not declared in the header. The compiler then generates it inline in each program, where Impl is incomplete, and std::unique_ptr<Impl> refuses to compile. Declaring it in the header and defining it in the library, even as = default, fixes the error and keeps the destruction code in the library.
  • Default visibility. Every helper becomes a symbol someone can call, and so part of the ABI.
  • Adding a parameter with a default to an exported function, thinking it is harmless. It changes the mangled name.
  • Changing the order or values of an enum that crosses the boundary.
  • Adding a virtual function to an interface that users implement. Their compiled classes have the old virtual table. Add a new interface instead, as Plugin architecture does for an optional capability.
  • Keeping the SONAME across a break. The SONAME is the only thing the system checks; if it does not change, old programs load the incompatible library.
  • Trusting a version check to catch everything. It catches only what the program thinks to compare. The tests that run old programs against new libraries are what show a release is compatible.
  • “Remove done(); nobody should use it.” That breaks both source and binary compatibility: deprecate it in a 1.x release, remove it in 2.0, and change the SONAME to libwidgets.so.2.
  • “Let users customise how the bar is drawn.” A virtual function in ProgressBar would put a virtual table in its ABI. Prefer a separate, small interface for the new hook, or a function object passed in, so that ProgressBar’s layout and functions stay as they are.
  • “Ship it for Windows too.” Define WIDGETS_API as __declspec(dllexport) when building the library and __declspec(dllimport) when using it, switched on a macro that only the library’s own build defines (CMake defines <target>_EXPORTS while it builds a shared library). Windows has no SONAME; the DLL’s file name often carries the major version instead. The MSVC ABI also differs from the Itanium ABI, and an exported class with a std::unique_ptr member draws warning C4251, which PImpl libraries usually silence with care. None of this was tested here.
  • “Make it header-only.” Then there is no binary interface to keep: every program compiles its own copy, and every change needs every program rebuilt. PImpl would only add cost.
  • “Programs built with other compilers must use it.” A C++ class interface is not enough; offer a C interface with opaque handles, as plugin interfaces often do.

Exercise: Safe or breaking?

libwidgets.so.1 is at version 1.1, as on this page. For each proposed change, say whether programs built against 1.1, not rebuilt, still run correctly with the new library; whether code written for 1.1 still compiles; and what version number the release needs.

  1. Add a field int width_hint to ProgressBar::Impl.
  2. Add void reset(); to ProgressBar, defined in the library.
  3. Change void advance(int steps) to void advance(long steps).
  4. Make int done() const inline in the header: int done() const { return impl_->done; }.
  5. Change std::string render(int width) const to return std::string_view, pointing into a buffer inside Impl.
  6. Change render’s output from 50% to 50 % for every bar.
  7. Mark total() with [[deprecated("use limit")]] and add int limit() const.
Show the explained answer
  1. Safe for both, version 1.2 or 1.1.1. No program can see Impl.
  2. Safe for both, version 1.2: a new symbol that old programs do not ask for.
  3. Breaks old programs: advance(int) is a different mangled name from advance(long), so old programs fail with an undefined symbol. Code still compiles, since an int converts to long. A major version, 2.0, with a new SONAME, or keep advance(int) and add the long overload in 1.2; then check that no call becomes ambiguous.
  4. Does not compile as written, because Impl is incomplete in the header (GCC: “invalid use of incomplete type”; Clang: “member access into incomplete type”). Making it compile means defining Impl in the header, which puts its layout back into every program. Worse, the library then stops exporting done(): in a scratch build, the old 1.0 client failed at load with “undefined symbol: _ZNK7widgets11ProgressBar4doneEv”. Both kinds of compatibility break, so this needs 2.0; better, do not do it.
  5. Breaks both. The mangled name of an ordinary function does not include its return type, so most return-type changes, such as int to long, are silent. This one happens to be loud: the [abi:cxx11] tag that std::string adds disappears, so render’s mangled name changes and old programs fail with an undefined symbol. (In a scratch file, GCC 11.4 mangled the two forms as ...6renderB5cxx11Ei and ...6renderEi.) Code such as std::string text = bar.render(10); no longer compiles, because the conversion from std::string_view to std::string is explicit, and code that keeps the view past the next call has a lifetime bug. Version 2.0, new SONAME.
  6. Binary compatible, but not behaviourally compatible. Old programs run, but any that parse the text break. Call it a change in the release notes and decide with the users; many libraries treat it as a major change.
  7. Safe for old programs, version 1.2. Code still compiles, with a warning, unless it is built with -Werror, where the warning is an error. Keep total() until 2.0.
  • A shared library has two interfaces: the source interface in its headers, and the binary interface that compiled programs copied from them: sizes, offsets, inline code, virtual-table order, constants, and mangled names.
  • Binary breaks usually show up only when an old program runs, and some are silent: a changed field layout or return type gives wrong results or memory corruption, not an error.
  • PImpl moves a class’s fields into the library; with no inline functions in the header, the library can change them freely.
  • Hidden visibility keeps everything else out of the ABI. The SONAME carries the compatibility promise: same SONAME, compatible; a break needs a new one.
  • Grow by adding functions, not by changing them; mark what will go with [[deprecated]], and remove it only in a new major version.
  • Test compatibility by running programs built against the old release with the new library.