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++)