在Visual Studio中进行增强测试

Grz*_*nio 1 c++ visual-studio-2010 boost-test

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

#define BOOST_TEST_DYN_LINK

#define BOOST_TEST_MODULE "BaumWelch Unit Tests"
#include <boost/test/unit_test.hpp>
Run Code Online (Sandbox Code Playgroud)

和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
}
Run Code Online (Sandbox Code Playgroud)

这个例子在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.
Run Code Online (Sandbox Code Playgroud)

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

编辑:在应用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.
Run Code Online (Sandbox Code Playgroud)

Arn*_*rtz 5

检查项目的链接器属性:有一个属性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一起提供两个定义.

解决方案:#define在包含标头之前使用每个源中的内容.