error LNK2005: new and delete already defined in LIBCMTD.lib(new.obj)

Viewed 81345

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.

18 Answers

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.

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.

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.

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!

#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:

  1. Project > Properties > Configuration Properties > General > Use of MFC set it to use in a Shared DLL, this should also automatically set C/C++ > Code Generation > Runtime Library to Multi-threaded debug dll /MDd make sure it indeed did that. Try compile now, for me it worked.
  2. 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:
#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??

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.

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

Got errors after applying Cipher Saw's solution to vs2015

1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "void __stdcall DDX_Control(class CDataExchange *,int,class CWnd &)" (?DDX_Control@@YGXPAVCDataExchange@@HAAVCWnd@@@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: int __thiscall CWnd::ExecuteDlgInit(void *)" (?ExecuteDlgInit@CWnd@@QAEHPAX@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(wincore2.obj) : error LNK2005: "public: void __thiscall CMFCDynamicLayout::GetHostWndRect(class CRect &)const " (?GetHostWndRect@CMFCDynamicLayout@@QBEXAAVCRect@@@Z) already defined in uafxcwd.lib(wincore2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "void __cdecl AfxRegisterMFCCtrlClasses(void)" (?AfxRegisterMFCCtrlClasses@@YAXXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "protected: void __thiscall CMFCControlContainer::PreUnsubclassControl(class CWnd *)" (?PreUnsubclassControl@CMFCControlContainer@@IAEXPAVCWnd@@@Z) already defined in uafxcwd.lib(afxctrlcontainer2.obj)
1>afxnmcdd.lib(afxctrlcontainer2.obj) : error LNK2005: "public: int __thiscall CMFCControlContainer::SubclassDlgControls(void)" (?SubclassDlgControls@CMFCControlContainer@@QAEHXZ) already defined in uafxcwd.lib(afxctrlcontainer2.obj)

Was able to fix them by changing libs list from uafxcw.lib;Libcmt.lib to afxnmcdd.lib;uafxcwd.lib;Libcmtd.lib (debug unicode build)

Related