2020-12-14 14:07:53
最近又碰到一个问题,明明在cmake中添加了依赖的库,但库中某些函数能链接上,某些找不到符号,后面才发现是因为 cmake中没写库路径的情况下,链接了/usr/lib中的同名的依赖库,而/usr/lib中的库是早期版本,所有有些函数没有,自然找不到符号定义。两种解决方法:
1 删除/usr/lib中的老版本,避免混淆,同时cmake中加上 动态库路径
2 直接用新版本的依赖库替换/usr/lib中的库

2018年6月20日15:42:38
记录下 最近发现的一个问题
说有两个函数的定义找不到,初步用 nm 看了下, 导出了这两个函数的,
用 nm -C xxx.so 看了下, 发现函数签名中 参数 的类型不太一样, 一个是 c++0x ::basic_string之类的,
但找不到那个错误 报出的 是 std::string
原来 使用 xxx.so 的代码是用 C++11 编译的, 但 导出函数的xxx.so库是 C++0x编译的, 两个不统一,
导致 string 被修饰得不一样,所以发生 未定义错误!
使用 C++11 编译后就好了!

比如 boost库

./bootstrap.sh --with-toolset=gcc --prefix=/usr/local./b2 -j12 toolset=gcc variant=release link=shared threading=multi address-model=64 cxxflags=-std=c++11 install

首先这篇文章总结得不错

https://blog.csdn.net/stpeace/article/details/73302833

然后依然会碰到问题,这时候就要看具体的函数签名了
Name Mangling in C++

参考
以下内容来自:
http://blog.51cto.com/hipercomer/855223

nm工具的 --demangle 选项 可以让函数名可读

name demangling
C++的name mangling技术一般使得函数变得面目全非,而很多情况下我们在查看这些符号的时候并不需要看到这些函数name mangling之后的效果,而是想看看是否定义了某个函数,或者是否引用了某个函数,这对于我们调试程序是非常有帮助的。
所以需要一种方法从name mangling之后的符号变换为name mangling之前的符号,这个过程称之为name demangling.事实上有很多工具提供这些功能,最常用的就是c++filt命令,c++filt命令接受一个name mangling之后的符号作为输入并输出demangling之后的符号。例如:

[lichao@sg01 name_mangling]$ c++filt _Z9test_funcRiPKcdSsf
test_func(int&, char const*, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float)

一般更常用的方法为:

[lichao@sg01 name_mangling]$ nm func.o | c++filt 0000000000000060 t global constructors keyed to _Z9test_funcRiPKcdSsf U _Unwind_Resume
0000000000000022 t __static_initialization_and_destruction_0(int, int) 0000000000000000 T test_func(int&, char const*, double, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, float) U std::allocator<char>::allocator() U std::allocator<char>::~allocator() U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() U std::ios_base::Init::Init() U std::ios_base::Init::~Init()
0000000000000000 b std::__ioinit U __cxa_atexit U __dso_handle U __gxx_personality_v0
0000000000000076 t __tcf_0 000000000000008e T main
另外使用nm命令也可以demangle符号,使用选项-C即可,例如:[lichao@sg01 name_mangling]$ nm -C func.o 0000000000000060 t global constructors keyed to _Z9test_funcRiPKcdSsf U _Unwind_Resume
0000000000000022 t __static_initialization_and_destruction_0(int, int) 0000000000000000 T test_func(int&, char const*, double, std::string, float) U std::allocator<char>::allocator() U std::allocator<char>::~allocator() U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(char const*, std::allocator<char> const&) U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(std::string const&) U std::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string() U std::ios_base::Init::Init() U std::ios_base::Init::~Init()
0000000000000000 b std::__ioinit U __cxa_atexit U __dso_handle U __gxx_personality_v0
0000000000000076 t __tcf_0 000000000000008e T main

又到了Last but not least important的时候了,还有一个特别重要的接口函数就是__cxa_demangle(),此函数的原型为:

namespace abi {
extern "C" char* __cxa_demangle (const char* mangled_name, char* buf,
size_t* n,
int* status);
}

用于将mangled_name所指向的mangled进行demangle并将结果存放在buf中,n为buf的大小。status存放函数执行的结果,返回值为0表示执行成功。
下面是使用这个接口函数进行demangle的例子:

/*
* Author: Chaos Lee * Description: Employ __cxa_demangle to demangle a mangling function name. * Date:2012/05/06 *
*/
#include<iostream>
#include<cxxabi.h>
using namespace std; using namespace abi; int main(int argc,char *argv[]) { const char * mangled_string = "_Z9test_funcRiPKcdSsf"; char buffer[100]; int status; size_t n=100; __cxa_demangle(mangled_string,buffer,&n,&status); cout<<buffer<<endl; cout<<status<<endl; return 0;
}

测试结果:

[lichao@sg01 name_mangling]$ g++ cxa_demangle.cpp -o cxa_demangle

[lichao@sg01 name_mangling]$ ./cxa_demangle

test_func(int&, char const*, double, std::string, float)

0

解决 undefined reference to 问题相关推荐

  1. 解决undefined reference to symbol ‘sem_close@@GLIBC_2.2.5‘问题

    错误图示 问题原因 编译的时候,没有引入库文件  sem()位于pthread库中,所以在编译和链接时请确保使用-pthread标志,因此在编译的时候需要导入pthread库文件 编译的顺序出现问题 ...

  2. 解决undefined reference to symbol ‘LZ4_decompress_safe‘问题

    文章目录 问题描述 解决方法 参考资源 问题描述 make时碰到如下问题 /usr/bin/ld: CMakeFiles/nearest_neighbors.dir/nearest_neighbors ...

  3. android 解决 undefined reference to 报错

    在jni中写了一个inline函数.编译时报错undefined reference to. 仔细检查了下,我已经在c文件前面定义了这个函数. inline const char *checkJump ...

  4. 如何解决undefined reference to `bblib_idft_burst_fxp'编译时函数未定义问题

    本文主要提供一种解决问题的思路: 出现的错误如图所示: bblib_idft_burst_fxp在库函数中已经定义了,然而编译的时候显示未定义:经过一天的尝试,最终还是让别人解决了,我在此记录一下方法 ...

  5. clion编译器解决undefined reference to symbol ‘shm_open@@GLIBC_2.2.5‘

    修改CMakelists文件 cmake_minimum_required(VERSION 3.17) project(mutex_learn)set(CMAKE_CXX_STANDARD 14)se ...

  6. undefined reference to pow

    解决undefined reference to `pow'问题 问题现象 使用math库, #include <math.h> 提示: undefined reference to `p ...

  7. 【GCC编译优化系列】GCC链接失败的错误提示 undefined reference to ‘xxx‘ 可能还有一种情况你没注意到?

    文章目录 1 写在前面 2 问题描述 2.1 问题现场 2.2 快速排查 2.3 判断问题 3 知识点突破 3.1 场景复现 3.2 深入分析 3.3 涨点新知识 4 经验总结 5 参考链接 6 更多 ...

  8. 【Qt】报错error: undefined reference to `vtable for的解决方法

    1.问题描述 编译Qt程序时,在某个类构造函数定义处报错: error: undefined reference to `vtable for 2.原因分析 导致错误信息的原因是:子类没有实现父类的纯 ...

  9. 报错解决:undefined reference to `snappy::MaxCompressedLength(unsigned long)'

    下午在编译phxpaxos的样例时报错了,报错如下: g++ echo_sm.o echo_server.o main.o -o phxecho -L/home/zhang/phxpaxos/.lib ...

最新文章

  1. 在Ubuntu上编译opencv 2.4.13源码支持android平台操作步骤
  2. linux swp 内存不足,Linux 增加 Swap 交换分区解决内存不足
  3. 2017.10.9 JVM入门学习
  4. python程序设计丁亚涛课后答案_python程序设计丁亚涛版课后答案
  5. 20211126 为什么转动惯量矩阵是正定的?
  6. 安装rabbitMQ delayed-messaged
  7. 【Linux】时间戳与正常日期
  8. linux安装python3.7的步骤_centos7安装python3 的三种方式
  9. android ascii 比较大小写,为什么可以通过ASCII中的字母排序规则来进行字母的大小写转换?...
  10. dataframe 空值替换为0_Python数据结构大结局:DataFrame
  11. turbo编译码c语言,Turbo码的编译码原理及仿真.pdf
  12. ERP基础数据 华夏
  13. c语言中pinMode的作用,Arduino编程基础与常用函数(详细)解析
  14. 苹果描述文件服务器证书无效,22.iOS企业版证书、描述文件过期问题解决
  15. 联想计算机无线网络设置密码,联想(Lenovo)无线路由器怎么设置
  16. 悬浮窗——判断及跳转(包含OPPO 5.1 系统等)
  17. 计算机c盘用户爆满,为啥你的windows电脑C盘经常爆满?
  18. 特殊格式的时间读取并排序
  19. 图形界限命令在命令行输入_设置图形界限的命令为在命令行输入
  20. 使用QuickBI制作企业报表门户

热门文章

  1. 配置并检验 S1 上的安全功能
  2. Nginx的代理缓存设置
  3. C#+arcengine开发中ITable与DataTable的转换问题,ITable与DataTable相互转换的代码[转]
  4. git push报错 Empty reply from server
  5. emplace_back减少内存拷贝和移动
  6. 如何在hdfs上将文件下载_如何在Windows 10上将文件复制到USB闪存驱动器
  7. CSS实现赛博朋克风格按钮
  8. 系统中怎么删除右键新建菜单中多余的选项
  9. 细节是决定成败,网络上告诉你怎么做。
  10. JAVA中Integer的缓存机制