Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linker error while building unit tests with Boost

I'm using gcc 4.8.2 with Boost 1.59. I run on Kubuntu 12.04 LTS. I'm just trying to write a simple unit test application and I'm experiencing linker error.

Here is my code:

main.cpp

#define BOOST_TEST_MODULE My Module
#define BOOST_TEST_DYN_LINK
#include <boost/test/unit_test.hpp>

test1.cpp

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

BOOST_AUTO_TEST_SUITE( suite1 );
BOOST_AUTO_TEST_CASE( case1 )
{
    BOOST_TEST_MESSAGE( "Hello!" );
}
BOOST_AUTO_TEST_SUITE_END();

When I build I do: g++ -std=c++11 -o test main.cpp test1.cpp -I/usr/local/include -L/usr/local/lib -lboost_unit_test_framework. And I get this result:

/tmp/ccmNLUMx.o: In function `__static_initialization_and_destruction_0(int, int)':
test1.cpp:(.text+0x15e): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::basic_cstring<char const>)'
test1.cpp:(.text+0x1b9): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
/tmp/ccmNLUMx.o: In function `boost::unit_test::make_test_case(boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&, boost::unit_test::basic_cstring<char const>)':
test1.cpp: (.text._ZN5boost9unit_test14make_test_caseERKNS0_9callback0INS0_9ut_detail6unusedEEENS0_13basic_cstringIKcEE[_ZN5boost9unit_test14make_test_caseERKNS0_9callback0INS0_9ut_detail6unusedEEENS0_13basic_cstringIKcEE]+0x6d): undefined reference to `boost::unit_test::test_case::test_case(boost::unit_test::basic_cstring<char const>, boost::unit_test::callback0<boost::unit_test::ut_detail::unused> const&)'
collect2: erreur: ld a retourné 1 code d'état d'exécution

Do you have any idea about what I'm doing wrong? Thanks!

Dominique

EDIT

What is given here could not solve my problem... :-(

like image 256
dom_beau Avatar asked Dec 25 '22 12:12

dom_beau


1 Answers

I had the same problem. My solution is, change the include path:

#include <boost/test/unit_test.hpp> // wrong  
#include <boost/test/included/unit_test.hpp> // this work

I had found the solution in the first example of boost unit test. I test it with boost 1.60 and gcc 4.8.5

like image 140
SebastianH Avatar answered Dec 28 '22 10:12

SebastianH