简体   繁体   English

Boost单元测试框架找不到主要功能

[英]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. 我得到_main的未定义符号。 -lboost_unit_test_framework uses the shared library. -lboost_unit_test_framework使用共享库。

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. 我以前使用过这个库,记得没有必要自己声明main函数,因为它会自动运行我定义的几个BOOST_AUTO_TEST_CASE

What am I doing wrong? 我究竟做错了什么?

When linking Boost.Test dynamically, you need to define BOOST_TEST_DYN_LINK ( see boost docs here ). 动态链接Boost.Test时,您需要定义BOOST_TEST_DYN_LINK请参阅此处的boost文档 )。

You may also have to link boost_test_exec_monitor . 您可能还必须链接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. @ melak47也正确。 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. 如果将多个这样的模块链接在一起,请确保在包含之前仅在其中一个模块中定义了BOOST_TEST_MODULE

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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