c++ - 如果没有公开任何标准符号,则让最近的 C++ 编译 .so 在旧系统上运行

标签 c++ linux gcc shared-libraries abi

我的目标是使用最新的 C++ 功能,如智能指针(至少 std::shared_ptr)用于 linux C++ so 库,它应该在多个发行版上运行(显然为每个平台重新编译它)。我定位的最旧的 gcc 是 4.4.x since I need to support RHEL6 .看起来像

这样的简单程序支持智能指针
#include <iostream>
#include <memory>

int main()
{
    std::shared_ptr<int> aa;
    return 0;
}

我的库将公开一个 C API (extern),我不在 API 中使用异常或 C++ 类型。我是 linking statically both libstdc++ and libgcc and marking all symbols local except the API exported ones .

我现在有另一个问题:在上述情况下,使用较新的 gcc(例如 5.0 甚至 6.0)编译的库是否可以在默认安装 gcc 4.4.x 的旧系统(如 RHEL6)上可靠地?

理论上,使用更新的 C++ 功能但不在符号或 API 中公开任何功能应该可行,但我不太确定,我宁愿问。

最佳答案

是的。

实际上,Red Hat posted a blog item关于你的情况:

Users that depend on third-party libraries or plugin interfaces that still use the old ABI can build their code with -D_GLIBCXX_USE_CXX11_ABI=0 and everything should work fine. In most cases, it will be obvious when this flag is needed because of errors from the linker complaining about unresolved symbols involving __cxx11.

[The] plan for [the C++11] ABI change has been to leave the soname (and the existing binary interface) alone, and express the new ABI using different mangled names.

由于您使用 libstdc++ 进行静态编译,因此所有应用程序都将可靠地运行,或者无法链接。发生这种情况时,更改编译标志以添加 _GLIBCXX_USE_CXX11_ABI=0


总而言之,您不应该关心 API 的变化,而应该只关心 ABI 的变化。而这一次,GNU 团队做对了!

关于c++ - 如果没有公开任何标准符号,则让最近的 C++ 编译 .so 在旧系统上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51359318/

相关文章:

arrays - 使用 jq 根据对象中的键值从数组返回整个对象

c++ - 编译一个相当简单的c++ 11程序时gcc和clang之间的不同结果

gcc - 在 gcc 中禁用除少数警告之外的所有警告

c++ - 如何将深度图像复制到彩色图像?

linux - 什么是 hrtick_clear(rq);在 linux 调度程序中?

c++ - #if/#endif 预处理指令

Linux:最强大的调试器

gcc - __builtin_expect 来自 GCC 的概率

c++ - 将问题与 libharu 联系起来

c++ - 如何换出内存块?