10

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 ?

Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
  • are you actually including the boost headers? shoudln't there be an `-I/usr/include` or something? I believe the boost test headers should provide a main for you... :hmmm: sorry it's been a while since I used boost::test, imo it's a bit over-engineered – Chris Beck Oct 15 '15 at 08:24
  • By default, the compiler can include the header file `boost/test/unit_test.hpp` which is located in `/usr/include`. Hence `-I/usr/include` doesn't change a thing… Out of curiosity : what is your favourite unit testing framework ? (not that I'd like to start a flamewar ! (^.^) ) – Gael Lorieul Oct 15 '15 at 08:34
  • The official tutorial has been updated: http://www.boost.org/doc/libs/master/libs/test/doc/html/boost_test/intro.html uses the header only variant, and this section http://www.boost.org/doc/libs/master/libs/test/doc/html/boost_test/usage_variants.html introduces all variants – Raffi Jan 11 '16 at 21:23

3 Answers3

6

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.

Hcorg
  • 11,598
  • 3
  • 31
  • 36
  • Yes (almost !) : compilation succeeds with `g++ test.cpp -DBOOST_TEST_DYN_LINK -lboost_unit_test_framework` ! But running the outputted binary gives following error : `Test setup error: test tree is empty` whereas the official tutorial shows a successful output : `*** No errors detected`. What is the (hopefully) last missing step ? – Gael Lorieul Oct 15 '15 at 09:42
  • BTW, in `/usr/lib/x86_64-linux-gnu/` I have both files `libboost_unit_test_framework.so` and `libboost_unit_test_framework.a` : does that make sense ? Why leave the choice to the user ? – Gael Lorieul Oct 15 '15 at 09:42
  • @GLorieul - probably tutorial is outdated - you need to define at least single test to see "No errors detected". As for .a lib - it might be only a wrapper that loads .so, or (as you suggest) - it's for those users who really need static linking – Hcorg Oct 15 '15 at 10:00
  • indeed, adding a test solves my issue ! Thanks for the help ! – Gael Lorieul Oct 15 '15 at 10:40
  • BTW the tutorial I was originally linking to was the tutorial for a previous version (v 1.43) of Boost::Test. Yet, its content has not been updated either in the tutorial relative to the [version I am using (1.54)][1] nor to the one concerning the [latest version (1.59)][2]. [1] http://www.boost.org/doc/libs/1_54_0/libs/test/doc/html/tutorials/new-year-resolution.html [2] http://www.boost.org/doc/libs/1_59_0/libs/test/doc/html/boost_test/practical_usage_recommendations/tutorials/bt_and_tdd.html – Gael Lorieul Oct 15 '15 at 10:41
5

Note : the following procedure is based in @Hcorg's answer and the subsequent discussion in the comments.

Step #1 : Install the dependencies

In my case (Linux Mint 17.0 Qiana) :

sudo aptitude install libboost-test-dev

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

Both the static (*.a) and dynamic (.so) library files are being installed, which leaves the choice to the user (or compiler) to use either.

Step #2 : Create the source file to be compiled

Create one single file called test.cpp which contains :

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

BOOST_AUTO_TEST_CASE(dummy) {
    BOOST_CHECK(1 + 1 == 2);
}

If you are using Boost::Test v1.59 you should instead write :

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

BOOST_AUTO_TEST_CASE(dummy) {
    BOOST_TEST(1 + 1 == 2);
}

This differs from the instructions in the official tutorial (as of today 15th Oct 2015).

Step #3 : Compilation

Compile the source file by calling either :

g++ test.cpp -DBOOST_TEST_DYN_LINK -lboost_unit_test_framework

if your compiler or yourself decide to use the dynamic library or

g++ test.cpp -lboost_unit_test_framework

if your compiler or yourself decide to use the static library.

Compilation should succeed silently.

Step #4 : Running the program

Calling

./a.out

should lead to following output :

Running 1 test case...

*** No errors detected
Gael Lorieul
  • 3,006
  • 4
  • 25
  • 50
1

On your system, both static and shared libraries are installed by aptitude and this is known that in this case, gcc will prefer shared libraries. (see making gcc prefer static libs to shared objects when linking? for example.)

You are hence building your boost.test module with the shared library variant

and this requires setting the macro BOOST_TEST_DYN_LINK before any headers of boost.test.

Community
  • 1
  • 1
Raffi
  • 3,068
  • 31
  • 33