提升测试链接

2022-01-11 00:00:00 unit-testing linker c++ boost boost-test

我想在我的项目中使用 Boost 测试.

I want to use Boost test in my project.

我在我的项目中使用了 cmake,所以我写了一个简单的 CMakeList.txt 来包装它:

I use cmake in my project so I wrote a simple CMakeList.txt for wrapping it:

find_package (Boost COMPONENTS unit_test_framework REQUIRED)
file(GLOB_RECURSE UnitTests_sources tests/*.cpp)
add_executable(UnitTests
    ${UnitTests_sources}
)
enable_testing()
ADD_TEST (UnitTests UnitTests)

所以,cmake 在这里可以正常工作.编译时出现问题:

So, cmake works fine here. The trouble becomes during compiling:

链接 CXX 可执行文件 ../../bin/UnitTests

Linking CXX executable ../../bin/UnitTests

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crt1.o:在function _start': (.text+0x20): undefined reference tomain'collect2: ld 返回 1 个退出状态

/usr/lib/gcc/x86_64-unknown-linux-gnu/4.6.1/../../../../lib/crt1.o: In function _start': (.text+0x20): undefined reference tomain' collect2: ld returned 1 exit status

这是tests文件夹中唯一的文件(LogManagerTest.cpp):

Here is the only file in tests folder (LogManagerTest.cpp):

#include "Utils/LogManager.hpp"
#include <boost/test/unit_test.hpp>

#define BOOST_TEST_DYN_LINK
#define BOOST_TEST_MAIN

#define BOOST_TEST_MODULE LogManager

BOOST_AUTO_TEST_CASE(LogManagerCase)
{
    BOOST_REQUIRE(true);
    /*LogManager manager;
    manager.Initialize();
    manager.Deinitialize();*/
}

这里有什么问题?

推荐答案

添加

ADD_DEFINITIONS(-DBOOST_TEST_DYN_LINK) 

到您的 CMakeLists.txt 中,因此它会自动为您生成一个 main().还有,

to your CMakeLists.txt so it will automatically generate a main() for you. Also,

#define BOOST_TEST_MODULE xxx

必须在包含 unit_test.hpp 之前定义.

must be defined before you include unit_test.hpp.

您可以找到更多信息和选项:http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/compilation.html

You can find more information and options on: http://www.boost.org/doc/libs/1_47_0/libs/test/doc/html/utf/compilation.html

相关文章