I have a C/C++ program that’s a plugin to Firefox. Because it’s a plugin, it has non-main entry points. Those entrypoints need to be compiled in C because otherwise they get name mangled.However, other functions are over-loaded, so they need to be C++. The solution is extern “C”. That much I’ve already figured out.
However, if I extern “C” around the .c file, I get link errors because the C++ files got name mangled, but the calls in the .c file didn’t. At least I THINK that’s what’s happening.
The solution APPEARS to be to put the extern “C” around the .h file. This SEEMS to mean that the names of the functions declared in the .h file aren’t mangled, even when they’re defined in the (presumably mangled) .c file.
However, it doesn’t really make any sense to me why this would work. Is this a kludge? Am I setting myself up for a hard to find bug later? or is this the right way to solve this?
Extern “C” should apply to function prototype, so if you have separate prototype and implementation, put extern declaration around prototypes. Implementation, provided prototype is visible, will be extern as well and not mangled. It is not a bug.