iveney / googletest

Automatically exported from code.google.com/p/googletest
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

LNK2005 Linking Errors using gtest in VS2008 project #29

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1. Download gtest
2. Follow steps to upgrade solution/projects to VS2008
3. Create a new console application in VS2008
4. Add references to the gtest library/include files
5. Add the statement to run all google tests.

Instead of a successful build, you may receive a LNK2005 error in VS2008.

This happened to me as I was attempting to adopt Google test for our MFC-
based project.  I was able to solve the problem and wanted to post it here 
for others.

The following article provides additional information on LNK2005 errors.
http://cubicspot.blogspot.com/2007/06/solving-pesky-lnk2005-errors.html

Option #2 fixed my issue.  In "Properties..." under "C++->Code Generation-
>Runtime Library" Multi-threaded(/MT) was selected for the Release build 
and Multi-Threaded Debug (/MTd) was selected for the Debug build.  My 
project was set to Multi-threaded DLL (/MD) and Multi-threaded Debug DLL 
(/MDd) respectively.  Any mismatch has the potential to cause this error.

I am not an expert on the subject by any means, but wanted to provide what 
I know for others that may experience the issue.

Original issue reported on code.google.com by kidwell....@gmail.com on 5 Sep 2008 at 7:26

GoogleCodeExporter commented 9 years ago
I'm using VS2005. This solution doesn't work for me. After changing to /MD and 
/MDd, 
got many other linking errors (from gtest color example):

Error   1   error LNK2005: "private: static void __cdecl 
std::ios_base::_Ios_base_dtor(class std::ios_base *)" (?
_Ios_base_dtor@ios_base@std@@CAXPAV12@@Z) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   2   error LNK2005: "public: static void __cdecl 
std::ios_base::_Addstd(class std::ios_base *)" 
(?_Addstd@ios_base@std@@SAXPAV12@@Z) 
already defined in msvcprtd.lib(MSVCP80D.dll)   libcpmtd.lib    
Error   3   error LNK2005: "private: static class std::locale::_Locimp * __cdecl 
std::locale::_Getgloballocale(void)" (?
_Getgloballocale@locale@std@@CAPAV_Locimp@12@XZ) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   4   error LNK2005: "private: static class std::locale::_Locimp * __cdecl 
std::locale::_Init(void)" (?_Init@locale@std@@CAPAV_Locimp@12@XZ) already 
defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   5   error LNK2005: "private: static void __cdecl 
std::locale::facet::facet_Register(class std::locale::facet *)" (?
facet_Register@facet@locale@std@@CAXPAV123@@Z) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   6   error LNK2005: "public: static void __cdecl 
std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,char const *)" (?
_Locinfo_ctor@_Locinfo@std@@SAXPAV12@PBD@Z) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   7   error LNK2005: "public: static void __cdecl 
std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" (?
_Locinfo_dtor@_Locinfo@std@@SAXPAV12@@Z) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   8   error LNK2005: "void * __cdecl operator new(unsigned int,struct 
std::_DebugHeapTag_t const &,char *,int)" 
(??2@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z) 
already defined in msvcprtd.lib(MSVCP80D.dll)   libcpmtd.lib    
Error   9   error LNK2005: "void * __cdecl operator new[](unsigned int,struct 
std::_DebugHeapTag_t const &,char *,int)" 
(??_U@YAPAXIABU_DebugHeapTag_t@std@@PADH@Z) 
already defined in msvcprtd.lib(MSVCP80D.dll)   libcpmtd.lib    
Error   10  error LNK2005: "struct std::_DebugHeapTag_t const & __cdecl 
std::_DebugHeapTag_func(void)" 
(?_DebugHeapTag_func@std@@YAABU_DebugHeapTag_t@1@XZ) 
already defined in msvcprtd.lib(MSVCP80D.dll)   libcpmtd.lib    
Error   11  error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??
0_Lockit@std@@QAE@H@Z) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   12  error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??
1_Lockit@std@@QAE@XZ) already defined in 
msvcprtd.lib(MSVCP80D.dll)  libcpmtd.lib    
Error   13  error LNK2005: __configthreadlocale already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   14  error LNK2005: _free already defined in MSVCRTD.lib(MSVCR80D.dll)   
LIBCMTD.lib 
Error   15  error LNK2005: __CrtSetCheckCount already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   16  error LNK2005: "public: __thiscall std::exception::exception(void)" 
(??0exception@std@@QAE@XZ) already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   17  error LNK2005: "public: __thiscall std::exception::exception(char 
const * const &)" (??0exception@std@@QAE@ABQBD@Z) already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   18  error LNK2005: "public: __thiscall std::exception::exception(class 
std::exception const &)" (??0exception@std@@QAE@ABV01@@Z) already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   19  error LNK2005: "public: virtual __thiscall 
std::exception::~exception(void)" (??1exception@std@@UAE@XZ) already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   20  error LNK2005: "public: virtual char const * __thiscall 
std::exception::what(void)const " (?what@exception@std@@UBEPBDXZ) already 
defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   21  error LNK2005: "public: __thiscall std::bad_cast::bad_cast(char const 
*)" (??0bad_cast@std@@QAE@PBD@Z) already defined in MSVCRTD.lib(MSVCR80D.dll)   
LIBCMTD.lib 
Error   22  error LNK2005: "public: __thiscall std::bad_cast::bad_cast(class 
std::bad_cast const &)" (??0bad_cast@std@@QAE@ABV01@@Z) already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   23  error LNK2005: "public: virtual __thiscall 
std::bad_cast::~bad_cast(void)" (??1bad_cast@std@@UAE@XZ) already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   24  error LNK2005: _localeconv already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   25  error LNK2005: __encode_pointer already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   26  error LNK2005: __decode_pointer already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   27  error LNK2005: __crt_debugger_hook already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   28  error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR80D.dll)   
LIBCMTD.lib 
Error   29  error LNK2005: __exit already defined in MSVCRTD.lib(MSVCR80D.dll)  
LIBCMTD.lib 
Error   30  error LNK2005: __cexit already defined in MSVCRTD.lib(MSVCR80D.dll) 
LIBCMTD.lib 
Error   31  error LNK2005: __amsg_exit already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   32  error LNK2005: __initterm_e already defined in 
MSVCRTD.lib(MSVCR80D.dll)   LIBCMTD.lib 
Error   33  error LNK2005: __stricmp already defined in MSVCRTD.lib(MSVCR80D.dll)   
LIBCMTD.lib 

..................................
..................................

Original comment by ping....@gmail.com on 10 Sep 2008 at 4:19

GoogleCodeExporter commented 9 years ago
I think this is fixed in the svn head.  Please wait for the next release of 
gtest or
check out the svn head revision directly.

Original comment by shiq...@gmail.com on 10 Sep 2008 at 6:18

GoogleCodeExporter commented 9 years ago
The issue is still there in the 1.6.0 version.

Steps to reproduce:
1. downloaded the googletest 1.6.0 version
2. converted the project to VS2008 and built it
3. build my test project with /MTd, linking to gtestd.lib and gtest_maind.lib
4. got errors:

Linking...
msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __thiscall 
std::basic_stringstream<char,struct std::char_traits<char>,class 
std::allocator<char> >::basic_stringstream<char,struct 
std::char_traits<char>,class std::allocator<char> >(int)" 
(??0?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@H@Z) 
already defined in gtestd.lib(gtest-all.obj)
msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: void __thiscall 
std::basic_stringstream<char,struct std::char_traits<char>,class 
std::allocator<char> >::`vbase destructor'(void)" 
(??_D?$basic_stringstream@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEXXZ) 
already defined in gtestd.lib(gtest-all.obj)
msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: char const * __thiscall 
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> 
>::c_str(void)const " 
(?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ) 
already defined in gtestd.lib(gtest-all.obj)
msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: __thiscall 
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> 
>::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> 
>(void)" 
(??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) 
already defined in gtestd.lib(gtest-all.obj)
msvcprtd.lib(MSVCP90D.dll) : error LNK2005: "public: class std::locale::facet * 
__thiscall std::locale::facet::_Decref(void)" 
(?_Decref@facet@locale@std@@QAEPAV123@XZ) already defined in 
gtestd.lib(gtest-all.obj)
libcpmtd.lib(ios.obj) : error LNK2005: "private: static void __cdecl 
std::ios_base::_Ios_base_dtor(class std::ios_base *)" 
(?_Ios_base_dtor@ios_base@std@@CAXPAV12@@Z) already defined in 
msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(ios.obj) : error LNK2005: "public: static void __cdecl 
std::ios_base::_Addstd(class std::ios_base *)" 
(?_Addstd@ios_base@std@@SAXPAV12@@Z) already defined in 
msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(locale0.obj) : error LNK2005: "void __cdecl _AtModuleExit(void 
(__cdecl*)(void))" (?_AtModuleExit@@YAXP6AXXZ@Z) already defined in 
msvcprtd.lib(locale0_implib.obj)
libcpmtd.lib(locale0.obj) : error LNK2005: __Fac_tidy already defined in 
msvcprtd.lib(locale0_implib.obj)
libcpmtd.lib(locale0.obj) : error LNK2005: "private: static void __cdecl 
std::locale::facet::facet_Register(class std::locale::facet *)" 
(?facet_Register@facet@locale@std@@CAXPAV123@@Z) already defined in 
msvcprtd.lib(locale0_implib.obj)
libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class 
std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" 
(?_Getgloballocale@locale@std@@CAPAV_Locimp@12@XZ) already defined in 
msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(locale0.obj) : error LNK2005: "private: static class 
std::locale::_Locimp * __cdecl std::locale::_Init(void)" 
(?_Init@locale@std@@CAPAV_Locimp@12@XZ) already defined in 
msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl 
std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,class 
std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> 
> const &)" 
(?_Locinfo_ctor@_Locinfo@std@@SAXPAV12@ABV?$basic_string@DU?$char_traits@D@std@@
V?$allocator@D@2@@2@@Z) already defined in msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(locale0.obj) : error LNK2005: "public: static void __cdecl 
std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" 
(?_Locinfo_dtor@_Locinfo@std@@SAXPAV12@@Z) already defined in 
msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(xlock.obj) : error LNK2005: "public: __thiscall 
std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) already defined in 
msvcprtd.lib(MSVCP90D.dll)
libcpmtd.lib(xlock.obj) : error LNK2005: "public: __thiscall 
std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) already defined in 
msvcprtd.lib(MSVCP90D.dll)
LIBCMTD.lib(_file.obj) : error LNK2005: ___iob_func already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(osfinfo.obj) : error LNK2005: __open_osfhandle already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(dosmap.obj) : error LNK2005: __errno already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(tidtable.obj) : error LNK2005: __encode_pointer already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(tidtable.obj) : error LNK2005: __decode_pointer already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(fflush.obj) : error LNK2005: _fflush already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(lconv.obj) : error LNK2005: _localeconv already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(crt0dat.obj) : error LNK2005: _exit already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(crt0dat.obj) : error LNK2005: __exit already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(invarg.obj) : error LNK2005: __invalid_parameter already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(mlock.obj) : error LNK2005: __lock already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(mlock.obj) : error LNK2005: __unlock already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(dbghook.obj) : error LNK2005: __crt_debugger_hook already defined 
in MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(stricmp.obj) : error LNK2005: __stricmp already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)" 
(?terminate@@YAXXZ) already defined in MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(sprintf.obj) : error LNK2005: _sprintf_s already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(getenv.obj) : error LNK2005: _getenv already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(strtol.obj) : error LNK2005: _strtol already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(strtoq.obj) : error LNK2005: __strtoui64 already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(dbgrptw.obj) : error LNK2005: __CrtDbgReportW already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(vsnprnc.obj) : error LNK2005: __vsnprintf_s already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(printf.obj) : error LNK2005: _printf already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LIBCMTD.lib(vprintf.obj) : error LNK2005: _vprintf already defined in 
MSVCRTD.lib(MSVCR90D.dll)
LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; 
use /NODEFAULTLIB:library
C:\XXXX : fatal error LNK1169: one or more multiply defined symbols found

Error executing link.exe (tool returned code: 1169)

Original comment by roman.we...@gmail.com on 2 Aug 2011 at 7:54