error LNK2005: new and delete already defined in L

2019-01-13 06:05发布

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.

15条回答
够拽才男人
2楼-- · 2019-01-13 06:14

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.

查看更多
Viruses.
3楼-- · 2019-01-13 06:14

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>
查看更多
啃猪蹄的小仙女
4楼-- · 2019-01-13 06:14

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.

查看更多
孤傲高冷的网名
5楼-- · 2019-01-13 06:16

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

查看更多
6楼-- · 2019-01-13 06:16

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楼-- · 2019-01-13 06:19

in config linker input

  • In additional dependicies put uafxcw.lib;LIBCMT.lib
  • In Ignore specific put put uafxcw.lib;LIBCMT.lib
查看更多
登录 后发表回答