undefined reference to `std::__cxx11::basic_string<char,

centos上编译报错,部分信息如下:

/usr/local/lib/libprotobuf.so.9: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::find_first_of(char, unsigned long) [email protected]_3.4.21‘
/usr/local/lib/libboost_regex.so.1.68.0: undefined reference to `std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_M_create(unsigned long&, unsigned long)@GLIBCXX_3.4.21‘
/home/jwy/pytorch/torch/lib/libtorch.so: undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()@GLIBCXX_3.4.21‘
/usr/local/lib/libboost_regex.so.1.68.0: undefined reference to `std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_M_assign(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocat        or<wchar_t> > const&)@GLIBCXX_3.4.21‘
…… ……

注意到主要出现的信息是关于

undefined reference to `std::__cxx11::basic_string  

原因

  gcc5以及以后的版本,将std::string和std::list重写,std::list变为std::__cxx11::list<int>,std::string在c++03库是std::basic_string<char>,而在c++11中变为了std::__cxx11::basic_string<char>。而为了在编译的时候兼容旧版本(链接阶段),可在编译的时候启动_GLIBCXX_USE_CXX11_ABI 宏指定具体链接的库。

  注意:不要将c++11和c++03的库混用。

GCC官方解释

If you get linker errors about undefined references to symbols that involve types in the std::__cxx11 namespace or the tag [abi:cxx11] then it probably indicates that you are trying to link together object files that were compiled with different values for the _GLIBCXX_USE_CXX11_ABI macro. This commonly happens when linking to a third-party library that was compiled with an older version of GCC. If the third-party library cannot be rebuilt with the new ABI then you will need to recompile your code with the old ABI.
Not all uses of the new ABI will cause changes in symbol names, for example a class with a std::string member variable will have the same mangled name whether compiled with the old or new ABI. In order to detect such problems the new types and functions are annotated with the abi_tag attribute, allowing the compiler to warn about potential ABI incompatibilities in code using them. Those warnings can be enabled with the -Wabi-tag option.

  如出现std::__cxx11命名空间或abi:cxx11中未定义符号的链接错误,很可能是因为链接了使用不同的_GLIBCXX_USE_CXX11_ABI宏编译的目标,在链接旧版gcc编译的第三方库中经常出现。如第三方库无法使用新的ABI编译,则需使用旧版ABI重新编译本地代码。

  使用新版ABI不一定都会出现符号的重命名,如std::string在新旧版中名字相同。可通过在编译的时候使用-Wabi-tag选项提醒ABI是否一致。

宏定义的使用

-D_GLIBCXX_USE_CXX11_ABI=0 // 链接到旧版本,未启用c++11特性,std::string是std::basic_string<char>,如将c++11下的string当作参数传入非c++11库,就会出现error: cannot convert ‘const std::__cxx11::basic_string<char>‘ to ‘const char*‘,或者未定义的方法引用(undefined reference)
-D_GLIBCXX_USE_CXX11_ABI=1 // 链接到新版本
#define _GLIBCXX_USE_CXX11_ABI 0
#define _GLIBCXX_USE_CXX11_ABI 1

参考链接:

https://gcc.gnu.org/onlinedocs/libstdc++/manual/using_dual_abi.html

原文地址:https://www.cnblogs.com/lukybee/p/11846889.html

时间: 2024-10-13 16:30:08

undefined reference to `std::__cxx11::basic_string<char,的相关文章

Windows编译OpenCV4Android解决undefined reference to std错误

OpenCV4Android支持三种使用方式1.Java层调用2.NDK调用动态库(方便,但是会包含整个.so库)3.NDK调用静态库(灵活,比如没有用到机器学习模块,libopencv_ml.a里的代码是不会打包到动态库里的)通过链接静态库的方式使用OpenCV4Android,CMAKE配置如下 #############################prepare for using of opencv static library#################### #first

在codeblocks 下,C++编译不成功一直出现“undefined reference to `std::cout&#39;|

自己搞了好久才知道,编辑c++,要用g++ 希望对大家有帮助 在codeblocks 下,C++编译不成功一直出现"undefined reference to `std::cout'|

ubuntu下 GCC编译程序出现 undefined reference to `std::ios_base::Init::Init()&#39;问题

网上的解释是:“ you need to add -lstdc++, or use 'g++' rather than 'gcc' as your driver program.”,也就是说如果想要使用g++编译程序的话,那么链接时需要添加“-Istdc++”. 我试了一下 , 在链接时加上"-Istdc++",就没有问题了. 关于g++,让我想起了,在ubuntu下一开始安装完code::blocks的时候,提示需要安装g++.原来code::blocks内置编译器使用的是g++,但

[ c++] cmake 编译时 undefined reference to `std::cout&#39; 错误的解决方案

cmake ..  和 make 之后,出现如下错误 Linking CXX executable ../../../bin/ModuleTest CMakeFiles/ModuleTest.dir/tmp.cpp.o: In function `main': /ModuleTest/tmp.cpp:4: undefined reference to `std::cout' tmp.cpp 内容如下 1 #include <iostream> 2 3 int main(){ 4 std::co

iOS出现 Undefined symbols for architecture armv7 std::basic_string&lt;char, std::char_traits&lt;char&gt;

Undefined symbols for architecture i386: “_OBJC_CLASS_$_XXX”, referenced from: objc-class-ref in XXX ld: symbol(s) not found for architecture i386 clang: error: linker command failed with exit code 1 (use -v to see invocation) 如果真机调试就是 undefined symb

error LNK2019: 无法解析的外部符号 &quot;class std::vector&lt;class std::basic_string&lt;char,struct std::char_traits&lt;cha

error LNK2019: 无法解析的外部符号 "class std::vector<class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >,class std::allocator<class std::basic_string<char,struct std::char_traits<char>,class

由于link顺序错误导致的undefined reference

其实我之前就遇到过这个问题,也强调过,GNU-G++在link阶段是依赖输入的.o或者.a文件的顺序的.如果顺序错误会导致undefined reference错误 见这篇随笔:http://www.cnblogs.com/qrlozte/p/4137704.html 刚才我遇到的问题是什么呢? 代码demo.cpp:其中ZJ::open_max在util.h中声明,在$(PATH_ONE)/libutil.a中:err_sys在apue.h中声明,在$(PATH_TWO)/libapue.a中

「caffe编译bug」.build_release/lib/libcaffe.so: undefined reference to cv::imread

转自:https://www.douban.com/note/568788483/ CXX/LD -o .build_release/tools/convert_imageset.bin.build_release/lib/libcaffe.so: undefined reference to cv::imread(cv::String const&, int)' .build_release/lib/libcaffe.so: undefined reference tocv::imencode

Linux下运行《UNIX环境高级编程》undefined reference to `err_quit 编译出错的处理方法

错误信息: : undefined reference to `err_quit': undefined reference to `err_sys' 解决方法: 因为err_quit跟err_sys是作者自己定义的错误处理函数.所以最简单的解决办法是修改下apue.h文件,如下 /* * Our own header, to be included before all standard system headers. */ #ifndef _APUE_H #define _APUE_H #d