Name mangling of c++ classes and its member functions?

后端 未结 4 1099
失恋的感觉
失恋的感觉 2021-01-21 01:15

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 ?

相关标签:
4条回答
  • 2021-01-21 01:54

    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.

    0 讨论(0)
  • 2021-01-21 01:55

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

    0 讨论(0)
  • 2021-01-21 02:00

    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.

    0 讨论(0)
  • 2021-01-21 02:02

    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.

    0 讨论(0)
提交回复
热议问题