简体   繁体   中英

Boost Unit Test framework can't find main function

I have this minimal unit test:

#include <boost/test/unit_test.hpp>
#define BOOST_TEST_MODULE a_test

struct Color{};

BOOST_AUTO_TEST_CASE(color_test)
{
    BOOST_CHECK(std::is_pod<Color>());
}

However when I compile it like so

clang -std=c++14 -lc++ -lboost_unit_test_framework a_test.cc -o main

I get an undefined symbol for _main. -lboost_unit_test_framework uses the shared library.

I used this library before, and I remember not having to declare a main function myself, as it was automatically running the several BOOST_AUTO_TEST_CASE s I define.

What am I doing wrong?

When linking Boost.Test dynamically, you need to define BOOST_TEST_DYN_LINK ( see boost docs here ).

You may also have to link boost_test_exec_monitor .

Also, all the configuration macros need to be defined before including the library header to have any effect.

@melak47 is also right. I believe the following should work, too:

#define BOOST_TEST_MODULE my_tests TestSuites // to define main()
#include <boost/test/unit_test.hpp>

struct Color{};

BOOST_AUTO_TEST_SUITE(MyColorTests)

BOOST_AUTO_TEST_CASE(color_test)
{
    BOOST_CHECK( std::is_pod<Color>() );
}

BOOST_AUTO_TEST_SUITE_END()

If you link multiple such modules together, make sure that BOOST_TEST_MODULE is only defined in one of them, before the include.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM