Boost test linking

Max Frai picture Max Frai · Oct 16, 2011 · Viewed 17.8k times · Source

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?

Answer

Andr&#233; Puel picture André Puel · Oct 16, 2011

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