Follow Excellent, Success will Chase you

0%

C与C++混合编译

在C代码中引用C++编译的库时,编译错误处理

1
2
3
4
5
6
7
8
9
10
11
12
13
undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
undefined reference to `std::allocator<char>::~allocator()'
undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
undefined reference to `std::allocator<char>::~allocator()'
undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
undefined reference to `std::allocator<char>::~allocator()'
undefined reference to `__cxa_free_exception'
undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
undefined reference to `std::allocator<char>::~allocator()'
undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
undefined reference to `std::allocator<char>::~allocator()'
undefined reference to `std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >::~basic_string()'
undefined reference to `std::allocator<char>::~allocator()'

链接时的错误信息

原因

编译器版本不兼容

添加编译选项

1
-D_GLIBCXX_USE_CXX11_ABI=1

cmake: add_definitions(-D_GLIBCXX_USE_CXX11_ABI=1)

未引用C++库

1
-lstdc++

camke: target_link_libraries(exe_target stdc++)

-------------本文结束感谢您的阅读-------------
  • 本文作者: Winddoing
  • 本文链接: https://winddoing.github.io/post/74f3b3c3.html
  • 作者声明: 本博文为个人笔记, 由于个人能力有限,难免出现错误,欢迎大家批评指正。
  • 版权声明: 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!