18

I want to use Boost test in my project.

I use cmake in my project so I wrote a simple CMakeList.txt for wrapping it:

find_package (Boost COMPONENTS unit_test_framework REQUIRED)
file(GLOB_RECURSE UnitTests_sources tests/*.cpp)
add_executable(UnitTests
    ${UnitTests_sources}
)
enable_testing()
ADD_TEST (UnitTests UnitTests)

So, cmake works fine here. The trouble becomes during compiling:

Linking CXX executable ../../bin/UnitTests

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crt1.o: In function _start': (.text+0x20): undefined reference tomain' collect2: ld returned 1 exit status

Here is the only file in tests folder (LogManagerTest.cpp):

#include "Utils/LogManager.hpp"
#include <boost/test/unit_test.hpp>

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN

#define BOOST_TEST_MODULE LogManager

BOOST_AUTO_TEST_CASE(LogManagerCase)
{
    BOOST_REQUIRE(true);
    /*LogManager manager;
    manager.Initialize();
    manager.Deinitialize();*/
}

What's wrong here?

Jay P.
  • 4,745
  • 5
  • 44
  • 74
Max Frai
  • 52,556
  • 73
  • 182
  • 293
  • Just go to the [starter example](http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/intro.html) and then go to the [usage variants](http://www.boost.org/doc/libs/1_66_0/libs/test/doc/html/boost_test/usage_variants.html). If you are not comfortable with linker options, etc... stick to the header only variant of Boost.Test. And yes: changing the order of the macros and includes **do have** an impact. – Raffi Feb 14 '18 at 19:43

4 Answers4

28

Add

ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) 

to your CMakeLists.txt so it will automatically generate a main() for you. Also,

#define BOOST_TEST_MODULE xxx

must be defined before you include unit_test.hpp.

You can find more information and options on: http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/compilation.html

André Puel
  • 7,716
  • 7
  • 45
  • 78
  • It's defined in my cpp-file, isn't it? (your idea returned for me warning, that `BOOST_TEST_DYN_LINK` is redefined). – Max Frai Oct 15 '11 at 23:36
  • It is defined only after you included . So it didnt affect. You must define before. – André Puel Oct 15 '11 at 23:46
  • I've found that while `ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK)` resolves the problem on Linux, it breaks my build on WIndows(MinGW). If this happens to you, consider wrapping this line in IF(UNIX) ... ENDIF(UNIX). – Laurynas Lazauskas Mar 10 '16 at 14:07
7

You need to compile with -lboost_unit_test_framework, boost generates the main for you if you use the BOOST_TEST_DYN_LINK so you need to tell the makefile to look for that main. Then you can compile using the following:

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MODULE LogManager                                   
BOOST_AUTO_TEST_CASE(LogManagerCase)                                   
{                                                                      
    BOOST_REQUIRE(true);                                               
    /*LogManager manager;                                              
    manager.Initialize();                                              
    manager.Deinitialize();*/                                          
}                                                                      
BOOST_AUTO_TEST_SUITE_END()    
shuttle87
  • 14,785
  • 9
  • 69
  • 104
3

Try moving

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN

before your includes - it worked for me.

0

Once I made a stupid typo and also got this. And compiler was cheated.

Just as "#define BOOTS_TEST_MODULE DUMMY". Yes, I like boots :)

DAG
  • 347
  • 2
  • 7