Boost.Test - 覆盖 main 的问题

Boost.Test - problems overriding main

我想在使用 Boost.Test 时提供自己的主要功能。所以我包含了以下宏:

#define BOOST_TEST_ALTERNATIVE_INIT_API
#define BOOST_TEST_NO_MAIN

在我的主要功能中,我调用了:

::boost::unit_test::unit_test_main(&registering_all_tests, argc, argv)

但是当使用 Xcode 6 在 OS X 上构建时,这给了我以下错误:

Undefined symbols for architecture x86_64:
  "boost::unit_test::unit_test_main(bool (*)(), int, char**)", referenced from:
      _main in main.o

我发现包括以下文件:

#include <boost/test/included/unit_test.hpp>

解决了这个问题,但我对其中的含义感到困惑。该文件的描述表明它使用包含(与链接)版本的单元测试框架。

Boost 入门指南指出:

Boost.Test can be used in “header-only” or “separately compiled” mode, although separate compilation is recommended for serious use.

我单独建库了

Boost 文档还指出:

Including the UTF directly into your test module

If you prefer to avoid the standalone library compilation you can either include all files that constitute the static library in your test module's makefile or include them as a part of a test module's source file. To facilitate the later variant the UTF presents the single-header usage variant. In either case no special build options or macro definitions are required to be added to your compilation options list by default. But the same flags that can be used for the standalone library compilation are applicable in this case. Though, obviously, neither BOOST_TEST_DYN_LINK nor BOOST_TEST_NO_LIB are applicable. This solution may not be the best choice in a long run, since it requires the UTF sources recompilation for every test module you use it with and for every change of a test module you are working on. In a result your testing cycle time may increase. If it become tiresome, I recommend switching to one of the prebuilt library usage variants.

似乎通过选择包含单元测试框架的包含(与链接)版本我做错了事。

谁能解释一下正确的方法。覆盖 main 的例子没有提到需要这样做。

Boost.Test documentation 的最新版本比以前的版本对此主题进行了更详细的介绍。

通过使用定义的符号 BOOST_TEST_NO_MAIN 构建单元测试框架静态库,然后我可以提供自己的主要函数,它使用默认初始化函数 [=13] 手动调用默认测试运行器 unit_test_main =]作为第一个参数:

int exitCode = ::boost::unit_test::unit_test_main(&init_unit_test_suite, argc, argv);

不需要定义BOOST_TEST_ALTERNATIVE_INIT_API.