1. 程式人生 > >VC執行庫版本不同導致連結.LIB靜態庫時發生重複定義問題的一個案例分析和總結

VC執行庫版本不同導致連結.LIB靜態庫時發生重複定義問題的一個案例分析和總結

from:http://blog.csdn.net/ithzhang/article/details/13170047

Background

MSDN中對於在不同的配置下Link的LIB作了說明:

C Runtime Library:

開關

對應的庫

版本

/MD

MSVCRT.LIB

多執行緒DLL的Release版本

/MDd

MSVCRTD.LIB

多執行緒DLL的Debug版本

/MT

LIBCMT.LIB

多執行緒靜態連結的Release版本

/MTd

LIBCMTD.LIB

多執行緒靜態連結的Debug版本

/clr

MSVCMRT.LIB

託管程式碼和非託管程式碼混合

/clr:pure

MSVCURT.LIB

純託管程式碼

C++ Standard Library:

開關

對應的庫

版本

/MD

MSVCPRT.LIB

多執行緒DLL的Release版本

/MDd

MSVCPRTD.LIB

多執行緒DLL的Debug版本

/MT

LIBCPMT.LIB

多執行緒靜態連結的Release版本

/MTd

LIBCPMTD.LIB

多執行緒靜態連結的Debug版本

編譯器會自動根據編譯選項,選擇對應的LIB檔案。一般情況下這不會出現問題。

然而,在部分情況下,一旦你的程式的各個部分(LIB, OBJ…)並非由相同的編譯選項編譯出,而Link在一起的話,會出現各種各樣的看似很難解決的問題,這類問題主要以重複定義的錯誤形式存在,通常的解決方法也很簡單,就是選擇同樣的編譯選項進行編譯之後再Link。

Case Study

之前剛下載了ANTLR,在準備編譯它的Example的時候發現了下面的Build錯誤(我自己為這個例子建立了VS的專案,當前配置為動態連結Runtime庫,Debug版):

1>msvcprtd.lib(MSVCP80D.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)" (

[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::replace(unsigned int,unsigned int,char const *,unsigned int)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: void __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::reserve(unsigned int)" ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: unsigned int __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::length(void)const " ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@QBEIXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator+=(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.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> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: char const & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator[](unsigned int)const " ([email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator=(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator=(char const *)" ([email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.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> >(char const *)" ([email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > __cdecl std::operator+<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected]) already defined in antlr.lib(MismatchedCharException.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: char const * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str(void)const " ([email protected][email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@QBEPBDXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "bool __cdecl std::operator<<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::operator<<(class std::basic_ostream<char,struct std::char_traits<char> > & (__cdecl*)(class std::basic_ostream<char,struct std::char_traits<char> > &))" ([email protected][email protected]@[email protected]@@[email protected]@[email protected]@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::operator<<(int)" ([email protected][email protected]@[email protected]@@[email protected]@[email protected]@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)" ([email protected]@@[email protected][email protected]@[email protected]@@[email protected]@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: void __thiscall std::basic_ios<char,struct std::char_traits<char> >::setstate(int,bool)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: int __thiscall std::ios_base::width(int)" ([email protected][email protected]@@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: int __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::sputn(char const *,int)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static bool __cdecl std::char_traits<char>::eq_int_type(int const &,int const &)" ([email protected][email protected]@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static int __cdecl std::char_traits<char>::eof(void)" ([email protected][email protected]@[email protected]@SAHXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: int __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::sputc(char)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_streambuf<char,struct std::char_traits<char> > * __thiscall std::basic_ios<char,struct std::char_traits<char> >::rdbuf(void)const " ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: char __thiscall std::basic_ios<char,struct std::char_traits<char> >::fill(void)const " ([email protected][email protected][email protected]@[email protected]@@[email protected]@QBEDXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: int __thiscall std::ios_base::flags(void)const " ([email protected][email protected]@@QBEHXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: int __thiscall std::ios_base::width(void)const " ([email protected][email protected]@@QBEHXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static unsigned int __cdecl std::char_traits<char>::length(char const *)" ([email protected][email protected]@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > & __thiscall std::basic_ostream<char,struct std::char_traits<char> >::flush(void)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_ostream<char,struct std::char_traits<char> > * __thiscall std::basic_ios<char,struct std::char_traits<char> >::tie(void)const " ([email protected][email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@@[email protected]) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: bool __thiscall std::ios_base::good(void)const " ([email protected][email protected]@@QBE_NXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: void __thiscall std::basic_ostream<char,struct std::char_traits<char> >::_Osfx(void)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@QAEXXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: void __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::_Lock(void)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@QAEXXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: void __thiscall std::basic_streambuf<char,struct std::char_traits<char> >::_Unlock(void)" ([email protected][email protected][email protected]@[email protected]@@[email protected]@QAEXXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl std::operator<<<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_ostream<char,struct std::char_traits<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" ([email protected]@[email protected]@[email protected]@[email protected]@[email protected]@[email protected][email protected]@[email protected]@@[email protected]@[email protected][email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z) already defined in antlr.lib(LLkParser.obj)

1>libcpmt.lib(locale0.obj) : error LNK2005: "private: static class std::locale::_Locimp * __cdecl std::locale::_Getgloballocale(void)" ([email protected]@[email protected]@[email protected]@XZ) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(locale0.obj) : error LNK2005: "private: static void __cdecl std::locale::facet::facet_Register(class std::locale::facet *)" ([email protected]@[email protected]@@[email protected]@Z) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_dtor(class std::_Locinfo *)" ([email protected][email protected]@@[email protected]@Z) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(locale0.obj) : error LNK2005: "public: static void __cdecl std::_Locinfo::_Locinfo_ctor(class std::_Locinfo *,char const *)" ([email protected][email protected]@@[email protected]@Z) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(xmutex.obj) : error LNK2005: "public: void __thiscall std::_Mutex::_Lock(void)" ([email protected][email protected]@@QAEXXZ) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(xmutex.obj) : error LNK2005: "public: void __thiscall std::_Mutex::_Unlock(void)" ([email protected][email protected]@@QAEXXZ) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(xlock.obj) : error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" ([email protected]@@[email protected]@Z) already defined in msvcprtd.lib(MSVCP80D.dll)

1>libcpmt.lib(xlock.obj) : error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" ([email protected]@@[email protected]) already defined in msvcprtd.lib(MSVCP80D.dll)

1>LIBCMT.lib(invarg.obj) : error LNK2005: __invoke_watson already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(invarg.obj) : error LNK2005: __invalid_parameter already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(setlocal.obj) : error LNK2005: __configthreadlocale already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(tidtable.obj) : error LNK2005: __encode_pointer already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(tidtable.obj) : error LNK2005: __decode_pointer already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(tolower.obj) : error LNK2005: _tolower already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __amsg_exit already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __initterm_e already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0dat.obj) : error LNK2005: _exit already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __exit already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0dat.obj) : error LNK2005: __cexit already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(lconv.obj) : error LNK2005: _localeconv already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(mlock.obj) : error LNK2005: __unlock already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(mlock.obj) : error LNK2005: __lock already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(winxfltr.obj) : error LNK2005: __XcptFilter already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_a already defined in MSVCRTD.lib(cinitexe.obj)

1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xi_z already defined in MSVCRTD.lib(cinitexe.obj)

1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_a already defined in MSVCRTD.lib(cinitexe.obj)

1>LIBCMT.lib(crt0init.obj) : error LNK2005: ___xc_z already defined in MSVCRTD.lib(cinitexe.obj)

1>LIBCMT.lib(hooks.obj) : error LNK2005: "void __cdecl terminate(void)" ([email protected]@YAXXZ) already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(crt0.obj) : error LNK2005: _mainCRTStartup already defined in MSVCRTD.lib(crtexe.obj)

1>LIBCMT.lib(errmode.obj) : error LNK2005: ___set_app_type already defined in MSVCRTD.lib(MSVCR80D.dll)

1>LIBCMT.lib(_ctype.obj) : error LNK2005: _isprint already defined in MSVCRTD.lib(MSVCR80D.dll)

1>MSVCRTD.lib(MSVCR80D.dll) : error LNK2005: __stricmp already defined in LIBCMT.lib(stricmp.obj)

1>LINK : warning LNK4098: defaultlib 'MSVCRTD' conflicts with use of other libs; use /NODEFAULTLIB:library

1>LINK : warning LNK4098: defaultlib 'LIBCMT' conflicts with use of other libs; use /NODEFAULTLIB:library

1>D:/home/doc/Visual Studio 2005/Projects/tst_antlr_3/Debug/tst_antlr_3.exe : fatal error LNK1169: one or more multiply defined symbols found

分析一下錯誤來源,會發現:

1.     錯誤來源主要是重複定義的問題,而且重複定義的基本上都是VC Runtime和Standard C++ Library中的函式

2.     LIBCMT和LIBCPMT為Release下的Lib,本來不應該出現在Debug版本的連結的Lib中

3.     重複定義的問題主要出現在:LIBCMT, LIBCPMT, MSVCPRTD, MSVCRTD

來看看出問題的LIB是那些:

1.     LIBCMT:C Runtime庫的多執行緒靜態連結的Release版本

2.     LIBCPMT:C++ Standard Library的多執行緒靜態連結的Release版本

3.     MSVCPRTD:C++ Standard Library的多執行緒DLL的Debug版本

4.     MSVCRTD:C Runtime Library的多執行緒DLL的Debug版本

當前我們的配置是多執行緒DLL的Debug版,因此3和4是應該出現在link的列表中的,不屬於多餘。而後兩者則是隻是當多執行緒靜態連結Release版中才會出現。這提示我在專案中加入的ANTLR.LIB可能是造成這個問題的根源,因為靜態庫的編譯選項很容易和主程式發生衝突,並且根據實際資訊我們可以看出ANTLR.LIB應該是用多執行緒靜態連結的Release版本來編譯的。

這樣,解決方法就很清楚了:

1.     切換到Release,因為ANTLR.LIB是在Release下面編譯的

2.     把Run Time庫的版本修改成多執行緒靜態連結

做了這兩個修改之後編譯通過。

還有一種方法是,自己用多執行緒DLL的Debug版重新編譯一次ANTLR,生成一個新的ANTLRD.LIB,再link這個Lib也可以解決這個問題。

Summary

1.     知道各個不同的LIB代表的版本資訊非常重要,可以幫助快速定位問題

2.     在程式設計的時候,一定要把所有的專案的編譯選項(是靜態連結Runtime庫還是動態連結Runtime庫,Debug/Release)配置成一樣的。如果部分LIB/OBJ是由第三方提供(OBJ情況很少見),一般情況下只能調整自己的編譯選項,除非你可以要求第三方提供其他版本的編譯好的LIB

3.     在釋出可重用的靜態LIB庫供其他人呼叫的時候,最好對應不同的編譯選項,乃至VC版本,提供不同版本的LIB。VC自己的Runtime就是最好的例子。