Question

I am trying to compile trivial unit test project in Visual Studio 2010. I have a testrunner.cpp:

#define BOOST_TEST_DYN_LINK

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

and 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
}

This example worked fine in Linux, however here it fails to compile:

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.

What am I doing wrong and how can I fix it?

EDIT: After applying the answer from Arne Mertz, I get:

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.
Was it helpful?

Solution

Check your project's linker properties: There is a property System/SubSystem that should be set to /SUBSYSTEM:CONSOLE.

Edit: For the multiple definition error it seems you have the following situation: In your testrunner.cpp, you define BOOST_TEST_DYN_LINK wich leads to a dynamic linking of boost_unit_test_framework-vc100-mt-1_46_1.dll which contains a definition of the master_test_suite function. In the other .cpp, you do NOT define that symbol, so that one gets linked statically against boost_unit_test_framework-vc100-mt-1_46_1.lib which, together with the dll gives two definitions.

Solution: use the #define in every source before including the header.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top