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.
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?