5

Is this legal to do? I want to export a C function, but internally that function will use a C++ class.

extern "C" BOOL /*BOOL==int*/ Func()
{
   return someclass::getinstance()->Func(); // this is just bool tho
}
Harry
  • 179
  • 8
  • 2
    I don't know which document(s) would offer official guidance, but I see this done all the time. – NPE Oct 26 '14 at 15:03

1 Answers1

5

This is perfectly legitimate. The purpose of extern "C" is to prevent Func() from getting its name mangled (decorated with type information) so that a C module can link to it using its plain name. C++ mangles names so that functions with the same name but different parameter lists can be resolved (function overloading).

Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • +1 Exactly, this is a very common practice. While neither are standardized (AFAIK), the C name mangling scheme is generally [consistent](http://en.wikipedia.org/wiki/Name_mangling#C_name_decoration_in_Microsoft_Windows) while C++ is all over the place. So functions exported from both static and shared libraries are generally written as `extern "C"` to use the C scheme and allow for greater interoperability between compilers (and even between different versions of the same compiler) when linking to compiled libraries. The rest of that wiki article contains good explanations. – Jason C Oct 26 '14 at 15:45