Search code examples
c++cexternlinkagecross-language

Declare a C++ function that has C calling convention but internal linkage


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:

  • Declaring it as static extern "C" void f(); which caused a compilation error to the effect that static and extern "C" cannot be used together.
  • Declaring it within an anonymous namespace as namespace { extern "C" void f(); } which turned out to have the same effect as regular namespace, exposing the unmangled unqualified name.

Solution

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