问题描述
我正在设计一个用于C ++库的API,它将分布在dll / shared对象中。库包含具有虚函数的多态类。我担心,如果我公开这些虚拟函数的DLL API,我削减了扩展相同的类与更多的虚拟函数的可能性,而不破坏与为以前的版本的库的应用程序的二进制兼容性。
I am designing an API for a C++ library which will be distributed in a dll / shared object. The library contains polymorhic classes with virtual functions. I am concerned that if I expose these virtual functions on the DLL API, I cut myself from the possibility of extending the same classes with more virtual functions without breaking binary compatibility with applications built for the previous version of the library.
一个选择是使用成语隐藏所有具有虚拟函数,但这似乎也有它的局限性:这种方式应用程序失去子类的类和覆盖虚拟方法的可能性。
One option would be to use the PImpl idiom to hide all the classes having virtual functions, but that also seem to have it's limitations: this way applications lose the possibility of subclassing the classes of the library and overriding the virtual methods.
如何设计一个API类,可以在应用程序中进行子类化,而不会失去在新版本的dll中使用(非抽象)虚拟方法扩展API的可能性,同时保持向后兼容的二进制?
How would you design a API class which can be subclassed in an application, without losing the possibility to extend the API with (not abstract) virtual methods in a new version of the dll while staying backward binary compatible?
更新:库的目标平台是windows / msvc和linux / gcc。
Update: the target platforms for the library are windows/msvc and linux/gcc.
推荐答案
一篇名为在GNU / Linux系统上用C ++实现的共享库的二进制兼容性的文章[]。虽然在Windows系统上的概念是相似的,我敢肯定,他们是不一样的。但是阅读这篇文章后,你可以得到一个关于C ++二进制级别与兼容性有什么关系的观点。
Several months ago I wrote an article called "Binary Compatibility of Shared Libraries Implemented in C++ on GNU/Linux Systems" [pdf]. While concepts are similar on Windows system, I'm sure they're not exactly the same. But having read the article you can get a notion on what's going on at C++ binary level that has anything to do with compatibility.
顺便说一下,GCC应用二进制接口在标准文档草案中进行了总结,因此您将有一个正式的理由您选择的编码标准。
By the way, GCC application binary interface is summarized in a standard document draft "Itanium ABI", so you'll have a formal ground for a coding standard you choose.
只是一个快速示例:在GCC中,如果没有其他类继承它,您可以扩展一个具有更多虚函数的类。阅读文章更好的规则集。
Just for a quick example: in GCC you can extend a class with more virtual functions, if no other class inherits it. Read the article for better set of rules.
但无论如何,规则有时太复杂,难以理解。因此,您可能对验证两个给定版本的兼容性的工具感兴趣: for Linux 。
But anyway, rules are sometimes way too complex to understand. So you might be interested in a tool that verifies compatibility of two given versions: abi-compliance-checker for Linux.
这篇关于如何设计二进制兼容可扩展性的C ++ API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!