error LNK2005: new and delete already defined in L

2019-01-13 06:09发布

问题:

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.

回答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).



回答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.



回答3:

in config linker input

  • In additional dependicies put uafxcw.lib;LIBCMT.lib
  • In Ignore specific put put uafxcw.lib;LIBCMT.lib


回答4:

be sure that you have #include <afx.h> in "stdafx.h" BEFORE other includes like #include <string>



回答5:

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.



回答6:

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:

  1. Don't miss the ; between the two .lib files.
  2. A suffix -d must be added in the files in Debug mode.


回答7:

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.



回答8:

Typo. One stupid way you got that is instead of include the header, you inlucde the cpp. e.g.

#include <myclass.cpp> //should be #include <myClass.h>


回答9:

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.



回答10:

Got rid of the problem

uafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned __int64)"
  • In additional dependicies put uafxcw.lib.
  • In Ignore specific put put uafxcw.lib.


回答11:

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.



回答12:

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.



回答13:

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:

__forceinline void * operator new(size_t size)
{
    return _aligned_malloc(size, 16);
}
__forceinline void operator delete(void* ptr)
{
    _aligned_free(ptr);
}
__forceinline void * operator new [](size_t size)
{
    return _aligned_malloc(size, 16);
}
__forceinline void operator delete [](void* ptr)
{
    _aligned_free(ptr);
}


回答14:

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



回答15:

For me the problem was solved by changing

Project -> Properties -> Configuration Properties -> General: Use of MFC = Use MFC in a Shared DLL

Before it was set to "Use Standard Windows Libraries"

Additionally I had to set the /MD option under

Project -> Properties -> C/C++ -> Code Generation : Runtime Library = Multi-threaded DLL (/MD)