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

__cxa_demangle fails on rhel6 (centos6) with devtoolset-4 gcc-5.2

本文关键字:rhel6 centos6 失败 gcc-5 deangle cxa devtoolset-4      更新时间:2023-10-16

我尝试了一个最小的测试用例。这个案例通过了devtoolset-4(gcc-5.2)中的rhel-7,但在rhel-6中失败了。状态-2表示"mangled_name不是C++ABI mangling规则下的有效名称。"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 demanglen";
        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>
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);