简体   繁体   English

请解释C++ ABI

[英]Please explain the C++ ABI

The common explanation for not fixing some issues with C++ is that it would break the ABI and require recompilation, but on the other hand I encounter statements like this:不解决 C++ 的一些问题的常见解释是它会破坏 ABI 并需要重新编译,但另一方面我遇到这样的语句:

Honestly, this is true for pretty much all C++ non-POD types, not just exceptions.老实说,几乎所有 C++ 非 POD 类型都是如此,而不仅仅是例外。 It is possible to use C++ objects across library boundaries but generally only so long as all of the code is compiled and linked using the same tools and standard libraries.可以跨库边界使用 C++ 对象,但通常只要使用相同的工具和标准库编译和链接所有代码即可。 This is why, for example, there are boost binaries for all of the major versions of MSVC.例如,这就是为什么所有主要版本的 MSVC 都有 boost 二进制文件的原因。

(from this SO answer ) (来自这个 SO 答案

So does C++ have a stable ABI or not?那么 C++ 是否有稳定的 ABI?

If it does, can I mix and match executables and libraries compiled with different toolsets on the same platform (for example VC++ and GCC on Windows)?如果是这样,我是否可以在同一平台上混合和匹配使用不同工具集编译的可执行文件和库(例如 Windows 上的 VC++ 和 GCC)? And if it does not, is there any way to do that?如果没有,有没有办法做到这一点?

And more importantly, if there is no stable ABI in C++, why are people so concerned about breaking it?更重要的是,如果 C++ 中没有稳定的 ABI,为什么人们这么关心破解它呢?

Although the C++ Standard doesn't prescribe any ABI, some actual implementations try hard to preserve ABI compatibility between versions of the toolchain.尽管 C++ 标准没有规定任何 ABI,但一些实际实现努力保持工具链版本之间的 ABI 兼容性。 Eg with GCC 4.x, it was possible to use a library linked against an older version of libstdc++ , from a program that's compiled by a newer toolchain with a newer libstdc++ .例如,在 GCC 4.x 中,可以使用与旧版本libstdc++链接的库,该库是由具有较新libstdc++的较新工具链编译的程序。 The older versions of the symbols expected by the library are provided by the newer libstdc++.so , and layouts of the classes defined in the C++ Standard Library are the same.库所期望的旧版本符号由较新的libstdc++.so提供,并且在 C++ 标准库中定义的类的布局是相同的。

But when C++11 introduced the new requirements to std::string and std::list , these couldn't be implemented in libstdc++ without changing the layout of these classes.但是当 C++11 向std::stringstd::list引入新要求时,如果不更改这些类的布局,就无法在libstdc++中实现这些要求。 This means that, if you don't use the _GLIBCXX_USE_CXX11_ABI=0 kludge with GCC 5 and higher, you can't pass eg std::string objects between a GCC4-compiled library and a GCC5-compiled program.这意味着,如果您不将_GLIBCXX_USE_CXX11_ABI=0与 GCC 5 及更高版本一起使用,则无法在 GCC4 编译的库和 GCC5 编译的程序之间传递例如std::string对象。 So the ABI was broken.所以ABI坏了。

Some C++ implementations don't try that hard to have compatible ABI: eg MSVC++ doesn't provide such compatibility between major compiler releases (see this question ), so one has to provide different versions of library to use with different versions of MSVC++.一些 C++ 实现并没有那么努力地拥有兼容的 ABI:例如 MSVC++ 不提供主要编译器版本之间的这种兼容性(请参阅这个问题),因此必须提供不同版本的库以与不同版本的 MSVC++ 一起使用。

So, in general, you can't mix and match libraries and executables compiled with different versions even of the same toolchain.所以,一般来说,你不能混合和匹配使用不同版本编译的库和可执行文件,即使是同一个工具链。

C++ does not have an ABI standard as of yet. C++ 目前还没有 ABI 标准。 They are attempts to have it in the standard;他们试图将其纳入标准; You can read following it explains it in details:您可以阅读以下内容,详细说明:

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2028r0.pdf http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2020/p2028r0.pdf

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

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