I'm trying to interface with a C library, which expects me to provide a pointer to a callback function.
As I understand it, according to the standard the callback must have C language linkage, due to possibly different calling convention. I can accomplish this by declaring my callback function as extern "C"
. However this has an undesirable side effect: exposing the function's unqualified and unmangled name to other translation units.
Is it possible to declare a function such that its name has internal linkage (not visible to other translation units), but which can be called from C via a pointer (has appropriate calling convention) using only standard C++?
If it's impossible to make it have internal linkage, is it at least possible to make it keep its C++ name mangling?
I tried:
static extern "C" void f();
which caused a compilation error to the effect that static
and extern "C"
cannot be used together.namespace { extern "C" void f(); }
which turned out to have the same effect as regular namespace, exposing the unmangled unqualified name.extern "C" {
static int my_callback(int a)
{
return a + 1;
}
}
The above code compiles perfectly fine. The symbol would not be visible outside the translation unit and you can invoke the function through a pointer.
You can pass this function to your C code from within this file and rest assured that it doesn't pollute your global namespace.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With