gcc编译C++ 符号名解码 c++filt demangle

本文介绍了gcc如何为C++函数重命名以避免多态冲突,并通过c++filt工具展示解码函数名的过程。同时,展示了如何使用abi::__cxa_demangle函数进行函数名还原,以及一个实用的demangle函数示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

gcc 编译c++函数会对函数名重新命名保证重载等多态不会重名,

所以在查看时候很不方便,我们可以使用工具c++filt,

比如我们有一个函数名:

./filt _Z5test1PKc

解出来是这样的

test1(char const*)

我们调用函数也可以实现:

#include <cxxabi.h>

bool demangle(string s, string& res) {
    int status;
    char* name = abi::__cxa_demangle(s.c_str(), nullptr, 0, &status);
    if (status != 0) {
        string err;
        switch(status) {
        case -1: res = "memory allocation error"; break;
        case -2: res = "invalid name given"; break;
        case -3: res = "internal error: __cxa_demangle: invalid argument"; break;
        default: res = "unknown error occured"; break;
        }
        return false;
    }
    res = name;
    free(name);
    return true;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值