Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 261
Gael Lorieul Avatar asked Oct 15 '15 08:10

Gael Lorieul


People also ask

How do I compile with Boost?

To compile anything in Boost, you need a directory containing the boost/ subdirectory in your #include path. depending on your preference regarding the use of angle bracket includes. Don't be distracted by the doc/ subdirectory; it only contains a subset of the Boost documentation. Start with libs/index.

How do you write Hello World in CPP?

6) std::cout<<“Hello World”;: This line tells the compiler to display the message “Hello World” on the screen. This line is called a statement in C++. Every statement is meant to perform some task. A semi-colon ';' is used to end a statement.

How do you add Boost in C++?

In the properties dialog, select "Configuration Properties" and then "VC++ Directories". You will need to add the Boost include path to the "Include Directories" list. If you're using all header-only libraries then you're done. Otherwise, you will need to add the Boost library path to "Library Directories".


2 Answers

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.

like image 159
Hcorg Avatar answered Nov 04 '22 20:11

Hcorg


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
like image 36
Gael Lorieul Avatar answered Nov 04 '22 21:11

Gael Lorieul