Extern C and the C ABI
Expose C-compatible interfaces from C++ libraries with extern C.
Extern C and the C ABI is a free C++ Academy lesson on CoddyKit. This is lesson 1 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Why Interop?
C++ has a complex ABI (Application Binary Interface) with name mangling, exceptions, templates. Other languages (C, Python, Rust) cannot consume mangled C++ symbols directly.
extern "C"
Wrap declarations in extern "C" to disable C++ name mangling and use the C calling convention.
extern "C" {
void greet(const char* name);
int add(int a, int b);
}Why C ABI?
The C ABI is the lowest common denominator across languages. Almost every language can call C functions via FFI.
Header File Pattern
Make a header usable from both C and C++ with a conditional extern "C".
#ifdef __cplusplus
extern "C" {
#endif
void hello(void);
int sum(int a, int b);
#ifdef __cplusplus
}
#endifWhat You Cannot Export
Some C++ features have no C equivalent:
- Classes (no class concept in C)
- Templates (resolved at compile time, not exportable)
- Overloaded functions (no name mangling means no overloading)
- Exceptions (must be caught before crossing FFI)
Exposing a Class
Hide the class behind opaque pointers and a C-style API.
// header (C visible)
typedef struct Widget Widget;
Widget* widget_create(int x);
void widget_destroy(Widget*);
int widget_value(const Widget*);
// .cpp
class WidgetImpl { /* ... */ };
extern "C" {
Widget* widget_create(int x) { return reinterpret_cast<Widget*>(new WidgetImpl(x)); }
void widget_destroy(Widget* w) { delete reinterpret_cast<WidgetImpl*>(w); }
}Avoid Exceptions Across FFI
Exceptions thrown from a C++ function called via C will abort. Catch them at the boundary.
extern "C" int safe_call() {
try {
risky();
return 0;
} catch (...) {
return -1;
}
}Memory Ownership
Document who owns what — usually the side that allocates frees. Mismatches lead to leaks or double frees.
Strings Across FFI
std::string is C++-only. Cross FFI as const char*. Decide whether the receiver copies, and document lifetime.
Structs Across FFI
Use POD (plain old data) structs that have the same layout in C and C++. Avoid virtual functions, references, or std types.
Callbacks
Pass function pointers (or void* + function pointer) across FFI. Provide a context pointer for closures.
extern "C" {
typedef void (*Callback)(int value, void* user_data);
void register_callback(Callback cb, void* user_data);
}Linkage and Headers
The extern "C" affects only linkage, not behavior. The function still compiles as C++ inside but is callable as C from outside.
Quick Check
Why wrap C++ functions in extern "C" when exposing them to other languages?
Recap
extern "C" gives a function C linkage — no name mangling, C calling convention. Use it to expose C++ to other languages via FFI. Hide classes behind opaque pointers, catch exceptions at the boundary, and document ownership clearly.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Extern C and the C ABI” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Extern C and the C ABI”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Extern C and the C ABI”?
Expose C-compatible interfaces from C++ libraries with extern C. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
How long does the “Extern C and the C ABI” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Extern C and the C ABI
- Calling Python from C++ via pybind11
- C++ for Rust FFI Boundaries
- Using SWIG for Multi-Language Bindings