0Pricing
C++ Academy · Lesson

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 — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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
}
#endif

What 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.

Frequently asked questions

Is the “Extern C and the C ABI” lesson free?

Yes — the full text of “Extern C and the C ABI” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Extern C and the C ABI
  2. Calling Python from C++ via pybind11
  3. C++ for Rust FFI Boundaries
  4. Using SWIG for Multi-Language Bindings
← Back to C++ Academy