使用STL时的一个链接错误(error LNK2005:std::_Lockit::_Lockit(int)重定义)
2006-08-09 12:53
337 查看
今天编译一个使用STL的工程时遇到以下的链接错误:
PerformanceTest error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) 已经在 PetCore.lib(PetDib.obj) 中定义
PerformanceTest error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) 已经在 PetCore.lib(PetDib.obj) 中定义
PerformanceTest fatal error LNK1169: 找到一个或多个多重定义的符号
这个错误可能是由下面的原因造成的:
- 在一个项目中,部分链接到Debug CRT,部分链接到Release CRT
- 不是以同一种方式链接到CRT(/MT, /ML, /MD).
在我的工程中同时使用了/MTd和/MLd是产生这个链接错误的原因.
解决方法:
使项目中的所有工程使用同一个方式链接到CRT. 在VS2003中使用下面的方法修改CRT的链接方式:
打开工程属性页,修改"配置属性->C/C++->代码生成"中的"运行时库"项.
MSDN中关于CRT链接方式的文档:
PerformanceTest error LNK2005: "public: __thiscall std::_Lockit::_Lockit(int)" (??0_Lockit@std@@QAE@H@Z) 已经在 PetCore.lib(PetDib.obj) 中定义
PerformanceTest error LNK2005: "public: __thiscall std::_Lockit::~_Lockit(void)" (??1_Lockit@std@@QAE@XZ) 已经在 PetCore.lib(PetDib.obj) 中定义
PerformanceTest fatal error LNK1169: 找到一个或多个多重定义的符号
这个错误可能是由下面的原因造成的:
- 在一个项目中,部分链接到Debug CRT,部分链接到Release CRT
- 不是以同一种方式链接到CRT(/MT, /ML, /MD).
在我的工程中同时使用了/MTd和/MLd是产生这个链接错误的原因.
解决方法:
使项目中的所有工程使用同一个方式链接到CRT. 在VS2003中使用下面的方法修改CRT的链接方式:
打开工程属性页,修改"配置属性->C/C++->代码生成"中的"运行时库"项.
MSDN中关于CRT链接方式的文档:
Option | Description |
---|---|
/MD | Causes your application to use the multithread- and DLL-specific version of the run-time library. Defines _MT and _DLL and causes the compiler to place the library name MSVCRT.lib into the .obj file. Applications compiled with this option are statically linked to MSVCRT.lib. This library provides a layer of code that allows the linker to resolve external references. The actual working code is contained in MSVCR80.DLL, which must be available at run time to applications linked with MSVCRT.lib. When /MD is used with _STATIC_CPPLIB defined (/D_STATIC_CPPLIB) it will cause the application to link with the static multithread Standard C++ Library (libcpmt.lib) instead of the dynamic version (msvcprt.lib) while still dynamically linking to the main CRT via msvcrt.lib. |
/MDd | Defines _DEBUG, _MT, and _DLL and causes your application to use the debug multithread- and DLL-specific version of the run-time library. It also causes the compiler to place the library name MSVCRTD.lib into the .obj file. |
/MT | Causes your application to use the multithread, static version of the run-time library. Defines _MT and causes the compiler to place the library name LIBCMT.lib into the .obj file so that the linker will use LIBCMT.lib to resolve external symbols. |
/MTd | Defines _DEBUG and _MT. This option also causes the compiler to place the library name LIBCMTD.lib into the .obj file so that the linker will use LIBCMTD.lib to resolve external symbols. |
/LD | Creates a DLL. Passes the /DLL option to the linker. The linker looks for, but does not require, a DllMain function. If you do not write a DllMain function, the linker inserts a DllMain function that returns TRUE. Links the DLL startup code. Creates an import library (.lib), if an export (.exp) file is not specified on the command line; you link the import library to applications that call your DLL. Interprets /Fe (Name EXE File) as naming a DLL rather than an .exe file; the default program name becomes basename.dll instead of basename.exe. Implies /MT unless you explicitly specify /MD. |
/LDd | Creates a debug DLL. Defines _MT and _DEBUG. |
相关文章推荐
- 使用STL时的一个链接错误(error LNK2005:std::_Lockit::_Lockit(int)重定义)
- 使用Tinyxml.lib链接出错问题解决 (LIBCMT.lib(sprintf.obj) : error LNK2005: _sprintf_s 已经在 msvcrtd.lib(MSVCR80D.dll) 中定义)
- 使用Tinyxml.lib链接出错问题解决 (LIBCMT.lib(sprintf.obj) : error LNK2005: _sprintf_s 已经在 msvcrtd.lib(MSVCR80D.dll) 中定义)
- VS2005链接问题: LNK2005错误 :error LNK2005: _free 已经在 libcmtd.lib(dbgheap.obj) 中定义
- LNK2005错误 :error LNK2005: _free 已经在 libcmtd.lib(dbgheap.obj) 中定义
- 【链接库出错】:error LNK2005: "void __cdecl operator ***(void *)" (??3@YAXPAX@Z) 已经在xxxx.lib中定义
- 关于VS2008编译错误"error LNK2005: 已经在 .obj 中定义"
- VC编译错误: Nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12已经在dllmain.obj 中定义
- VC缺省链接库的顺序不对导致链接错误error LNK2005: "void __cdecl operator delete(void *)"
- VC缺省链接库的顺序不对导致链接错误error LNK2005: "void __cdecl operator delete(void *)"
- VS2008(VC6)出现如下链接错误的解决方法uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsign
- VC编译错误: Nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12已经在dllmain.obj 中定义
- uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案
- LNK2005错误 :error LNK2005: 已经在 libcmtd.lib(dbgheap.obj) 中定义
- VS2005(VC6)出现如下链接错误的解决方法uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsign
- 封装dll遇到的奇葩错误:error LNK2005: _DllMain@12 已经在 DLLMain.obj 中定义
- 【VS开发】uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案
- VC++编译错误: Nafxcwd.lib(dllmodul.obj) : error LNK2005: _DllMain@12已经在dllmain.obj 中定义
- 全局变量一定要在cpp中定义,在.h文件中定义会出现错误error LNK2005: "m_mysqlpack" ** already defined in**
- uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案