45fan.com - 路饭网

搜索: 您的位置主页 > 网络频道 > 阅读资讯:如何解决使用STL时的一个链接错误问题?

如何解决使用STL时的一个链接错误问题?

2016-08-26 04:42:02 来源:www.45fan.com 【

如何解决使用STL时的一个链接错误问题?

今天编译一个使用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.



本文地址:http://www.45fan.com/a/question/67667.html
Tags: 一个 错误 STL
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部