g ++项目编译与boost单元测试 [英] g++ project compilation with boost unit test
问题描述
我试图在Linux上编译单元测试(boost),但编译器出现错误。
有人可以检查我的命令吗?
g ++ -o UTest ../UTest/UT1.cpp ../UTest /UT2.cpp -lboost_system -lboost_thread -lboost_unit_test_framework
错误
/usr/lib/gcc/i686-linux-gnu/4.6 /../../../ i386-linux-gnu /crt1.o:在函数`_start'中:
(.text + 0x18):未定义引用`main'
我从g ++ comand中删除了 main()
,因为 boost单元测试时不应该使用
PS没有单元测试的项目( main()
)编译得很好。
问题与
code> main()已解决。 UT1.cpp
和 UT2.cpp
包括 UTCommon.h
,现在我有很多错误,如下面
错误
tmp2.cpp :(。text + 0xd44a) :unit_test_log_t :: operator<<<(boost :: unit_test :: lazy_ostream const&)'
/tmp/cc0jw8uR.o:tmp.cpp:(.text+0xd44a):首先定义这里
/ tmp / cctLn9QJ.o:在函数`boost :: test_tools :: tt_detail :: equal_impl(char const *,char const *)'
UTCommon.h
#ifndef UT_COMMON_H
# define UT_COMMON_H
#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE UnitTest
#endif
#if defined(__GNUC__)&& defined(__ unix__)
#include< boost / test / included / unit_test.hpp>
#elif defined(WIN32)
#include< boost / test / unit_test.hpp>
#endif
#endif
最好创建一个单独的.cpp文件,其中包含文件 boost / test / included / unit_test.hpp
。这将包括在代码中预先生成的 main()
函数。然后,您可以使用 BOOST_AUTO_TEST_CASE
宏进行实际测试(根据您的喜好):
#define BOOST_TEST_DYN_LINK //这是可选的
#define BOOST_TEST_MODULE MyTest //指定测试模块的名称
#include< boost / test / included / unit_test.hpp> // include this to get main()
BOOST_AUTO_TEST_CASE(my_test_1)//指定测试用例
{
/ *测试一下... * /
const static auto expected = 12;
auto actual = my_func();
BOOST_CHECK(actual == expected);
}
编译此.cpp文件(根据需要为所有定义的函数在你自己的代码),它将成为一个可执行文件,执行所有的测试和生成报告。
I'm trying to compile unit test (boost) on Linux but compiler thows an error. Could someone check my command?
g++ -o UTest ../UTest/UT1.cpp ../UTest/UT2.cpp -lboost_system -lboost_thread -lboost_unit_test_framework
Error
/usr/lib/gcc/i686-linux-gnu/4.6/../../../i386-linux-gnu/crt1.o: In function `_start':
(.text+0x18): undefined reference to `main'
I removed main()
from g++ comand because it should not be used when boost unit test
is used.
What's wrong?
PS The project without unit tests (with main()
) is compiled fine. Unit tests on Windows work fine also.
Update
The issue with main()
is resolved. But a new one has born.
Both UT1.cpp
and UT2.cpp
has included UTCommon.h
and now I have lots error like the following
Error
tmp2.cpp:(.text+0xd44a): multiple definition of `boost::unit_test::unit_test_log_t::operator<<(boost::unit_test::lazy_ostream const&)'
/tmp/cc0jw8uR.o:tmp.cpp:(.text+0xd44a): first defined here
/tmp/cctLn9QJ.o: In function `boost::test_tools::tt_detail::equal_impl(char const*, char const*)'
UTCommon.h
#ifndef UT_COMMON_H
#define UT_COMMON_H
#ifndef BOOST_TEST_MODULE
#define BOOST_TEST_MODULE UnitTest
#endif
#if defined (__GNUC__) && defined(__unix__)
#include <boost/test/included/unit_test.hpp>
#elif defined (WIN32)
#include <boost/test/unit_test.hpp>
#endif
#endif
Best create a separate .cpp file that includes the file boost/test/included/unit_test.hpp
. This will include an pre-generated main()
function in your code. You can then use the BOOST_AUTO_TEST_CASE
macro for the actual tests (as many times as you like):
#define BOOST_TEST_DYN_LINK // this is optional
#define BOOST_TEST_MODULE MyTest // specify the name of your test module
#include <boost/test/included/unit_test.hpp> // include this to get main()
BOOST_AUTO_TEST_CASE(my_test_1) // specify a test case
{
/* Test something... */
const static auto expected = 12;
auto actual = my_func();
BOOST_CHECK(actual == expected);
}
Compile this .cpp file (add linker options as necessary for all the functions defined in your own code), and it will become an executable that performs all the tests and generates a report.
这篇关于g ++项目编译与boost单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!