Is there no way I could avoid name mangling of C++ classes and its member functions when exposed from a c++ dll.
Can\'t I use a def file mechanism in this regard ?
I think the best way to do this is to provide C wrappers around the C++ library. This was quite popular 10 or more years back when I was programming in C++ but I don't know if it is done any more.
Basically, for every class C
, for every constructor ctor
to be exposed to create an extern "C" CPtr cCtor(....)
method that returns an opaque pointer CPtr
and for every function func
to be exposed you create extern "C" cFunc(CPtr,....)
Another approach is to create a CStruct
that has member variables of function pointer types, implement them to call the class methods and let the client do all the hard work.
The only way I could think of is declaring functions as extern "C". The name mangling is required for the linker to distinguish e.g. overloaded functions by their parameter list (which would be unavailable to the linker if not for name mangling).
I don't believe so. Name mangling is used so that each overloaded function has a different name as viewed by the linker.
You could rewrite them in C and use the extern "C" {}
construct but then you lose all your beautiful inheritance, polymorphism and so forth. You could also wrap the C++ inside C functions and expose only the C functions.
Virtual function would be the answer I think, have you noticed COM, create a instance, it gives you a pointer to that interface represented as class. I guess that methods declared are not resolved by name, but by slot index.