测试环境 CentOS Linux release 7.9.2009 (AltArch) 编译环境gcc version 4.8.2 (GCC)
场景
对接大华ARM架构的SDK,编译链接libdhconfigsdk.so文件出错:
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::_M_replace_aux(unsigned long, unsigned long, unsigned long, char)'
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long)'
/dahua/lib/libdhconfigsdk.so: undefined reference to `__cxa_throw_bad_array_new_length'
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string(unsigned long, char, std::allocator<char> const&)'
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::basic_string()'
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::operator[](unsigned long) const'
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::length() const'
/dahua/lib/libdhconfigsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::at(unsigned long)'
/dahua/lib/libdhnetsdk.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::append(unsigned long, char)'
解决过程
1)更新gcc version 4.8.2 (GCC)支持C11,提示同样错误
解决方案
红帽提供的开发工具包来管理gcc版本,这样做的好处是随时切换版本,并且可以并存多个版本,不破坏原有gcc环境。
红帽官方Developer Toolset文档地址:https://access.redhat.com/documentation/en-us/red_hat_developer_toolset/8/
devtoolset对应gcc的版本
devtoolset-3对应gcc4.x.x版本
devtoolset-4对应gcc5.x.x版本
devtoolset-6对应gcc6.x.x版本
devtoolset-7对应gcc7.x.x版本
安装devtoolset包
yum install centos-release-scl yum install devtoolset-4
yum install centos-release-scl
yum install devtoolset-7
激活gcc版本,使其生效
scl enable devtoolset-7 bash
或
source /opt/rh/devtoolset-7/enable
此时通过gcc --version命令可以看到,gcc版本已经变成5.3.1,值得注意的是这仅仅在当前bash生效,如果需要永久生效,可以请自行添加环境变量。