error LNK2005: new and delete already defined in LIBCMTD.lib(new.obj)
Solution 1
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).
Solution 2
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.
Solution 3
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.
I solve the problem by the following ways, please locate Project -> Properties -> Configuration Properties -> Linker -> Input at first.
In Debug mode:
- Add
uafxcwd.lib;Libcmtd.lib
in Additional Dependencies. - Add
uafxcwd.lib;Libcmtd.lib
in Ignore Specific Default Libraries.
In Release mode:
- Add
uafxcw.lib;Libcmt.lib
in Additional Dependencies. - Add
uafxcw.lib;Libcmt.lib
in Ignore Specific Default Libraries.
Notice:
- Don't miss the
;
between the two.lib
files. - A suffix
-d
must be added in the files in Debug mode.
Solution 4
be sure that you have #include <afx.h>
in "stdafx.h"
BEFORE other includes like #include <string>
Solution 5
in config linker input
- In additional dependicies put uafxcw.lib;LIBCMT.lib
- In Ignore specific put put uafxcw.lib;LIBCMT.lib
Related videos on Youtube
Helen
Updated on February 15, 2022Comments
-
Helen over 2 years
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.
-
Donnie DeBoer almost 15 yearsIs the other (non-static-library) project a CRT project?
-
Donnie DeBoer almost 15 yearsIf the other (non static library) project is a CRT project, check out this link: support.microsoft.com/kb/148652
-
Synetech about 12 yearsI got this error trying to compile a console app from the command-line with
cl
. I didn’t have it until I#includ
ed one of my libraries that uses MFC’sCString
. Thanks to the KB article, I just moved my#include
up and the problem was resolved.
-
-
Nav about 11 yearsCorrect. This happened in my case. I kept all the function and class definitions in the cpp, and got this error. When I shifted the definitions to a hpp file, the errors went away. Weird!
-
grim over 10 yearsIf you're compiling with unicode you want to use uafxcwd.lib instead of nafxcwd.lib. Note that the d is only for debug builds as well. In you're release configuration use uafxcw.lib and libcmt.lib
-
darda almost 10 yearsI put them in additional dependencies as suggested (in front of anything else I had) but did not need to put them in "ignore specific library". For any of those looking, this is "how you solve a dependency problem" as suggested by Step 2 of this knowledge base article frequently linked to for issues like this.
-
The Vivandiere almost 10 years@grim, that saved my life.
-
mallardz over 9 yearsI don't know if I'm going crazy, but I tried your answer after the other options failed to work. The error(s) went away. I then removed the line to make a note of the exact nature of the errors... and they didn't come back?! hmm c++ compiler errors are cryptic to say the least...
-
Mark Ch over 7 yearsI independently came to the same conclusion.
-
IInspectable about 7 yearsI believe
inline
is sufficient. After all, you don't really care, whether those calls get inlined or not. You just need to be able to have multiple definitions (in different translation units), and that's what inline buys you. -
Vivek Subramanian over 5 yearsExcellent answer!
-
Den-Jason almost 4 yearsYep, this worked for me too. I suspect it is due to what is included by stdafx.h. I wonder whether removing the stdafx.h include from all files and removing stdafx.cpp would have the same effect?
-
Den-Jason almost 4 yearsThe pch.h file is generated as a precompiled header. You can turn them off for the project using Properties -> C/C++ -> Precompiled Headers and selecting "Not using precompiled headers". You can also disable precompiled headers for individual C/C++ files by right-clicking the file in Solution Explorer and going through the properties menu.