__cxa_demangle 在 rhel6 (centos6) 上使用 devtoolset-4 gcc-5.2 失败

Posted

技术标签:

【中文标题】__cxa_demangle 在 rhel6 (centos6) 上使用 devtoolset-4 gcc-5.2 失败【英文标题】:__cxa_demangle fails on rhel6 (centos6) with devtoolset-4 gcc-5.2 【发布时间】:2016-04-03 23:43:21 【问题描述】:

我已经尝试过最小的测试用例。这个案例通过 devtoolset-4 (gcc-5.2) 在 rhel-7 上通过,在 rhel-6 下失败。状态 -2 表示“mangled_name 不是 C++ ABI 重整规则下的有效名称。” https://gcc.gnu.org/onlinedocs/libstdc++/libstdc++-html-USERS-4.3/a01696.html

如果我想在 RHEL-6 上使用 gcc-5.2,我是不是做错了,或者这是我必须以某种方式解决的错误?如果是这样,有什么建议吗?到目前为止,我最好的想法是将重整名称中的“IJ”正则表达式为“II”,然后再将其提供给 __cxa_demangle()。如果这可能是相对可靠的,我可以接受它。

#include <iostream>
#include <string>
#include <tuple>
#include <typeinfo>
#include <cxxabi.h>

#define DUMP(x) std::cout << #x << " is " << x << std::endl

template<typename T>
void print_type(T t)

    int status;
    auto name = typeid(t).name();
    DUMP(name);
    auto thing2 = abi::__cxa_demangle(name, 0, 0, &status);
    if(status == 0)
    
        DUMP(thing2);
    
    else
    
        std::cout << typeid(t).name() << " failed to demangle\n";
        DUMP(status);
    


typedef std::tuple<foo_t, foo_t> b_t;

int main(int argc, char **argv)

    std::tuple<bool, bool> t;
    print_type(t);

Centos-6 输出

name is St5tupleIJbbEE
St5tupleIJbbEE failed to demangle
status is -2

Centos-7 输出

name is St5tupleIJbbEE
thing2 is std::tuple<bool, bool>

带有 devtoolset-3 (gcc-4.9) 的 Centos-6 输出

name is St5tupleIIbbEE
thing2 is std::tuple<bool, bool>

【问题讨论】:

gcc 4 和 5 之间没有 ABI 变化吗?我不是专家,但我记得我的同事说过。 【参考方案1】:
std::string typestring(typeid(T).name());
auto pos = typestring.find("IJ");
if(pos != std::string::npos)

    // gcc-5.2 uses IJ in typestring where
    // gcc-4.9 used II - this fugly hack makes
    // cxa_demangle work on centos/rhel-6 with gcc-5.2
    // eg std::tuple<bool, bool> 
    typestring[pos + 1] = 'I';

rv = abi::__cxa_demangle(typestring.c_str(), 0, 0, &status);

【讨论】:

以上是关于__cxa_demangle 在 rhel6 (centos6) 上使用 devtoolset-4 gcc-5.2 失败的主要内容,如果未能解决你的问题,请参考以下文章

C++封装一个易用的打印backtrace信息的函数

_GLIBCXX_USE_CXX11_ABI 在 RHEL6 和 RHEL7 上禁用?

RHEL6.5安装Oracle RAC 11g(一,系统配置)

RHEL6.5安装Oracle RAC 11g( 三,集群安装配置)

RHEL6.5安装Oracle RAC 11g( 四,数据库安装)

RHEL6.6安装Oracle 11g RAC - 基于VMware的实验环境