您的位置:首页 > 移动开发

使用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链接方式的文档:

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.

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐