undefined reference to `std::__cxx11::basic_string


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) const@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_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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM