c++ undefined reference std::__cxx11::basic_string

记录今天遇到的问题:

  1. 使用c++11 写了一段c++程序,使用gcc 7.3编译。程序引用了非c++11编写的静态库。

  2. 链接时总是报某个函数找不到 但是使用nm命令查看,符号确实存在。

我们百度或者Google可以发现,这个原因来源与GCC的版本问题。因为自gcc 5以后引入了新的ABI,简单点说就是gcc5 重写了string和list,我们使用的库的接口一旦使用这几个类,就容易出现这个问题。

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

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.

对于我的问题,需要在参数上增加 -D_GLIBCXX_USE_CXX11_ABI=0 就解决了。
如果是使用cmake,在CMakelist.txt添加如下定义。

add_definitions(-D_GLIBCXX_USE_CXX11_ABI=0)

发布了45 篇原创文章 · 获赞 21 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/xxradon/article/details/103086594