错误LNK2005:LIBCMTD.lib(new.obj)中已定义new和delete
我有一个包含两个项目的 Visual Studio 2005 解决方案。 一个是静态库,另一个是用于测试静态库中的功能的可执行文件。 静态库使用MFC。 当我构建解决方案时,出现以下错误。
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (?? 3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
我不知道如何克服这个问题。 有人可以解释一下为什么会出现此错误吗? 任何概述 .lib 文件链接的解释都将受到高度赞赏。
I have a Visual studio 2005 solution that has two projects. One is a static library and the other is a executable used to test the features in the static library. The static library uses MFC. I got the following errors when I built the solution.
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (?? 3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmtd.lib(newaop.obj)
uafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
I do not know how to overcome this. Can some one please explain why this error is occuring. Any explanation that gives an overview of .lib files linkage will be highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
CRT 库对 new、delete 和 DllMain 函数使用弱外部链接。 MFC 库还包含 new、delete 和 DllMain 函数。 这些函数需要在链接 CRT 库之前链接 MFC 库。
http://support.microsoft.com/kb/148652
基于VS2005的解决方案(替换Nafxcwd. lib 和 Uafxcwd.lib for ~VS2013)
转到项目>属性>配置属性>链接器>输入
添加到“附加依赖项” -> Nafxcwd.lib Libcmtd.lib
添加到“忽略特定库”-> Nafxcwd.lib;Libcmtd.lib
库的顺序很重要(Nafxcwd.lib;Libcmtd.lib)。
The CRT libraries use weak external linkage for the new, delete, and DllMain functions. The MFC libraries also contain new, delete, and DllMain functions. These functions require the MFC libraries to be linked before the CRT library is linked.
http://support.microsoft.com/kb/148652
Solution based on VS2005 (Replace Nafxcwd.lib with Uafxcwd.lib for ~VS2013)
go to project>properties>configuration properties>linker>input
add to "Additional dependency" -> Nafxcwd.lib Libcmtd.lib
add to "ignore specific library" -> Nafxcwd.lib;Libcmtd.lib
order of libraries is important( Nafxcwd.lib;Libcmtd.lib).
要尝试的一件事是确保将:
作为 .cpp 文件中的第一行。 我确信这不是所有情况下的答案,但它使相同的错误在我的情况下消失。
One thing to try is to make sure you have:
as the first line in your .cpp files. I'm sure that's not the answer in all cases, but it made the identical error go away in my case.
我在Visual Studio 2010的MFC解决方案中遇到了这个问题,同时将
在共享DLL中使用MFC
更改为在静态库中使用MFC
项目-> 属性-> 配置属性-> 一般。我通过以下方式解决了问题,请找到项目-> 属性-> 配置属性-> 链接器-> 首先输入。
在调试模式下:
uafxcwd.lib;Libcmtd.lib
。uafxcwd.lib;Libcmtd.lib
。在发布模式下:
uafxcw.lib;Libcmt.lib
。uafxcw.lib;Libcmt.lib
。注意:
.lib
文件之间的;
。-d
。I meet this problem in a MFC solution of Visual Studio 2010, while changing
Use MFC in a Shared DLL
intoUse MFC in a Static Library
in Project -> Properties -> Configuration Properties -> General.I solve the problem by the following ways, please locate Project -> Properties -> Configuration Properties -> Linker -> Input at first.
In Debug mode:
uafxcwd.lib;Libcmtd.lib
in Additional Dependencies.uafxcwd.lib;Libcmtd.lib
in Ignore Specific Default Libraries.In Release mode:
uafxcw.lib;Libcmt.lib
in Additional Dependencies.uafxcw.lib;Libcmt.lib
in Ignore Specific Default Libraries.Notice:
;
between the two.lib
files.-d
must be added in the files in Debug mode.确保在
"stdafx.h"
中包含#include
,然后再添加其他包含内容,例如#include
be sure that you have
#include <afx.h>
in"stdafx.h"
BEFORE other includes like#include <string>
在配置链接器输入中
in config linker input
确保您链接的 C++ 运行时库与静态库和可执行文件上的相同。 检查您的项目属性 C/C++ -> 代码生成 -> 运行时库设置。
Make sure the C++ runtime library that you are linking with is the same on your static library as well as your executable. Check your project properties C/C++->Code generation->runtime library settings.
错字。 一种愚蠢的方法是不包含标头,而是包含 cpp。
例如
Typo. One stupid way you got that is instead of include the header, you inlucde the cpp.
e.g.
首先,libcmtd.lib 用于调试版本,libcmt.lib 用于生产版本。 仔细检查您是否没有同时包含两者。 要检查的地方之一是配置属性/链接器项目属性的“命令行”部分。
如果您转到项目的属性,并打开配置属性/链接器/输入部分,您可以“Ingore Specific Library”...尝试在该字段中列出 libcmtd.lib。
First, libcmtd.lib is for a debug version and libcmt.lib is for production. Double-check that you're not including both. One place to check is the "Command Line" section of the Configuration Properties/Linker project properties.
If you go to the properties for the project, and open up the Configuration Properties/Linker/Input section, you can "Ingore Specific Library"...try listing libcmtd.lib in that field.
对我来说,我有一个用 _CRTDBG_MAP_ALLOC 编译的静态库,并且应用程序不是用 _CRTDBG_MAP_ALLOC 编译的,我当时收到的是 LNK2005。 我已将应用程序更改为使用 _CRTDBG_MAP_ALLOC 进行编译,并且 LNK2005 消失了。
For me, I have a static library compiled with _CRTDBG_MAP_ALLOC, and the application not compiled with _CRTDBG_MAP_ALLOC, I was receiving then LNK2005. I've changed the application to compile with _CRTDBG_MAP_ALLOC, and the LNK2005 disappear.
解决了这个问题
uafxcw.lib
。uafxcw.lib
。Got rid of the problem
uafxcw.lib
.uafxcw.lib
.我用 VS2017 创建了两个新项目,一个可以运行,另一个不能运行,所以我比较了有什么区别。 该工作是使用
File > 创建的 新项目> 视觉C++> MFC/ATL> MFC 应用程序
无法正常工作的应用程序是使用
File > 创建的 新项目> 视觉C++> Windows 桌面 > Windows 桌面向导
然后添加 MFC。 在这两种情况下,我都使用 MFC 作为静态库。 我已经想出了两个解决办法。 但在此之前我们必须添加导入,因为第二个项目没有导入!
现在这两个修复中的任何一个对我都有效:
Project > 属性> 配置属性> 一般> 使用MFC
将其设置为在共享DLL中使用,这也应该自动设置C/C++> 代码生成> 运行时库
到多线程调试dll /MDd
确保它确实做到了这一点。现在尝试编译,对我来说它有效。
更改链接器设置的其他解决方案我尝试了它们,但它们不起作用。
如果有人知道我的解决方案为何有效,我将不胜感激,这很奇怪,为什么在
pch.h
中包含这些标头可以解决链接器问题,而在其他地方包含相同的标头会触发该错误?I had created two fresh projects with VS2017, one was working the other not, so I compared what was the difference. The one working was created with
File > New Project > Visual C++ > MFC/ATL > MFC Application
the one not working was created with
File > New Project > Visual C++ > Windows Desktop > Windows Desktop Wizard
then adding MFC. In both cases I was using MFC as static lib. I had figured out two fixes. But before that we have to add imports because the second project had NONE!
Now either of the two fixes worked for me:
Project > Properties > Configuration Properties > General > Use of MFC
set it to use in a Shared DLL, this should also automatically setC/C++ > Code Generation > Runtime Library
toMulti-threaded debug dll /MDd
make sure it indeed did that.Try compile now, for me it worked.
The other solutions changing Linker settings I tried them but they did not work.
I would appreciate if somebody knows why my solution works, it is weird, why including those headers in
pch.h
solves a linker issue whereas including those same headers anywhere else triggers that error??检查两个项目的清单文件,确保它们链接相同版本的标准库。 最有可能不是,检查属性->代码生成->标准库链接。
Check the manifest file of both projects, make sure that they are linking the same version of the standard library. Most likely they are not, check the properties->code generation->standard library linking.
我也有类似的问题。 唐尼给出的链接解释了原因。 解决方案是查看错误消息,然后删除涉及的那些库,并按照先 MFC 库、然后 CRT 库的顺序添加这些库。
vs2008中的实现方式阿里给出了。
I also had a similar problem. The link given by Donnie explains the the reason. The solution was to look at the error messages and then removing those libs involved and adding those libs in the order of MFC libs first and then CRT libs.
The way to do that in vs2008 is given by ali.
我还要补充一点,如果您替换了 new/delete 运算符(如果是,请同时替换数组和标量),您可能需要将它们标记为 __forceinline ,以便 obj 不会与 lib 发生冲突。
例如,我这样做是为了强制对齐分配,并且在我这样做之前遇到了同样的问题:
I will also add that if you have replaced the new/delete operators (and if so, please do the array and the scalar both), you may need to tag them as __forceinline so that the obj doesn't collide with the lib.
For example, I did these to force aligned allocations and had the same trouble until I did that:
声明并定义变量的头文件。 可能的解决方案包括:
在.h中声明变量:extern BOOL MyBool; 然后在 .c 或 .cpp 文件中对其进行赋值:BOOL MyBool = FALSE;。
将变量声明为静态。
声明变量 selectany。
https://msdn.microsoft.com/en-us/library/72zdcz6f。 ASPX
A header file declared and defined a variable. Possible solutions include:
Declare the variable in .h: extern BOOL MyBool; and then assign to it in a .c or .cpp file: BOOL MyBool = FALSE;.
Declare the variable static.
Declare the variable selectany.
https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx
对我来说,问题是通过改变解决的
设置为“使用标准 Windows 库”之前,
我还必须在下设置 /MD 选项
For me the problem was solved by changing
Before it was set to "Use Standard Windows Libraries"
Additionally I had to set the /MD option under
我在搜索此答案时遇到的另一个可能原因是:
我不小心在从应用程序(使用预编译的headers)到共享静态库(不使用预编译头)。
Another possible cause that I ran across while searching for this answer:
I accidentally left an
#include "StdAfx.h"
line at the top of a .cpp file that I moved from the application (which uses precompiled headers) into a shared static library (which doesn't use precompiled headers).将 Cipher Saw 的解决方案应用于 vs2015 后出现错误
可以通过将库列表从
uafxcw.lib;Libcmt.lib
更改为afxnmcdd.lib;uafxcwd.lib;Libcmtd.lib
来修复这些 错误code>(调试 unicode 构建)Got errors after applying Cipher Saw's solution to vs2015
Was able to fix them by changing libs list from
uafxcw.lib;Libcmt.lib
toafxnmcdd.lib;uafxcwd.lib;Libcmtd.lib
(debug unicode build)