Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost.test cannot find main

I'm working with gcc 4.8, boost 1.59 on kubuntu 12.04.

I wrote a simple main.cpp file:

#define BOOST_TEST_MODULE My_Module
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( foo )
{}

This doesn't work when I build with

g++ -std=c++11 main.cpp -I/usr/local/include -L/usr/local/lib -lboost_unit_test_framework -o test

I get a bunch of linker errors:

/usr/lib/x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to 'main'
/tmp/cc57ppN0.o: In function `__static_initialization_and_destruction_0(int, int)':
main.cpp:(.text+0x131): undefined reference to `boost::unit_test::ut_detail::auto_test_unit_registrar::auto_test_unit_registrar(boost::unit_test::test_case*, unsigned long)'
/tmp/cc57ppN0.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>)':
main.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

What does undefined reference to 'main' means??? Well, I know that it is because it could not find main() but why? AFAIK the syntax of my file is correct. It should link, no?

like image 638
dom_beau Avatar asked Dec 18 '22 22:12

dom_beau


2 Answers

You need to insert the following directive at the top of main.cpp:

#define BOOST_TEST_DYN_LINK

It seems that the example in the Boost.test documentation works for static linking only: the directive above is required, however, for dynamic linking.

See e.g C++ Unit Testing With Boost.Test for further details.

like image 59
Daniel Strul Avatar answered Dec 30 '22 12:12

Daniel Strul


OK, I found the solution!

It seems that, since 1.34.1, boost.test no longer contains main() in dynamic (.so) version. See here. So I wanted to link with the static and I also learned that gcc prefers the dynamic libraries over the static ones for the same name!. Thus, I changed my compile command to:

g++ -std=c++11 main.cpp -I/usr/local/include -L/usr/local/lib -lboost_unit_test_framework -static -o test

...and it worked fine!

I also tested with two files ... main.cpp test1.cpp and the run executed all the test cases correctly.

Thank you, I hope this can help someone else!

like image 35
dom_beau Avatar answered Dec 30 '22 14:12

dom_beau