Your C++ plugin SDK ships as a shared library; which changes to its classes break plugins compiled against the previous release, and why?
answer
- the plugin baked in your header
- sizeof and member offsets
- inline code lives in the plugin
- loud link error or silent corruption
- pimpl keeps sizeof fixed
basics
~20 sChanging anything a plugin compiled in from the old header breaks it: sizes and member offsets, virtual functions, base classes, inline bodies and default arguments, plus any changed signature. Adding non-virtual, non-inline functions or new classes is safe; pimpl keeps layout out of headers.
solid answer
~40 sA plugin binary contains decisions taken from your old header: `sizeof` for stack objects and `new`, member offsets for every inline accessor, virtual-call slots, inline function bodies and default argument values. Breaks are either **loud** – a changed parameter type changes the mangled name, so loading or linking fails – or **silent** – adding, removing or reordering data members, changing a member's type, adding a virtual function or changing a base class shifts layout without changing any symbol, so the plugin reads the wrong bytes. Changing a return type or an inline body is also silent. Safe changes add things the old plugin never saw: new non-virtual, non-inline functions, new classes, new free functions. **Pimpl** keeps the data behind one pointer so `sizeof` never changes.
code
cpp · 22 lines#include <cstddef>
#include <cstdio>
#include <memory>
namespace v1 { struct Settings { int volume; bool muted; }; }
namespace v2 { struct Settings { int volume; double gain; bool muted; }; }
namespace sdk {
class Mixer { // pimpl: the only data member is a pointer
public:
Mixer(); ~Mixer();
private:
struct Impl;
std::unique_ptr<Impl> impl_;
};
struct Mixer::Impl { int volume = 0; double gain = 1.0; bool muted = false; };
Mixer::Mixer() : impl_(std::make_unique<Impl>()) {}
Mixer::~Mixer() = default;
}
int main() {
std::printf("v1: sizeof=%zu offsetof(muted)=%zu\n", sizeof(v1::Settings), offsetof(v1::Settings, muted));
std::printf("v2: sizeof=%zu offsetof(muted)=%zu\n", sizeof(v2::Settings), offsetof(v2::Settings, muted));
std::printf("pimpl Mixer: sizeof=%zu\n", sizeof(sdk::Mixer));
}go deeper
Recall that adding data members or virtual functions to a class in a shipped library breaks code compiled against the old header.
Explain what a compiled caller bakes in from a header: sizeof, offsets, inline bodies and default arguments.
Separate loud breaks from silent ones, diagnose a corruption bug after an upgrade, and apply pimpl or opaque handles to keep layout private.
Turn this into release rules: which header edits need review, how CI detects symbol changes, and when a deliberate break earns a major version.
## What a compiled plugin remembers The **ABI** (application binary interface) is everything two separately compiled binaries must agree on: symbol names, calling conventions, object sizes and alignment, member offsets, virtual-call layout, exception and RTTI data. When a plugin is compiled against `sdk.h` version 1, the compiler bakes these into the plugin's machine code: - `sizeof(Settings)` for every local variable, array and `new Settings`; - the **offset** of each data member, used by every inline accessor and every direct member access; - the slots used for virtual calls; - the **bodies of inline functions** and templates, which are compiled into the plugin itself; - the values of **default arguments**, which the standard evaluates at each call site. Shipping version 2 of the shared library does not recompile the plugin. Every one of those facts must still be true. ## Loud breaks and silent breaks | Change in v2 | Symbol changes? | What the old plugin does | |---|---|---| | Parameter type changed, function removed or renamed | Yes | Fails to link or load: undefined symbol | | Data member added, removed or reordered | No | Allocates the old size, reads old offsets | | Data member's type changed | No | Same as above | | Virtual function added, or a first virtual added to a plain class | No | Slots it compiled in may shift, its own derived classes no longer line up, and a first virtual adds a vptr that moves every offset | | Base class added or changed | No | Pointer adjustments and offsets are wrong | | Return type changed (non-template) | No | Misreads the returned value | | Inline function body changed | No | Keeps running the old body it compiled in | | Default argument value changed | No | Keeps passing the old value | Loud breaks are the lucky ones. Silent breaks pass every link and load and then corrupt memory or misbehave, often far from the change. Adding a member that moves `muted` from offset 4 to offset 16, and `sizeof` from 8 to 24, means that when library code writes a v2 `Settings` into storage a v1 plugin allocated, it writes 16 bytes past the end. That is **undefined behaviour**, not a predictable wrong value. Virtual-call layout is the material on vtables; for this question it is enough that the slot assignment is fixed when the plugin is compiled. ## Changes that are safe The rule of thumb is: only **add** things an old binary could not have depended on. 1. New non-virtual, non-inline member functions and new free functions – new symbols, no layout change. 2. New classes and new enumerations. 3. New `static` member functions. 4. Changes to the bodies of **out-of-line** functions, as long as the behaviour contract holds. 5. New overloads – but only if no existing call in a plugin would now resolve differently when recompiled, which is a source-compatibility question rather than an ABI one. ## Pimpl as an ABI tool The **pointer to implementation** idiom puts all data members in a private `struct Impl` defined only in the library's source file, and keeps one `std::unique_ptr<Impl>` in the public class. The public class's `sizeof` is the size of that pointer forever, and its only inline-visible member is the pointer itself. Members can be added to `Impl` in any release. The costs are a heap allocation per object, an extra indirection, and that every accessor must be out of line – which is the point, since inline accessors are exactly what would bake offsets into plugins. The destructor and any move operations must be defined in the source file, where `Impl` is complete. ## How teams catch breaks - Keep a **list of exported symbols** per release and diff it in CI: removed or changed symbols are loud breaks caught before shipping. - Review header diffs for silent changes: any edit to a class's data members, bases, virtual functions, inline bodies or default arguments. - Build an old plugin binary and run it against the new library as a regression test. - When a break is unavoidable, make it explicit: bump the major version and the shared library's version so old plugins refuse to load rather than misbehave. ## The interview signal Juniors name "adding a virtual". Strong candidates explain *why* – what the plugin compiled in – and separate loud from silent breaks, and they reach for pimpl or opaque handles before being asked.
- Why is changing an inline function's body an ABI problem if nothing fails to link?Every plugin that called it compiled its own copy of the old body, so after the upgrade the library runs the new code while plugins keep the old, and the two can disagree about invariants. If the inline function touches data members it also hard-codes their old offsets. Keep functions that may change out of line in the shared library.
- Why do default argument changes not reach old plugins?A default argument is filled in at the call site when the caller is compiled, so `configure(int quality = 2)` changed to `= 3` leaves every old plugin passing 2. No symbol changes and nothing fails; behaviour just differs between old and recompiled plugins.
- Private data members are invisible to plugins, so adding one is safe.
- If the plugin still loads without errors, the ABI is compatible.
- Adding a virtual function at the end of the class is always safe.
- Changing an inline function's body takes effect in every plugin.
- Pimpl is only about compile times, not binary compatibility.