What is ABI?
- C++ course
- Expert
- Lesson 19/47
- 5 min
Learn the ABI (Application Binary Interface) concept.
What is ABI?
The ABI (Application Binary Interface) defines how compiled object files talk to each other. It is not source syntax but binary compatibility: calling convention, struct layout, name mangling and vtable.
If two compilers follow the same ABI, one can ship a compiled library and the other a main program and they link. When ABIs differ, even identical source code yields mismatched symbol names and link errors. This is why GCC and Clang share the Itanium ABI; MSVC uses a different one.
ABI components
The four core ABI components are: calling convention (how parameters are passed via registers or stack), struct layout (how members are arranged in memory), name mangling (encoding the function signature into a symbol name) and vtable (the virtual function table).
If any of these four changes, the ABI breaks. For example, adding a new virtual function to a class changes the vtable; old compiled code would call the wrong function. This is why library authors must keep ABI stable.
Practice Write the compute function
Write int compute(int x) with body return x * 3;. In main write std::cout << compute(4);.
Starter code
#include <iostream>
// Define the compute function here
int main() {
// Call compute and print it
return 0;
}Sample solution
#include <iostream>
int compute(int x) {
return x * 3;
}
int main() {
std::cout << compute(4);
return 0;
}Expected output
12
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does ABI define?
g++ -c lib.cpp
clang++ main.cpp lib.o- Only source code syntax
- The compiler version number
- Binary compatibility of executables Correct answer
- Class naming style
Explanation: ABI defines binary details like calling convention, layout and mangling.
2 Complete the extern C declaration
extern "C" void ____();
extern "C" void greet();
Explanation: The greet function is declared without mangling via extern "C".
3 Match each component to its role
| Term | Meaning |
|---|---|
calling convention |
Parameter passing |
struct layout |
Member arrangement |
name mangling |
Symbol naming |
vtable |
Virtual function table |