How do I expose stable C APIs over a C++ implementation?

Exposing stable C APIs over a C++ implementation can be done using extern "C" linkage to prevent name mangling, and by defining a clear set of functions that can be called from C code. This ensures that the API remains consistent and usable, regardless of the underlying C++ implementation.

Here’s a simple example demonstrating how to expose a C-compatible API using C++:

#include extern "C" { void c_function() { std::cout << "Hello from C++ function!" << std::endl; } int add(int a, int b) { return a + b; } }

In this example, the `c_function` and `add` functions can be called from C code. By using `extern "C"`, we prevent the C++ compiler from mangling the function names, making them accessible from C.


C API C++ extern "C" stable APIs function exposure