以可移植的方式将类导出到 dll,而不使用接口?

发布于 2024-11-08 13:54:29 字数 581 浏览 0 评论 0原文

我有一个关于共享库/.dll 和类导入/导出的问题。经过一番研究后,我了解到有两种方法:

  • 只需在类之前使用 __declspec(dllexport/dllimport) 并祈祷不同的编译器版本以相同的方式破坏名称,并接受您的 .dll 将无法被不同编译器使用的事实。
  • 使用dll中的纯虚拟类作为接口。然后所有需要导出的类都将从它们继承并应该实现虚函数。在这种情况下,.dll 将导出的只是“工厂”构造/销毁函数,用于创建和释放对象。
  • 这是我所知道的仅有的两种方法。第一个是禁忌,因为它提供了 0 可移植性。第二个虽然对于实现目的的 .dll 来说是方便且良好的编程设计,但当您意识到需要为每个不同的构造函数有不同的构造函数时,它开始变得烦人,但只能使用 POD 类型作为参数,并且您会丢失许多C++ 类的优点,例如重载函数和默认函数参数。

    对于应该向用户提供库的 .dll,例如类的集合,即使是第二种方式也变得非常不方便。所以我想知道这里的解决方案是什么?只是用第一种方法为每个主要编译器编译一个不同的.dll?大库的共享库版本如何工作?例如,wxWidgets 也提供了 .dll 版本。他们如何实现 .dll 的最终用户对类的正常使用,从而避免接口解决方案?

    I have a question about shared libraries/.dlls and class importing/exporting. From what I have understood after some research the two ways to do it is as follows:

  • Just using __declspec(dllexport/dllimport) before the class and praying that the different compiler versions mangle the names in the same way, and accept the fact that your .dll will not be useable by different compilers.
  • Use pure virtual classes in the dll as interfaces. Then all the classes that would need exporting would inherit from them and should implement the virtual functions. What the .dll would export in this case is just "factory" Construct/Destruct functions which would create and release the objects.
  • These are the only two ways I know. The first is a no-no since it offers 0 portability. The second, though convenient and a good programming design for a .dll that accomplishes a purpose starts to be annoying when you realize that you need to have a different constructor function for every different constructor, can only use POD types as parameters and you lose many advantages of C++ classes such as overloaded functions and default function arguments.

    For a .dll that is supposed to offer a library to the user, a collection of classes for example even the second way becomes really inconvenient. So I am wondering what is the solution here? Just compile a different .dll with the first way for each major compiler? How do the shared libraries version of big libraries work? For example wxWidgets does offer a .dll version too. How do they achieve normal usage of classes by the end user of the .dll avoiding the interface solution?

    如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

    扫码二维码加入Web技术交流群

    发布评论

    需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

    评论(1

    德意的啸 2024-11-15 13:54:29

    为每个编译器版本提供单独的 DLL 的解决方案是有效的。同时它仍然不是官方功能。你永远不会知道下一个服务包是否会破坏兼容性,没有人会给你维持/破坏兼容性的编译器/链接器密钥的准确列表,等等。我听到可靠的传言说Windows8最终以正确的方式实现了这一点。

    顺便说一下,仍然可以从以下位置下载 Visual Studio 2012 Release Candidate:http://msdn .microsoft.com/en-us/vstudio/bb984878.aspx。也许你应该尝试一下?

    Solution with having separate DLL for each compiler version works. At the same time it is still not an official feature. You will never know if next service pack will break the compatibility or not, nobody will give you exact list of compiler/linker keys that will maintain/break the compatibility, etc. I heard reliable rumors that Windows8 finally implemented this in the right way.

    By the way Visual Studio 2012 Release Candidate can be still downloaded from: http://msdn.microsoft.com/en-us/vstudio/bb984878.aspx. Maybe you should try it?

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文