Boost unit test link error — abi mismatch?

2019-05-07 03:07发布

问题:

I'm trying to build a unit test with boost, but the linker complains about a missing function. Take this skeleton code

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_SUITE(TestFuncOps);

BOOST_AUTO_TEST_CASE(CopyConstructor)
{
}    

BOOST_AUTO_TEST_SUITE_END();

But it fails with

Undefined symbols for architecture x86_64:
  "boost::unit_test::ut_detail::normalize_test_case_name[abi:cxx11](boost::unit_test::basic_cstring<char const>)", referenced from:
      __GLOBAL__sub_I_funcopstest.cc in funcopstest.o

The libboost_unit_test_framework is found by my linker command:

g++-5 --std=c++14 funcopstest.o -L/usr/local/lib -lboost_unit_test_framework -o test_funcops

because when take away the -lboost_unit_test_framework, I get a ton of undefined references instead of only one. Boost was installed via brew from source using c++11 mode. I have tried to compile with every -fabi-version=[0-8] but nothing changed.

Does anybody have a clue what's going on?

回答1:

Add -D_GLIBCXX_USE_CXX11_ABI=0 to your CPPFLAGS, then recompile.

The libstdc++ that comes with gcc 5 had to make some changes to std::string and std::list for conformance with C++11. For backwards compatibility it supports a dual ABI, but it uses the new ABI by default.

Your libboost_unit_test_framework.so appears to be compiled without support for the new one (possibly with gcc 4.x), so when you compile your code, the compiler will generate code that expects to be linked against libraries supporting the new one. In cases where the two ABIs differ, symbols will be missing. Defining the _GLIBCXX_USE_CXX11_ABI macro to 0 makes gcc 5 use the old library code.

Alternatively, you could rebuild Boost.Test with gcc 5.