【问题标题】:Boost tests in Visual StudioVisual Studio 中的 Boost 测试
【发布时间】:2013-01-30 14:51:44
【问题描述】:

我正在尝试在 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.

【问题讨论】:

标签: c++ visual-studio-2010 boost-test


【解决方案1】:

检查项目的链接器属性:有一个属性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

【讨论】:

  • 这修复了未解决的外部问题,但它仍然以2&gt;C:\Users\ga1009\Documents\dev\Oasis\Release\Test.exe : fatal error LNK1169: one or more multiply defined symbols found 失败
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-10-25
  • 2011-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多