Chinaunix首页 | 论坛 | 博客
  • 博客访问: 110226
  • 博文数量: 19
  • 博客积分: 419
  • 博客等级: 一等列兵
  • 技术积分: 265
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-03 20:13
文章分类

全部博文(19)

文章存档

2012年(6)

2011年(13)

分类: C/C++

2012-04-10 15:18:35

VC运行库版本不同导致链接.LIB静态库时发生重复定义问题的一个案例分析和总结(zz)
2009-03-06 2:48

感谢作者:http://blog.csdn.net/ATField/archive/2007/03/11/1526253.aspx

MSDN中对于在不同的配置下LinkLIB作了说明:

C Runtime Library

开关

对应的库

版本

/MD

MSVCRT.LIB

多线程DLLRelease版本

/MDd

MSVCRTD.LIB

多线程DLLDebug版本

/MT

LIBCMT.LIB

多线程静态链接的Release版本

/MTd

LIBCMTD.LIB

多线程静态链接的Debug版本

/clr

MSVCMRT.LIB

托管代码和非托管代码混合

/clr:pure

MSVCURT.LIB

纯托管代码

C++ Standard Library

开关

对应的库

版本

/MD

MSVCPRT.LIB

多线程DLLRelease版本

/MDd

MSVCPRTD.LIB

多线程DLLDebug版本

/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,class std::allocator >::~basic_string,class std::allocator >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string,class std::allocator > & __thiscall std::basic_string,class std::allocator >::replace(unsigned int,unsigned int,char const *,unsigned int)" (?replace@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV12@IIPBDI@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: void __thiscall std::basic_string,class std::allocator >::reserve(unsigned int)" (?reserve@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEXI@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: unsigned int __thiscall std::basic_string,class std::allocator >::length(void)const " (?length@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEIXZ) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string,class std::allocator > & __thiscall std::basic_string,class std::allocator >::operator+=(class std::basic_string,class std::allocator > const &)" (??Y?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@ABV01@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: __thiscall std::basic_string,class std::allocator >::basic_string,class std::allocator >(class std::basic_string,class std::allocator > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: char const & __thiscall std::basic_string,class std::allocator >::operator[](unsigned int)const " (??A?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEABDI@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string,class std::allocator > & __thiscall std::basic_string,class std::allocator >::operator=(class std::basic_string,class std::allocator > const &)" (??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@ABV01@@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string,class std::allocator > & __thiscall std::basic_string,class std::allocator >::operator=(char const *)" (??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z) already defined in antlr.lib(CharScanner.obj)

1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: __thiscall std::basic_string,class std::allocator >::basic_string,class std::allocator >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in antlr.lib(CharScanner.obj)

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 RuntimeStandard C++ Library中的函数

2.     LIBCMTLIBCPMTRelease下的Lib,本来不应该出现在Debug版本的链接的Lib

3.     重复定义的问题主要出现在:LIBCMT, LIBCPMT, MSVCPRTD, MSVCRTD

来看看出问题的LIB是那些:

1.     LIBCMTC Runtime库的多线程静态链接的Release版本

2.     LIBCPMTC++ Standard Library的多线程静态链接的Release版本

3.     MSVCPRTDC++ Standard Library的多线程DLLDebug版本

4.     MSVCRTDC Runtime Library的多线程DLLDebug版本

当前我们的配置是多线程DLLDebug版,因此34是应该出现在link的列表中的,不属于多余。而后两者则是只是当多线程静态链接Release版中才会出现。这提示我在项目中加入的ANTLR.LIB可能是造成这个问题的根源,因为静态库的编译选项很容易和主程序发生冲突,并且根据实际信息我们可以看出ANTLR.LIB应该是用多线程静态链接的Release版本来编译的。

这样,解决方法就很清楚了:

1.     切换到Release,因为ANTLR.LIB是在Release下面编译的

2.     Run Time库的版本修改成多线程静态链接

   (你用什么方式编译库文件就要用什么方式来编译引用库)

做了这两个修改之后编译通过。

还有一种方法是,自己用多线程DLLDebug版重新编译一次ANTLR,生成一个新的ANTLRD.LIB,再link这个Lib也可以解决这个问题。

Summary

1.     知道各个不同的LIB代表的版本信息非常重要,可以帮助快速定位问题

2.     在编程的时候,一定要把所有的项目的编译选项(是静态链接Runtime库还是动态链接Runtime库,Debug/Release)配置成一样的。如果部分LIB/OBJ是由第三方提供(OBJ情况很少见),一般情况下只能调整自己的编译选项,除非你可以要求第三方提供其他版本的编译好的LIB

3.     在发布可重用的静态LIB库供其他人调用的时候,最好对应不同的编译选项,乃至VC版本,提供不同版本的LIBVC自己的Runtime就是最好的例子。


阅读(3073) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~