Linker error: already defined

2019-05-23 00:02发布

问题:

I am trying to compile my Visual C++ project (uses MFC) in Microsoft Visual Studio 2012, and it comes back with the following errors:

error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)   
error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj) 
error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
error LNK1169: one or more multiply defined symbols found

So I googled around a bit and found this page: http://support.microsoft.com/?scid=kb%3Ben-us%3B148652&x=13&y=8 I tried solution one but the "Ignore Libraries" box does not exist, and so does the Object/library modules box. For the Ignore Libraries I found Ignore specific libraries, but skipping step five only gave me 17xx errors.

I googled around a lot, but always came back to the same page, how can I fix this problem in Visual Studio 2012?

回答1:

The most likely cause of the problem is having different versions of the C runtime (multi- or single-threaded, debug- or non-debug) linked for different parts of the project. Perhaps your main executable has one runtime linked but you are linking to a library built with a different runtime. Use depends.exe to check each library that you are linking explicitly in turn to find the odd one out.



回答2:

I fixed the problem: I found that it is a problem with the order of the includes. However forcing every file to have the afx.h include (properties->c/c++/advanced/force include file) fixed the problem for me.



回答3:

Ok, I just dealt with this problem while combining a .c file (compilation unit) with an MFC project. The .c file had the same name (compilation unit) that was in a library I was linking with. I changed the name of the file and the linker confusion went away.

I tried the other solutions above and none of them worked.



回答4:

I also found that if you use LTGC (link-time code generation) and you've just added a custom implementation for operator new or some other symbol that exists in the CRT, then you should do a full rebuild, only then does the incremental linker omit "weak references". This is true even in VS 2015.



回答5:

Were you having this problem with Visual Studio 2008 or 2010?

I am seeing a similar failure in Visual Studio 2012: code that linked fine in earlier versions of the toolchain is now giving multiple instances of error LNK2005: "void * __cdecl operator new(unsigned __int64,void *)" (??2@YAPEAX_KPEAX@Z) already defined. Unlike you, we're not using MFC.

This is not a solution to your problem, of course, but it may point to a regression bug in Microsoft's compiler or linker development. If you can verify that the same code works fine in VS2010, that would tend to confirm my diagnosis.

(To check this, simply set the Project Properties > Configuration Properties > General > Platform Toolset from "v110" to "v100" for all projects in your solution, and Rebuild All. Assuming you have VS2010 installed on the same machine, you can do this all without leaving VS2012.)

EDITED TO ADD: This error has been reported to Microsoft as bug #768788.