我来自 Linux 世界,了解很多关于维护用 C++ 语言编写的动态库 API 的向后二进制兼容性 (BC) 的文章。其中之一是“C++ 的策略/二进制兼容性问题”,基于 Itanium C++ ABI,由 GCC 编译器使用。但我找不到 Microsoft C++ 编译器(来自 MSVC)的任何类似内容。
我知道大多数技术都适用于 MS C++ 编译器,并且我想发现与 ABI 差异相关的编译器特定问题(v 表布局、重整等),
所以,我的问题以下是:
- 您知道 MS C++ 和 GCC 编译器在维护 BC 时有什么区别吗?
- 在哪里可以找到有关 MS C++ ABI 或有关在 Windows 中维护 API BC 的信息?
任何相关信息将不胜感激。
非常感谢您的帮助!
I came from the Linux world and know a lot of articles about maintaining backwards binary compatibility (BC) of a dynamic library API written in C++ language. One of them is "Policies/Binary Compatibility Issues With C++" based on the Itanium C++ ABI, which is used by the GCC compiler. But I can't find anything similar for the Microsoft C++ compiler (from MSVC).
I understand that most of the techniques are applicable to the MS C++ compiler and I would like to discover compiler-specific issues related to ABI differences (v-table layout, mangling, etc.)
So, my questions are the following:
- Do you know any differences between MS C++ and GCC compilers when maintaining BC?
- Where can I find information about MS C++ ABI or about maintaining BC of API in Windows?
Any related information will be highly appreciated.
Thanks a lot for your help!
发布评论
评论(3)
首先,这些政策是一般性的,不仅仅适用于海湾合作委员会。例如:函数中的私有/公共标记是 MSVC 特有的,而不是 gcc 特有的。
所以基本上这些规则也完全适用于MSVC和通用编译器。
但是...
您应该记住:
所以,是的,您可以使用这些规则,但与 MSVC 的通常情况一样,它有更多的怪癖。
另请参阅“关于二进制兼容性的一些想法”并且 Qt 也使它们的 ABI 与 MSVC 保持稳定。
请注意,我在这方面有一些经验,因为我遵循 CppCMS
First of all these policies are general and not refer to gcc only. For example: private/public mark in functions is something specific to MSVC and not gcc.
So basically these rules are fully applicable to MSVC and general compiler as well.
But...
You should remember:
So yes you can use these rules, but as in usual case of MSVC it has much more quirks.
See also "Some thoughts on binary compatibility" and Qt keeps they ABI stable with MSVC as well.
Note I have some experience with this as I follow these rules in CppCMS
在 Windows 上,您基本上有 2 个选项来实现长期二进制兼容性:
在这里查看我的帖子。在那里,您将看到一种跨不同编译器和编译器版本以二进制兼容方式创建 DLL 和访问 DLL 的方法。
C++ DLL 插件接口
On Windows, you basically have 2 options for long term binary compatibility:
Check out my post here. There you'll see a way to create DLLs and access DLLs in a binary compatible way across different compilers and compiler versions.
C++ DLL plugin interface
MSVC 二进制兼容性的最佳规则是使用 C 接口。根据我的经验,唯一可以使用的 C++ 功能是单继承接口。因此,将所有内容表示为使用 C 数据类型的接口。
下面列出了一些不二进制兼容的东西:
new
/malloc
并在另一个模块中delete
/free
。有不同的堆,彼此不了解。 STL 不能跨模块工作的另一个原因。简而言之,C++ 没有一致的 ABI,但 C 有,因此请避免 C++ 功能跨模块。由于单一继承是一个简单的 v 表,因此您可以有效地使用它来公开 C++ 对象,前提是它们使用 C 数据类型并且不进行跨堆分配。这也是 Microsoft 自己使用的方法,例如 Direct3D API。 GCC 在提供稳定的 ABI 方面可能很有用,但标准并不要求这样做,而 MSVC 利用了这种灵活性。
The best rule for MSVC binary compatibility is use a C interface. The only C++ feature you can get away with, in my experience, is single-inheritance interfaces. So represent everything as interfaces which use C datatypes.
Here's a list of things which are not binary compatible:
new
/malloc
in one module anddelete
/free
in another. There are different heaps which do not know about each other. Another reason the STL won't work cross-modules.In short, C++ has no consistent ABI, but C does, so avoid C++ features crossing modules. Because single inheritance is a simple v-table, you can usefully use it to expose C++ objects, providing they use C datatypes and don't make cross-heap allocations. This is the approach used by Microsoft themselves as well, e.g. for the Direct3D API. GCC may be useful in providing a stable ABI, but the standard does not require this, and MSVC takes advantage of this flexibility.