‘_ZNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE13_S_copy_charsEPcPKcS

文章讲述了在使用gcc编译ROS2demo时遇到的undefinedreference错误,经过尝试不同GCC和G++版本、更新包管理器优先级以及识别出Makefile中的target_library问题,最终发现是缺少动态链接库引起的。作者给出了正确链接动态库的解决方案。

问题记录

使用gcc编译ros2 demo时,报错如下

undefined reference to symbol ‘_ZNKSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEE7compareEPKc@@GLIBCXX_3.4.21’

libstdc++: DSO missing from command line

尝试方案

GCC切换版本
1.参考上述博主进行修改

gcc --version

gcc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright © 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

g++ --version

g++ (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0
Copyright © 2021 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

依次修改GCC及G++版本为10、9
安装对应版本

sudo apt-get install gcc-10
sudo apt-get install gcc-9
sudo apt-get install g++-10
sudo apt-get install g++-9

使用包管理工具修改优先级

sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-10 45 
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 40 
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-10 45
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 40 

修改版本,这里g++与gcc需要版本一致

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

修改后报错,指出c++17标准与10、9不兼容,编译失败

解决方案

改回11后,仔细查找相关问题,发现这个情况与gcc、g++版本无关,实际上是由于Makefile中缺乏target_library链接所导致的报错。

DSO missing from command line中,DSO是指“Dynamic Shared Object”,也称为共享库或者动态链接库

在gcc编译器中,可以使用“-L”和“-l”选项来指定需要链接的动态共享对象,添加命令行

                "-L", "/opt/ros/humble/lib/ ",  #这里是动态库文件所在目录
                "-lrclcpp","-lrcutils"          #rclcpp、rcutils即所添加的动态库文件

librclcpp.so #linux系统下动态库文件以.so结尾,静态库文件以.a结尾 代码
librcutils.so

程序顺利编译完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值