今天编译一个使用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链接方式的文档:
| 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的工程时,遇到LNK2005错误,指出_std::_Lockit::_Lockit(int)和_std::_Lockit::~_Lockit(void)已在不同库文件中定义。此问题可能由于项目中部分链接到Debug CRT,部分链接到Release CRT,或者链接CRT的方式不一致(/MT, /ML, /MD)。解决方案是确保项目所有工程统一使用相同方式链接到CRT,例如在VS2003中更改“运行时库”设置。"
112468158,10541106,基于SH66P58单片机的智能体脂秤设计方案,"['单片机开发', '体脂秤方案', '抗静电设计', '嵌入式硬件', '健康监测']

被折叠的 条评论
为什么被折叠?



