gpt4 book ai didi

c++ - 在 Visual Studio 中提升测试

转载 作者:行者123 更新时间:2023-11-30 01:23:41 24 4
gpt4 key购买 nike

我正在尝试在 Visual Studio 2010 中编译简单的单元测试项目。我有一个 testrunner.cpp:

#define BOOST_TEST_DYN_LINK

#define BOOST_TEST_MODULE "BaumWelch Unit Tests"
#include <boost/test/unit_test.hpp>

和exampletests.cpp

#include <boost/test/unit_test.hpp>

int add( int i, int j ) { return i+j; }

BOOST_AUTO_TEST_CASE( my_test )
{
// seven ways to detect and report the same error:
BOOST_CHECK( add( 2,2 ) == 4 ); // #1 continues on error
BOOST_REQUIRE( add( 2,2 ) == 4 ); // #2 throws on error

if( add( 2,2 ) != 4 )
BOOST_ERROR( "Ouch..." ); // #3 continues on error

if( add( 2,2 ) != 4 )
BOOST_FAIL( "Ouch..." ); // #4 throws on error

if( add( 2,2 ) != 4 ) throw "Ouch..."; // #5 throws on error

BOOST_CHECK_MESSAGE( add( 2,2 ) == 4, // #6 continues on error
"add(..) result: " << add( 2,2 ) );
BOOST_CHECK_EQUAL( add( 2,2 ), 4 ); // #7 continues on error
}

此示例在 Linux 中运行良好,但在这里无法编译:

1>------ Build started: Project: Test, Configuration: Release Win32 ------
1>Build started 30/01/2013 14:47:48.
1>InitializeBuildStatus:
1> Touching "Release\Test.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1>boost_unit_test_framework-vc100-mt-1_46_1.lib(boost_unit_test_framework-vc100-mt-1_46_1.dll) : error LNK2005: "class boost::unit_test::master_test_suite_t & __cdecl boost::unit_test::framework::master_test_suite(void)" (?master_test_suite@framework@unit_test@boost@@YAAAVmaster_test_suite_t@23@XZ) already defined in libboost_unit_test_framework-vc100-mt-1_46_1.lib(framework.obj)
1>MSVCRT.lib(crtexew.obj) : error LNK2001: unresolved external symbol _WinMain@16
1>C:\Users\ga1009\Documents\dev\Oasis\Release\Test.exe : fatal error LNK1120: 1 unresolved externals
1>
1>Build FAILED.

我做错了什么,我该如何解决?

编辑:应用 Arne Mertz 的答案后,我得到:

1>------ Build started: Project: Test, Configuration: Release Win32 ------
1>Build started 30/01/2013 15:09:54.
1>InitializeBuildStatus:
1> Touching "Release\Test.unsuccessfulbuild".
1>ClCompile:
1> All outputs are up-to-date.
1>boost_unit_test_framework-vc100-mt-1_46_1.lib(boost_unit_test_framework-vc100-mt-1_46_1.dll) : error LNK2005: "class boost::unit_test::master_test_suite_t & __cdecl boost::unit_test::framework::master_test_suite(void)" (?master_test_suite@framework@unit_test@boost@@YAAAVmaster_test_suite_t@23@XZ) already defined in libboost_unit_test_framework-vc100-mt-1_46_1.lib(framework.obj)
1>C:\Users\ga1009\Documents\dev\Oasis\Release\Test.exe : fatal error LNK1169: one or more multiply defined symbols found
1>
1>Build FAILED.

最佳答案

检查项目的链接器属性:有一个属性 System/SubSystem 应该设置为 /SUBSYSTEM:CONSOLE

编辑:对于多重定义错误,您似乎遇到以下情况:在您的 testrunner.cpp 中,您定义了 BOOST_TEST_DYN_LINK,它会导致 boost_unit_test_framework-vc100-mt-1_46_1.dll 的动态链接,其中包含 master_test_suite 函数的定义。在另一个 .cpp 中,您没有定义该符号,因此它与 boost_unit_test_framework-vc100-mt-1_46_1.lib 静态链接,它与 dll 一起给出了两个定义。

解决方案:在包含 header 之前在每个源中使用#define

关于c++ - 在 Visual Studio 中提升测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14606815/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com