C++ 错误 LNK2005:新建和删除已在 LIBCMTD.lib(new.obj) 中定义
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1146338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
error LNK2005: new and delete already defined in LIBCMTD.lib(new.obj)
提问by ali
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.
我有一个包含两个项目的 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)
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.
我不知道如何克服这一点。有人可以解释为什么会发生此错误。任何概述 .lib 文件链接的解释都将受到高度赞赏。
回答by ali
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
CRT 库对 new、delete 和 DllMain 函数使用弱外部链接。MFC 库还包含新建、删除和 DllMain 函数。这些函数需要先链接 MFC 库,然后才能链接 CRT 库。 http://support.microsoft.com/kb/148652
Solution based on VS2005 (Replace Nafxcwd.lib with Uafxcwd.lib for ~VS2013)
基于VS2005的解决方案(~VS2013用Uafxcwd.lib替换Nafxcwd.lib)
go to project>properties>configuration properties>linker>input
转到项目>属性>配置属性>链接器>输入
add to "Additional dependency" -> Nafxcwd.lib Libcmtd.lib
添加到“附加依赖项”-> Nafxcwd.lib Libcmtd.lib
add to "ignore specific library" -> Nafxcwd.lib;Libcmtd.lib
添加到“忽略特定库”-> Nafxcwd.lib;Libcmtd.lib
order of libraries is important( Nafxcwd.lib;Libcmtd.lib).
库的顺序很重要(Nafxcwd.lib;Libcmtd.lib)。
回答by Andrew Jens
One thing to try is to make sure you have:
要尝试的一件事是确保您拥有:
#include "stdafx.h"
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.
作为 .cpp 文件中的第一行。我敢肯定这不是所有情况下的答案,但在我的情况下,它使相同的错误消失了。
回答by user2199593
be sure that you have #include <afx.h>
in "stdafx.h"
BEFORE other includes like #include <string>
确保你#include <afx.h>
在"stdafx.h"
其他包括之前有#include <string>
回答by Cipher Saw
I meet this problem in a MFC solution of Visual Studio 2010, while changing Use MFC in a Shared DLL
into Use MFC in a Static Library
in Project -> Properties -> Configuration Properties -> General.
我满足这个问题Visual Studio 2010中的MFC的解决方案,同时改变Use MFC in a Shared DLL
成Use MFC in a Static Library
在项目- >属性- >配置属性- >常规。
I solve the problem by the following ways, please locate Project -> Properties -> Configuration Properties -> Linker -> Inputat first.
我通过以下方式解决问题,请先找到项目->属性->配置属性->链接器->输入。
In Debug mode:
在调试模式下:
- Add
uafxcwd.lib;Libcmtd.lib
in Additional Dependencies. - Add
uafxcwd.lib;Libcmtd.lib
in Ignore Specific Default Libraries.
- 添加
uafxcwd.lib;Libcmtd.lib
在附加依赖。 - 添加
uafxcwd.lib;Libcmtd.lib
在忽略特定的默认库。
In Release mode:
在发布模式下:
- Add
uafxcw.lib;Libcmt.lib
in Additional Dependencies. - Add
uafxcw.lib;Libcmt.lib
in Ignore Specific Default Libraries.
- 添加
uafxcw.lib;Libcmt.lib
在附加依赖。 - 添加
uafxcw.lib;Libcmt.lib
在忽略特定的默认库。
Notice:
注意:
- Don't miss the
;
between the two.lib
files. - A suffix
-d
must be added in the files in Debug mode.
- 不要错过
;
两个.lib
文件之间的。 -d
必须在调试模式下的文件中添加后缀。
回答by eit
in config linker input
在配置链接器输入中
- In additional dependicies put uafxcw.lib;LIBCMT.lib
- In Ignore specific put put uafxcw.lib;LIBCMT.lib
- 在附加依赖项中放入uafxcw.lib;LIBCMT.lib
- 在 Ignore specific put put uafxcw.lib;LIBCMT.lib
回答by msvcyc
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.
确保您要链接的 C++ 运行时库与静态库和可执行文件上的相同。检查您的项目属性 C/C++-> 代码生成-> 运行时库设置。
回答by Gob00st
Typo. One stupid way you got that is instead of include the header, you inlucde the cpp. e.g.
错字。你得到的一种愚蠢的方法是不包含标题,而是包含 cpp。例如
#include <myclass.cpp> //should be #include <myClass.h>
回答by Mark Laff
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.
首先,libcmtd.lib 用于调试版本,而 libcmt.lib 用于生产。仔细检查您是否不包括两者。要检查的一个地方是配置属性/链接器项目属性的“命令行”部分。
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.
如果您转到项目的属性,并打开配置属性/链接器/输入部分,您可以“Ingore 特定库”...尝试在该字段中列出 libcmtd.lib。
回答by boutinr
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.
对我来说,我有一个用_CRTDBG_MAP_ALLOC 编译的静态库,而应用程序不是用_CRTDBG_MAP_ALLOC 编译的,我当时收到的是LNK2005。我已将应用程序更改为使用 _CRTDBG_MAP_ALLOC 进行编译,并且 LNK2005 消失了。
回答by Melardev
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!
我用 VS2017 创建了两个新项目,一个在工作,另一个没有,所以我比较了不同之处。一个工作是用File > New Project > Visual C++ > MFC/ATL > MFC Application
一个不工作创建的,File > New Project > Visual C++ > Windows Desktop > Windows Desktop Wizard
然后添加 MFC。在这两种情况下,我都使用 MFC 作为静态库。我想出了两个修复方法。但在此之前我们必须添加导入,因为第二个项目没有!
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
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.- I noticed the working project had some imports in stdafx.h, I copied them into pch.h in the other project, it worked.(Keeping the properties unchanged, so static lib was used). The code copied was this:
Project > Properties > Configuration Properties > General > Use of MFC
将它设置为在共享 DLL 中使用,这也应该自动设置C/C++ > Code Generation > Runtime Library
以Multi-threaded debug dll /MDd
确保它确实做到了。现在尝试编译,对我来说它有效。- 我注意到工作项目在 stdafx.h 中有一些导入,我将它们复制到另一个项目中的 pch.h 中,它起作用了。(保持属性不变,因此使用静态库)。复制的代码是这样的:
#define _ATL_CSTRING_EXPLICIT_CONSTRUCTORS // some CString constructors will be explicit
// turns off MFC's hiding of some common and often safely ignored warning messages
#define _AFX_ALL_WARNINGS
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
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??
更改链接器设置的其他解决方案我尝试过,但它们不起作用。
如果有人知道我的解决方案为什么有效,我将不胜感激,这很奇怪,为什么在pch.h
解决链接器问题时包含这些标头,而在其他任何地方包含相同的标头会触发该错误?