I have simple boost unit test snippet:
#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE MyTest
#include <boost/test/unit_test.hpp>
int add(int i, int j) {
return i + j;
}
BOOST_AUTO_TEST_CASE(my_test) {
}
but by compiling using the following command:
g++ main.cpp --std=c++14 -lboost_unit_test_framework
I get the following error:
/tmp/ccHOUqfd.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x4ae): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, boost::unit_test::decorator::collector&, unsigned long)'
/tmp/ccHOUqfd.o: In function `boost::unit_test::make_test_case(boost::function<void ()> const&, boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long)':
main.cpp:(.text._ZN5boost9unit_test14make_test_caseERKNS_8functionIFvvEEENS0_13basic_cstringIKcEES8_m[_ZN5boost9unit_test14make_test_caseERKNS_8functionIFvvEEENS0_13basic_cstringIKcEES8_m]+0xa7): undefined reference to `boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::basic_cstring<char const>, unsigned long, boost::function<void ()> const&)'
collect2: error: ld returned 1 exit status
I have tried compiling with header only version and it works - by changing the boost unit test include to:
#include <boost/test/included/unit_test_framework.hpp>
But then the compile times are way longer. What am I missing here? I have tried several possibilities with dynamic and static flags but none of them worked.
EDIT
Ok, so thanks to cmake I have came to this command:
g++ main.cpp --std=c++14 -rdynamic /usr/local/lib/libboost_unit_test_framework.so -Wl,-rpath,/usr/local/lib
which builds my test successfully. Can anyone explain why is that the case? Why do I need -rdynamic
, explicit absolute library path and -Wl,-rpath,...
?
Well, your boost libs are obviously installed into a non-standard location. That's why you need to tell the linker where to find more boost libs on which the unit test lib depends.
For a complete example that builds and runs on both Linux and MSVC 2019, see: https://github.com/jjYBdx4IL/check_zpool