简体   繁体   English

C++ 类及其成员函数的名称重整?

[英]Name mangling of c++ classes and its member functions?

Is there no way I could avoid name mangling of C++ classes and its member functions when exposed from a c++ dll.当从 C++ dll 公开时,我有没有办法避免 C++ 类及其成员函数的名称修改。

Can't I use a def file mechanism in this regard ?我不能在这方面使用 def 文件机制吗?

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.你可以用 C 重写它们并使用extern "C" {}构造,但是你会失去所有美丽的继承、多态等等。 You could also wrap the C++ inside C functions and expose only the C functions.您还可以将 C++ 包装在 C 函数中并仅公开 C 函数。

I think the best way to do this is to provide C wrappers around the C++ library.我认为最好的方法是在 C++ 库周围提供 C 包装器。 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. 10 年前,当我用 C++ 编程时,这非常流行,但我不知道它是否已经完成。

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,....)基本上,对于每个class C ,对于每个要公开的构造函数ctor以创建一个extern "C" CPtr cCtor(....)方法,该方法返回一个不透明的指针CPtr并且对于要公开的每个函数func你创建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.另一种方法是创建一个具有函数指针类型成员变量的CStruct ,实现它们以调用类方法,并让客户端完成所有繁重的工作。

The only way I could think of is declaring functions as extern "C".我能想到的唯一方法是将函数声明为 extern "C"。 The name mangling is required for the linker to distinguish eg overloaded functions by their parameter list (which would be unavailable to the linker if not for name mangling).链接器需要名称修改来区分例如重载函数的参数列表(如果不是名称修改,链接器将无法使用)。

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.虚拟函数将是我认为的答案,您是否注意到 COM,创建一个实例,它为您提供一个指向表示为类的接口的指针。 I guess that methods declared are not resolved by name, but by slot index.我猜声明的方法不是按名称解析的,而是按插槽索引解析的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM