mardi 13 septembre 2016

Linking errors when compiling Boost unit tests with CMake

I realize from the title that this may appear to be a duplicate, but none of the other posts' answers worked for me.

I get a ton of undefined reference errors when I try to compile my Boost unit tests using CMake 2.8

point3D_test.cpp

#define BOOST_TEST_MAIN
#define BOOST_TEST_MODULE "Point3D"
#include <boost/test/unit_test.hpp>

#include "point3D.h"

BOOST_AUTO_TEST_CASE(TestDefaultConstructor)
{
    Reprojection::Point3D pt;
    BOOST_CHECK_EQUAL(pt.X(), 0);
    BOOST_CHECK_EQUAL(pt.Y(), 0);
    BOOST_CHECK_EQUAL(pt.Z(), 0);
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
project(MyProject)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

enable_testing()

# Necessary to automatically generate main() in tests
add_definitions(-DBOOST_TEST_DYN_LINK)

macro(BUILD_TEST TARGET_NAME SOURCES LIBRARIES)
    add_executable(${TARGET_NAME} ${SOURCES})
    target_link_libraries(${TARGET_NAME} ${LIBRARIES})
    add_test(${TARGET_NAME} ${TARGET_NAME})
endmacro(BUILD_TEST)

find_package(Boost COMPONENTS
    program_options
    filesystem
    system
    unit_test_framework
    REQUIRED)

set(MY_LIBS ${Boost_LIBRARIES})

BUILD_TEST(point3D_test point3D_test.cpp ${MY_LIBS})

This produces a bunch of errors like

CMakeFiles/point3D_test.dir/point3D_test.cpp.o: In function `init_unit_test()':
point3D_test.cpp:(.text+0x4b): undefined reference to `boost::unit_test::framework::master_test_suite()'
CMakeFiles/point3D_test.dir/point3D_test.cpp.o: In function `main':
point3D_test.cpp:(.text+0x94): undefined reference to `boost::unit_test::unit_test_main(bool (*)(), int, char**)'
CMakeFiles/point3D_test.dir/point3D_test.cpp.o: In function `TestDefaultConstructor::test_method()':
point3D_test.cpp:(.text+0xd3): undefined reference to `Reprojection::Point3D::Point3D()'
point3D_test.cpp:(.text+0x137): undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
point3D_test.cpp:(.text+0x278): undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
point3D_test.cpp:(.text+0x3ba): undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
point3D_test.cpp:(.text+0x4fc): undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'
point3D_test.cpp:(.text+0x62e): undefined reference to `boost::unit_test::unit_test_log_t::set_checkpoint(boost::unit_test::basic_cstring<char const>, unsigned long, boost::unit_test::basic_cstring<char const>)'

My unit test file is longer, but all of the errors are like this one. What's going on?

Aucun commentaire:

Enregistrer un commentaire