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

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

问题描述

假设我们有一个共享库,可以接受或返回某种std类:

 //lib.h#include< vector>std :: vector< int>returnSomeInts();//lib.cpp#include"lib.cpp"std :: vector< int>returnSomeInts(){返回{1,3,5};} 

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

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

 //app.cpp#include< lib.h>int main()自动v = returnSomeInts();//处理v} 

由于标准库定义了内联类,因此如果类成员的布局发生更改,则ABI兼容性将被破坏,因此上面的代码将无法正常工作.

我的问题是:使用不同的c ++标准针对同一标头进行编译时,std库的常见实现方式能否保证ABI稳定性?而针对不同的标头版本(例如libstdc ++-8和libstdc ++-9)进行编译时?

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

解决方案

实践中的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};
}

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.

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
}

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.

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: The code above is just an example, I am not referring specifically to std::vector

解决方案

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

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

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

As for guarantees: it is complicated:

Nothing is guaranteed by the standard.

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.

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天全站免登陆