标准库 ABI 兼容性 [英] Standard library ABI compatibility

查看:58
本文介绍了标准库 ABI 兼容性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有一个共享库,它接受或返回某种标准类:

Suppose we have a shared library which accepts or returns some kind of std class:

//lib.h
#include <vector>

std::vector<int> returnSomeInts();

//lib.cpp
#include "lib.cpp"

std::vector<int> returnSomeInts() {
   return {1, 3, 5};
}

因此,很明显,在编译共享库 lib.so 时,必须针对特定版本的标准库编译此代码,例如使用 -std=c++11.

So, obviously, when compiling the shared library lib.so, this code had to be compiled against a particular version of the standard library, for instance with -std=c++11.

现在假设我们有一个应用程序将使用我们的共享库,但它将针对更新的 std 库进行编译,例如 -std=c++2a

Now imagine we have an application which will be using our shared library, but it will be compiled against a newer std library, for example -std=c++2a

//app.cpp
#include <lib.h>

int main()
   auto v = returnSomeInts();

   //Process v
}

由于标准库定义了内联类,如果类成员的布局发生变化,ABI兼容性就会被破坏,所以上面的代码将无法正常工作.

As the standard library defines inline classes, if the layout of the class members changes, ABI compatibility gets broken, so the code above would not work properly.

我的问题是:当使用不同的 c++ 标准针对相同的头文件进行编译时,标准库的通用实现是否可以保证 ABI 的稳定性?当针对不同的头文件版本(例如 libstdc++-8 和 libstdc++-9)进行编译时?

My questions are: Is there any guarantee for ABI stability with the common implementations of the the std library when compiling against the same header using different c++ standards? And when compiling against different header versions (libstdc++-8 and libstdc++-9 for example)?

PD:上面的代码只是一个例子,我并不是特指std::vector

PD: The code above is just an example, I am not referring specifically to std::vector

推荐答案

实践中的 ABI 没有与标准挂钩,例如考虑以下代码 用 gcc 4.9.4 和 gcc 5.1 编译使用相同的标志:

ABIs in practice are not linked to the standard, for example consider this following code compiled with gcc 4.9.4 and gcc 5.1 using the same flags:

-std=c++11 -O2

-std=c++11 -O2

#include <string>
int main(){
    return sizeof (std::string);
}

gcc 4.9.4 从 main 返回 8,gcc 5.1 返回 32.

gcc 4.9.4 returns 8 from main, gcc 5.1 returns 32.

至于保证:很复杂:

标准没有任何保证.

实际上MSVC用来破坏ABI兼容性,他们停止了(v140,v141,v142使用相同的ABI),clang/gcc有一个稳定的ABI很长一段时间.

Practically MSVC used to break ABI compatability, they stopped (v140,v141,v142 use the same ABI), clang/gcc have a stable ABI for a long time.

对于有兴趣了解更多信息的人:有关与此问题不直接相关的 ABI/C++ 标准的广泛讨论,请查看此 博客 发布.

For those interested in learning more: For a broad discussion of ABI/C++ standard that is not directly related to this question you an look at this blog post.

这篇关于标准库 ABI 兼容性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆