该文章转自: https://blog.csdn.net/yapingxin/article/details/45683079
遇到的问题
一个很老的C++工程,之前编译一切正常,就在我引入了几个新的 .h 和 .cpp 文件之后,编译死活通不过了,报以下错误:
(当然这里有个前提,就是我新引入的那几个文件经反复检查绝无问题,否则我就该从那几个文件入手了,就不会存在本文了。)
-
Error 2 error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
-
C:\Work\Demo\DemoApplication\nafxcwd.lib(afxmem.obj) Sentinel-XP
-
1>------ Build started: Project: DemoApplication, Configuration: Debug Win32 ------
-
1> text.cpp
-
1>gbk.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification
-
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj)
-
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj)
-
1>nafxcwd.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj)
-
1>Debug\DemoApplication.exe : fatal error LNK1169: one or more multiply defined symbols found
-
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
问题分析
世上没有无缘无故的编译错误。经过广泛地搜索,MSDN上有一段解释我认为非常靠谱,摘录如下:
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.
我就不把它翻译成中文了,我比较懒,而且相信大家都能看明白这句话。
厚道一点,给出MSDN的链接:https://support.microsoft.com/en-us/kb/148652
解决问题
在这篇MSDN上的文章中:https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx
给出了这样一句话:
To fix, add /FORCE:MULTIPLE to the linker command line options, and make sure that ... is the first library referenced.
也就是这样做:
然后再编译,虽然有些Warning,但是编译通过了:
-
1>------ Build started: Project: DemoApplication, Configuration: Debug Win32 ------
-
1> ...
-
1> ...
-
1> Compiling...
-
1> ...
-
1> Generating Code...
-
1>LINK : warning LNK4075: ignoring '/INCREMENTAL' due to '/FORCE' specification
-
1>gbk.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:LBR' specification
-
1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj); second definition ignored
-
1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj); second definition ignored
-
1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj); second definition ignored
-
1>Debug\DemoApplication.exe : warning LNK4088: image being generated due to /FORCE option; image may not run
-
1> DemoApplication.vcxproj -> C:\Work\Demo\DemoApplication\Debug\DemoApplication.exe
-
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========
运行也没啥问题。
当然这不是从根本上解决问题,但是鉴于原先那个旧的工程文件我实在不方便过多地改动,所以,就这样不完美地解决吧。
参考文献
- A LNK2005 error occurs when the CRT library and MFC libraries are linked in the wrong order in Visual C++ https://support.microsoft.com/en-us/kb/148652
- Linker Tools Error LNK2005 https://msdn.microsoft.com/en-us/library/72zdcz6f.aspx
- selectany https://msdn.microsoft.com/en-us/library/5tkz6s71(v=vs.80).aspx
- LNK2005, “already defined error” linker error in MSVC2010 http://stackoverflow.com/questions/8343303/lnk2005-already-defined-error-linker-error-in-msvc2010