Search code examples
c++compiler-errorsprogram-entry-pointboost-testlinker-errors

Boost::Test : Compiling and running a "hello world" program


I am trying to get a dummy Boost.test "hello world" program running. I found documentation here and there but obviously there is something I am missing…

Here is what I have done :

Step #1 : I installed the dependencies

sudo aptitude install libboost-test-dev

which installs both the headers (libboost-test1.54-dev) and the binary files (libboost-test1.54.0).

Step #2 : Creating the source file to be compiled

I have one single file called test.cpp which contains :

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

// EOF

as was recommended in the official tutorial

Step #3 : Compilation

I compile my code by calling :

g++ test.cpp -lboost_unit_test_framework

I am not 100% sure about the option to link the library since the official tutorial does not mention it explicitly. Yet, it seems to match the library file names I have in /usr/lib. Plus, the linker does not complain about not finding the shared object or static library files.

Which returns the following error :

/usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
collect2: error: ld returned 1 exit status

The issue

I quite agree with the linker : I don't see any main() function in my code… But where and how should I implement it ?

I am quite surprised because I was expecting to have to create a runner.cpp file defining function main() but the official boost tutorial does not mention such a thing…

This answer suggests defining the BOOST_TEST_NO_MAIN macro, but the official boost tutorial does not mention it either. Is that the proper way of doing it ?

Could someone please give me clear step-by-step instructions on how to make my dummy "hello world" project compile ?


Solution

  • You might need to add #define BOOST_TEST_DYN_LINK before Boost.Test include.

    Check here - if library was built as dynamic (and it is usually so in many linux distributions), this macro is required. It makes header file define int main() - with static linking main is defined inside static library, but dynamic linking requires main to be in 'static' part of program. So this macro will make boost header 'inject' main into your cpp file and after compilation it will be there.