r/cpp_questions • u/Impressive_Gur_471 • 2d ago
OPEN What is the difference between ABI and API and what does it mean to say that C++ does not have a stable ABI while apparently C has a simple and stable ABI?
Can some simple examples be given to clarify this seemingly complicated topic?
What exactly is at stake in making an ABI stable and simple? Who benefits?
60
Upvotes
63
u/dodexahedron 2d ago edited 1d ago
API is about code (symbol naming mostly). Stable API means you can write code now and it should still compile on future versions. It makes no guarantees about already-compiled assets continuing to work together if compiled against different versions.
ABI is about the binary layout of things in the executable image, what shape the structures are, offsets of entrypoints for functions, calling convention, etc.
A stable ABI means that, for example, assets compiled originally against the same version should continue to work with each other even if one of them is later compiled against a different version.
It's important for dynamically linked software in particular, or else an application compiled against a shared dynamic library would break if either it or that library were updated without being re-compiled in that exact pairing (which would defeat a pretty major reason for dynamic linking).
Another teeny tiny case of ABI stability being critical is operating systems presenting a stable ABI to userspace applications. Some people have this bizarre desire for their applications not not break after a routine OS security patch.
Also, be aware that neither ABI nor API stability necessarily implies the other. You can have one, both, or neither.
However, certain kinds of API changes naturally do mean the ABI has definitely changed, too (e.g. changing the order of the parameters in a function is both an API and ABI change).
In short, API is a symbolic contract relevant at compile time (hence the P), and ABI is a binary contract relevant at link time (and, consequently, execution time) in the binary images (hence the B).
17
u/mredding 2d ago
An API defines a software interface. It is a contract of what behaviors exist and how to call them. In C and C++, this is the definition of a header file. Headers define APIs.
Now the difference between a library and a framework has to do with who is in control of the implementation.
A library is an implementation, often as a
.dllor.so, a.aor.lib. It is a library if YOUR PROGRAM maintains control. zlib is a library, just a bundle of tools. The standard template library is a library, and it's not all in headers, there's a runtime that is linked in to go with it.A framework inverts control. The FRAMEWORK is in control, and you pass your implementation TO IT, and it's the framework that decides the lifecycle of the application. A framework can be implemented as a library, as a program, or as a service. As a library, you may have to do a little to get the framework up and running, but it possesses most of the agency of that program, or at least it's facet of it. Qt, Unreal, JUICE are all frameworks.
Notice libraries and frameworks both have APIs.
Win32 is not a library, and it's not a framework, because you control the lifecycle, and while frameworks provide structure, Win32 does not. This is a new category - an SDK, typically a PLATFORM SDK, a Software Development Kit. This is the foundation upon which you build programs for a platform. Open GL is its own platform, and there is the Vulkan SDK. SDKs also come with libraries, sometimes frameworks, tools, and documentation.
THEN THERE IS THE ABI...
This is the Application Binary Interface. This is how two binary units communicate with one another. Your program has to talk to the kernel and other system libraries. This defines how functions are named, and the binary formats so you can load a system library and FIND the entry point to the function. Your program has to speak enough of this language to load a library and call it's implementation. At the very least, your language and tools need to support a Foreign Function Interface.
If you want your program TO BE a system library, you need to present yourself as one. That means your functions have to follow the same conventions, your calling conventions have to be the same, down to value types and pointers or reference types, other contractual expectations.
A system ABI tends to be essentially whatever the native C compiler does, because most operating systems are written in C.
C++ makes things a bit more difficult. You have things like objects, with automatic functions like constructors. Only another C++ program is going to know to honor those conventions, a C or Fortran program won't. So it's very difficult to write a binary layer where a Fortran program is constructing C++ objects and calling such functions. This is why non-system ABIs are useless for everyone but other uses of THAT ABI - same language, same tooling.
C and C++ have different ABIs, essentially because C has a single flat namespace:
There's only the global namespace to name
Innerin the ABI, and C and the ABI correspond 1:1.But C++ has hierarchical namespaces. The above code works just fine. But how do you do that? The system ABI is still a single flat namespace, but C++ isn't. Something has to give.
Enter: name mangling. The C++ language standard says nothing of it. This is a tooling solution to abstract the C++ nested namespace upon a flat namespace. So:
Will become name mangled to something like
_Z3fooN1A5InnerEfollowing a naming schema that guarantees no ambiguity and will flatten into a single global namespace.So a C program can load your C++ library and
dlbinda function pointer to THAT function for that type. According to C and the ABI, it's not anA::Inner, it's anA5InnerEor aB5InnerEto distinguish between the two.Since the language says nothing about mangling or how to do it, it's up to the tooling, and compilers and linkers do it differently. It can even change between versions of the same compiler. It's not portable and not consistent. When you're writing low level code like this, it's a major concern.
That's why C++ has
extern "C", where we can tell the compiler we want the C/system ABI for this block of code, don't mangle it. But that means you're restricted to C rules and types.This explanation is a bit of a rush job, but I hope it provides some clarity.
6
u/mgruner 2d ago
Imagine you have an application that uses an external library to operate. This external library can have a stable API, a stable ABI, both, or neither.
We can understand the whole stability concept by analyzing what happens to your program when you update this library to a new version.
The library has a stable API if you update it to the new version and all of the functions, constants, enums, declarations… you were using continue to work in your program. If any of that changes in a backward-incompatible way, it will break your code and you’ll need to manually change your program in order to make it work against the new API.
The library has a stable ABI if the binary-level conventions your program relies on remain the same. This includes memory layout, calling conventions, symbol naming, alignment, etc. For example, adding a new member to a struct can break the ABI because now that struct may have a different size. Imagine a function that received that struct by value: the compiler needs to know the size of the struct at compile time to correctly pass it as an argument. If the struct changed sizes, horrible things could happen at runtime.
Having a stable ABI means you could update the library without even recompiling your program. If the ABI is broken, but the API is stable, you’ll need to recompile the program. If the API compatibility is also broken, you’ll also need to fix the program to match the new API.
Now, the thing with ABI is that it’s not that trivial to understand. Many things are compiler-specific (one compiler may lay out one thing in memory differently than another). And, the more complex the language, the more potential ABI variations you’ll get across compilers and even compiler versions.
Neither the C nor the C++ language standard specifies an ABI, although both have stable ABIs for specific platforms and toolchains. However, C’s simpler language features make its ABI easier to define and maintain, making it less prone to the madness of ABI instability.
4
u/DeathLeopard 2d ago
When people complain about ABI stability in C++ relative to C I think they're really talking about two specific problems.
10
u/xiao_sa 2d ago
C++ DOES have a stable ABI
3
u/Discordbothost 2d ago
When testing a shared-library update, keep the old executable and run it against the new library before rebuilding it. Successful linking alone doesn't prove ABI compatibility: a symbol's name can stay the same while the expected arguments change.
2
u/DanKegel 2d ago
See e.g. https://www.oracle.com/technical-resources/articles/it-infrastructure/stable-cplusplus-abi.html
2
u/plugwash 1d ago
An API is for source code, and ABI is for binaries.
ABI means different but related things when we talk about compilers or we talk about libraries.
When we talk about compilers ABI refers to the set of rules we use to translate source code constructs into binary constructs. How does the stack work? how are parameters passed? what size are data types? how are structures laid out? and so-on.
When we talk about libraries, ABI reffers to the interface presented by the compiled library. This depends on both the ABI of the compiler and the content of the library's header files.
Importantly changes in a library can break the ABI without breaking the ABI, consider for example a structure whose only use is to be returned from a function. If I add a field to the structure so the function returns more information then I have broken the ABI of the library but not it's API.
Neither the C or the C++ standards defines an ABI. As far as the language is concerned each implementation can choose it's own ABI.
In practice though, the C ABI for a given platform tends to be relatively simple, stable and agreed upon by different compilers targeting that platform.
C++ is a much more complex language than C and so the ABI of the compiler is inevitably also much more complex. The nature of C++ also means that far more internal details of a library tend to become part of the library's ABI.
g++ has tried very hard to maintain a stable ABI over many years. Clang++ has adopted the same ABI at the compiler level but the library ABI for the standard C++ libraries provided by C++ and Clang differ. Even then there have been a few ABI changes over the years, with hacks to allow some level of library interoperability.
For years Microsoft used to declare a C++ ABI break with each new version of visual C++, but I belive they have stopped this in recent years.
2
u/conundorum 23h ago
An API is the interface between two codebases, such as between a program and a library or a driver; the "supplier" codebase will present the API as a means for the "consumer" to link up with it. (E.g., OpenGL or DirectX.) An ABI is the interface between object files, that allows two programs to connect with each other after being compiled. (E.g., name mangling schemes, ELF & P/Invoke, actual struct layouts, etc.)
For a good image, think of electrical wiring. The outlet is the API, and the power grid is the ABI. Most houses will present at least two APIs (the standard outlet for most things, and the heavy-duty outlet for large appliances like ovens), both of which sit on top of the ABI. You can change the API without changing the ABI (you can provide both grounded and ungrounded outlets, tied to the same grid), though you might have problems if the API implies an ABI it doesn't actually support (using a European outlet for a house on the North American grid is a recipe for disaster). If the consumer needs a different API, they might provide a wrapper to interface with your API (computer components & accessories that need a transformer typically plug into a standard grounded outlet, then have an outlet for a standard UPC computer power cord; your tower's PSU typically uses a cord that has a standard wall plug on one end, and a UPC plug on the other end). Different ABIs are usually paired with different APIs, but this isn't a hard requirement (NA and Euro wall sockets have different designs so you can't mix them up, but everyone uses the same UPC plug & socket; wall sockets use unique APIs for each grid, but UPC devices use the same API and a selector switch to identify the ABI in use).
2
u/CreepyWritingPrompt 20h ago
API is functions and data structures. ABI defines how those things are implemented in the hardware.
Sometimes when someone says ABI they include the API in that - so the entire interface of some software thing, all the way down to the hardware. Eg, when people talk about software being compatible with the linux ABI, they are talking about what syscalls exist, what they do, AND how they are invoked (what registers do what)
Other times, ABI specifically means "how are functions called", "how is data arranged". Such as in the context of a programming language discussion. Like this one
3
u/JVApen 2d ago
You can break the API without breaking the ABI, for example:
becoming
You can also break the ABI without breaking the API:
becoming
What do they mean when the talk about unstable ABI? Almost any change to your code will break ABI. Even the people working for years on the standard have to backtrack if it turns out they accidentally broke ABI (ignoring the question on wether that's a good thing or not). Also people working on compilers break it without realizing. Clang has flags to select the version who's ABI to use.
What do people consider a stable ABI? C. Instead of mangling every function into a name, the only use the function name and ignore everything else. In that, changing a 2 to 3 argument function does not break the ABI as you can swap the binaries and the exe starts. You will have troubles when that function is used.
3
u/GoogleIsYourFrenemy 2d ago
The C ABI is not simple.
3
u/alfps 2d ago edited 2d ago
ABI = Application Binary Interface, the memory layout of things. This includes not only memory layout of types but also of function call frames, exception handling, ...
API = a misnomer really, but in C++ it is the set of declarations that a logical module or library exposes, in particular the set of functions.
In web development an API is correspondingly the set of logical functions that a server process exposes to the net.
A literal reading of that question is that an ABI is stable if it does not change in a way that makes existing code that depends on it, stop working.
But that view does not make sense for the C versus C++ distinction.
The common problem with a C++ ABI versus C ABI is that even though basic types typically are the same for all compilers on a given platform (with some exceptions e.g.
long double), in C++ the ABI also includesstd::string,and possibly more that my association circuit failed to pop up (i'm still on first cup of coffee this morning), where all of this depends on the compiler and the standard library implementation.
So a shared library exposing a function returning
std::stringcan in practice only be used with a given compiler (such as Visual C++ or g++) and compilers that are ABI-compatible with it (e.g. clang-cl or the Intel optimizing compiler for Visual C++, and clang++ for g++).I think this is what you intended to ask about, and then it came out wrong when you put that question into words.
1
u/No_Mango5042 2d ago
The ABI is outside the scope of the standard. It would allow you to compile code on one machine and link to it or run it anywhere. In reality it’s easy to produce incompatible binaries, due to language version, compiler flags, calling convention, macros, platform, compiler, standard library version, etc. It’s vendor-specific what actually works, and yes it’s painful.
1
u/flatfinger 2d ago
The reason C became popular, even before there was a standard, is that it wasn't so much a language as a recipe for producing language dialects tailored to individual platforms and purposes. The term "high-level assembler" (a usage the C Standards Committee was, until recently, expressly chartered not to preclude) should be viewed not just as a high-level language that can be used to do things that would typically require assembly language, but also as a language for producing high-level assemblies.
On many platforms' assemblers, the act of putting a piece of code after a label doesn't attach any particular semantics beyond the fact that the label will be assigned the address of the first instruction following it. The assembler would neither know nor care how the code may use any registers, nor whether it alters them, leaves them alone, or uses them temporarily within a save/restore construct. It's possible and sometimes useful to write assembly language programs where different chunks of code treat registers or parts of the stack frame differently, but it's often simpler to have most pieces of code follow a consistent practice where certain things are to be left undisturbed, certain things may be freely used and overwritten, and others may be used if their values are saved beforehand and restored afterward. Pieces of code which follow such conventions may then be used as high level assemblies.
An ABI is a specification for how high-level assemblies should interact with each other. Sometimes the designer of an execution environment will specify an ABI, and sometimes the first entity to produce a C compiler for a platform will invent one, which others may or may not follow.
An API is generally a specification for a particular set of routines, rather than a general recipe for how routines should work with each other. An ABI may specify that every code label should hold the address of the second byte of the first instruction of a routine (that sounds weird, but it's how the normal ARM ABI works), while an API may specify that InitWoozle is the label for a function that expects to be passed certain parameters and will process them a certain way.
1
u/Adexsi29631 2d ago
Programming interface versus binary interface
0
u/Chippors 2d ago
By the same token C doesn't have a stable ABI either. One library may have a function crc16(const void* buf, size_t len), another version a crc16(size_t len, const void* buf), and although both of them work perfectly fine when used appropriately, linking with them in the wrong order will have your program crash when calling crc16().