skip to content

Why is passing std::string across a C++ plugin boundary risky when plugins may be built with a different compiler or standard library?

level: seniorimportance: should knowfreq 42%

answer

  1. the standard fixes interfaces, not layout
  2. std::__1 versus std::__cxx11
  3. _GLIBCXX_USE_CXX11_ABI
  4. same name, different layout
  5. C types at the boundary

basics

~20 s

The standard fixes std::string's interface, not its layout; libstdc++ and libc++ differ, and libstdc++ itself has two string ABIs. Mismatches fail to link, or worse, link and corrupt memory, so boundaries use C types or pin one toolchain.

solid answer

~40 s

The standard specifies what `std::string` does, never how it is laid out, so each standard library chooses its own representation. libc++ puts its types in the inline namespace `std::__1`, libstdc++ puts its C++11-conforming `std::string` and `std::list` in `std::__cxx11`, and libstdc++ also keeps the pre-GCC 5.1 implementation selectable with `_GLIBCXX_USE_CXX11_ABI=0`. A function taking `const std::string&` therefore has a different mangled name in each case, which fails loudly at link or load. The dangerous case is quiet: a struct that merely contains a `std::string` keeps one mangled name under both libstdc++ ABIs while its layout differs. So a plugin boundary either requires one compiler, standard library and ABI setting, or uses C types – pointer and length, sized integers, opaque handles – and converts at the edge.

code

cpp · 4 lines
cpp
#include <string>
namespace sdk {
  std::size_t name_len(const std::string& s) { return s.size(); }
}

go deeper

for a junior

Know that standard library types can differ between compilers and library versions, so they are risky in an interface shipped as a binary.

for a middle

Explain libc++'s std::__1 namespace, libstdc++'s std::__cxx11 dual ABI and the _GLIBCXX_USE_CXX11_ABI macro.

for a senior

Recognise the silent case – a struct with a string member keeps its mangled name while its layout changes – and fix it with C types at the boundary.

for a principal

Choose between pinning a toolchain for all plugin authors and a C-shaped boundary, weighing convenience against who you can force to rebuild.

## The standard specifies behaviour, not bytes For `std::string`, `std::vector` or `std::function` the C++ standard lists members, complexity and guarantees. It does not say how many pointers a string holds, where its small buffer sits, or in what order. Each **standard library implementation** makes those choices, and keeps them stable for its own users: - **libc++** (LLVM) – types live in the inline namespace `std::__1`, so `std::string` mangles as `std::__1::basic_string<...>`. Its documentation says the layout of user-observable structs is kept stable across versions (with an asterisk for breaks it deems worth it), and lists vendor-only ABI flags such as `_LIBCPP_ABI_ALTERNATE_STRING_LAYOUT` that change `basic_string`'s layout and "should never be set by the user". - **libstdc++** (GCC) – since GCC 5.1 it has a **dual ABI**. C++11 forbade copy-on-write strings and required `std::list::size` to be constant time, so new implementations were added in the inline namespace `std::__cxx11` while the old ones stayed for existing binaries. The macro `_GLIBCXX_USE_CXX11_ABI` selects which one a translation unit uses; with GCC's default configuration it is 1 (the new ABI), and the manual notes that some distributions configured GCC 5 with 0 as the default. - **MSVC's standard library** is a third, separate implementation, and MSVC uses a different mangling scheme altogether. The libstdc++ manual also notes that the choice is independent of `-std`: compiling one file as C++17 and another as C++20 does not change the string ABI. ## How a mismatch shows up **Loud: the symbol changes.** The SDK exports `sdk::name_len(const std::string&)`. Built against libc++, the symbol mentions `std::__1::basic_string`; built against libstdc++'s new ABI it mentions `std::__cxx11::basic_string`; built with `_GLIBCXX_USE_CXX11_ABI=0`, neither. A plugin built differently references a symbol the host does not define and fails to link or load. libstdc++'s troubleshooting section says exactly this: undefined references involving `std::__cxx11` or the tag `[abi:cxx11]` usually mean objects were built with different values of the macro. **Silent: the symbol does not change.** Mangling encodes parameter *types by name*. A class `sdk::Config` that holds a `std::string` member mangles as `sdk::Config` under both libstdc++ ABIs, yet its size and layout differ. A function taking `const sdk::Config&` links happily, then one side reads the string with the other side's layout – undefined behaviour. GCC can warn about such cases with `-Wabi-tag`, which warns when a type with an ABI tag is used in a context that does not have that tag; Apple clang 21 reported it as an unknown warning option. ## Finding out which ABI a binary uses Run `nm` on the library and demangle: symbols mentioning `std::__1` come from libc++, symbols mentioning `std::__cxx11` or `[abi:cxx11]` from libstdc++'s new ABI, and `std::basic_string` with neither from libstdc++'s old one. When a third-party library was built with the old ABI and cannot be rebuilt, the libstdc++ manual's advice is to recompile your own code with `_GLIBCXX_USE_CXX11_ABI` defined to 0 before any library header is included. ## Beyond layout Two more things must match when C++ objects cross a shared-library boundary: 1. **Ownership and allocation.** A `std::string` grown on one side and destroyed on the other assumes both sides use the same allocator and the same implementation of its destructor. 2. **Exceptions and RTTI.** GCC's documentation of `-fvisibility` notes that an exception class thrown between shared objects must have default visibility so that the `type_info` objects are unified; otherwise a `catch` in the plugin may not match an exception thrown by the SDK. ## Designing the boundary | Approach | What crosses | Works across toolchains | |---|---|---| | Pin the toolchain | `std::string`, `std::vector`, exceptions | No – document compiler, standard library, ABI macro and major flags | | C types at the boundary | `const char*` + length, fixed-width integers, plain structs, opaque handles | Yes | | SDK-owned vocabulary types | A small string or buffer type whose layout the SDK defines and freezes | Yes, at the cost of maintaining it | Most widely distributed plugin SDKs take the second row and offer a header-only C++ wrapper on top, compiled into each plugin, that converts `std::string` to pointer-and-length on the way out and back on the way in. The convenience is kept inside each binary; only stable shapes cross the line. ## What the interviewer is checking That the candidate knows "standard type" does not mean "standard layout", can name the dual ABI and the inline-namespace trick that makes mismatches visible as link errors, and – the senior part – knows the case where they are *not* visible.

  • Why did libstdc++ need a new std::string ABI at all?
    Its pre-GCC 5.1 `std::string` used copy-on-write, which C++11 forbids, and its `std::list` did not store its size, which C++11 requires to be constant time. Changing them changed their layouts, so GCC 5.1 added new implementations in the inline namespace `std::__cxx11` and kept the old ones in the same library for existing binaries, selected per translation unit by `_GLIBCXX_USE_CXX11_ABI`.
  • Is std::string_view a safe boundary type instead?
    Safer, not safe. The standard does not fix its layout: libc++ stores a pointer then a size, while libstdc++ stores the length then the pointer, and it is a different mangled type per standard library. For a boundary that must survive any toolchain, pass `const char*` plus a length explicitly.

  • std::string is standard, so its layout is the same everywhere.
  • Compiling with -std=c++11 or later switches libstdc++ to the new string ABI.
  • If it links, the std::string ABI must match.
  • Mixing libc++ and libstdc++ is fine as long as both are recent.
  • Exceptions can always be caught across shared libraries.