0

In C++, can I check whether a certain function has been declared "extern C" or not?

  • I don't mind checks restricted to the current translation unit.
  • I'd like to check this at compile-time, but if this is somehow possible only at run-time, that's useful too.
  • Non-standard, compiler-extension-based solutions are relevant.
einpoklum
  • 118,144
  • 57
  • 340
  • 684
  • I'm having trouble coming up with a reason to do this. What do you want to use this information for? – Kevin May 13 '22 at 15:30
  • @Kevin: I thought of using something like this in some macro that gets used in unit testing a library which involves exposure of things via shared objects. I can't say I have a compelling use case. But you know what? I'll expand the scope slightly. – einpoklum May 13 '22 at 17:25
  • The only thing that extern C changes is the name of the internal symbol used for the function. And a "extern C" function can be C++ code and can access your shared objects just fine. – Goswin von Brederlow May 13 '22 at 17:29
  • @GoswinvonBrederlow: You are mistaken in your first sentence. Language linkage is [part of the function type](https://stackoverflow.com/a/26647735/1593077). The second sentence is true, but not what I'm asking about here. – einpoklum May 13 '22 at 17:55
  • @einpoklum Ok, it changes the type, which is how the compiler knows how the symbol for the function is named. My point was that just because something is `extern "C"` doesn't mean it isn't C++ code. It's required for cross-language calls but is not sufficient to detect cross-language calls (even if you could detect `extern "C"` declarations). – Goswin von Brederlow May 13 '22 at 18:36
  • @GoswinvonBrederlow: Yes, I realize that. – einpoklum May 13 '22 at 22:17

0 Answers0