纯基类需要从DLL导出吗?
我有两个 DLL:a.dll
和 b.dll
,并且在每个 DLL 中我都有一个类 AClass
和 BClass
>.
我想让 AClass
和 BClass
继承并实现相同的接口 AbsBase
,这是一个纯抽象类。
在每个类中,我为 __declspec(dllimport)
和 __declspect(dllexport)
设置了 #defines
。当我尝试编译时,我得到这个:
警告 C4275:非 dll 接口类“AClass”用作 dll 接口类“AbsBase”的基础
它基本上希望我将 AbsBase
声明为 __declspec(dllexport)
但如果编译器按照自己的方式行事,我就必须声明从 a.dll
和 b.dll> 导出
AbsBase
。
为什么类的接口需要导出?
有什么办法解决吗? 我真的应该从两个 DLL 中导出 AbsBase
吗?难道这有什么本质上的错误吗? (我需要定义一个新的 XXX_EXPORT 宏..)
I have two DLLs: a.dll
and b.dll
and in each one I have one class AClass
and BClass
.
I would like to have both AClass
and BClass
inherit and implement the same interface AbsBase
which is a pure abstract class.
In each class I set up the #defines
for __declspec(dllimport)
and __declspect(dllexport)
. When I'm trying to compile I get this:
warning C4275: non dll-interface class 'AClass' used as base for dll-interface class 'AbsBase'
which basically wants me to declare AbsBase
as __declspec(dllexport)
But if the compiler would have it his way, I would have to declare AbsBase
to be exported from both a.dll
and b.dll
.
Why does the interface of a class needs to be exported?
Is there any way around it?
Should I really export AbsBase
from both DLLs? isn't there something inherently wrong with this? (I would need to define a new XXX_EXPORT macro..)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它看起来像是编译器警告而不是错误,所以它应该仍然有效。编译器只是让你知道你正在做一些很容易搞砸的事情。只要 DLL 和核心程序都同意基类的定义,这样做应该是完全可以接受的。
您应该能够使用编译指示来抑制警告:
http:// /forums.devx.com/archive/index.php/t-84785.html
It looks like its a compiler warning and not an error, so it should still work. The compiler is just letting you know that you are doing something that makes it easy for you to screw up. It should be perfectly acceptable to do this as long as both DLLs and the core program agree on the definition of the base class.
You should be able to use a pragma to supress the warning:
http://forums.devx.com/archive/index.php/t-84785.html
这是值得烦恼的事情。编译器检测到基类中的代码可以运行。它不会是纯粹的虚拟方法,它知道如何过滤这些方法。也许是构造函数或析构函数?失败模式是客户端代码与 DLL 中类对象的内存布局可能不同。这导致的运行时混乱非常很难诊断。
只要您能保证客户端和 DLL 都是使用完全相同的编译和链接设置、使用完全相同版本的 CRT 和这些工具来编译的,那就没问题。您可以通过使用非标准 __interface 关键字而不是类来使基类保证抽象。
This is something to fret about. The compiler has detected that code in the base class may run. It won't be pure a virtual method, it knows how to filter those. Maybe a constructor or a destructor? The failure mode is that the memory layout of the class object might not be the same in the client code vs the DLL. The runtime mayhem this causes is very hard to diagnose.
You'll be okay if as long as you can guarantee that both the client and the DLL are compiled with the exact same compile and link settings, using the exact same versions of the CRT and those tools. You can make the base class guaranteed abstract by using the non-standard __interface keyword instead of class.
我有一个提示:
虚方法表中f和g的顺序是在基类中指定的,这个信息是非常需要的。
I have a tip:
The order of f and g in virtual method table is specified in the base class and this information is very needed.