c++boostcmake

Compiling a boost test with Cmake


I am trying to simplify a large project by having cmake compile it all for me, but i am having trouble compiling the boost unit tests. The cmake file for my simple example is shown below.

cmake_minimum_required(VERSION 2.8)
find_package(Boost COMPONENTS system filesystem REQUIRED)
add_excecutable(testTheTester boostTester.cpp)
target_link_libraries(testTheTester ${Boost_FILESYSTEM_LIBRARY} ${Boost_SYSTEM_LIBRARY})
add_test(tester tester)

and the code in boostTester.cpp is:

#define BOOST_TEST_MAIN
#if !defined( WIN32 )
    #define BOOST_TEST_DYN_LINK
#endif
#include <boost/test/unit_test.hpp>

BOOST_AUTO_TEST_CASE( la ) {
    BOOST_CHECK_EQUAL(1, 1)
}

Now this cpp code will compile and run fine if i build it manually with:

g++ boostTester.cpp -o output -lboost_unit_test_framework

and the cmake works fine but when using the output make file the make crashes with a huge amount of errors the first of which is this:

undefined referance to 'boost::unit_test::unit_test_log_t::set_checkpoint(boost... bla bla

now my initial thought is that the cmake is not linking the boost library correctly and I have tried many commands and combinations with no luck. Does anyone know how to link the boost_unit_test in a cmake file?


Solution

  • You need to include the unit test framework in the list of requirements in the find_package command, and then link it:

    find_package(Boost COMPONENTS system filesystem unit_test_framework REQUIRED)
    ...
    target_link_libraries(testTheTester
                          ${Boost_FILESYSTEM_LIBRARY}
                          ${Boost_SYSTEM_LIBRARY}
                          ${Boost_UNIT_TEST_FRAMEWORK_LIBRARY}
                          )