Visual Studio Tips: error LNK2005: ... already defined in LIBCMTD.lib(new.obj)
2015-05-13 03:07
579 查看
遇到的问题
一个很老的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/148652Linker 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
相关文章推荐
- LIBCMTD.lib(dbgheap.obj) : error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D
- greta的问题及解决: LIBCMTD.lib(_wctype.obj) : error LNK2005: _iswspace already defined in xxx.obj
- VC6错误:libcmtd.lib(ccccc.obj) :error lnk2005:xxxx already defined in libcd.lib(ccccc.obj)
- 碰到这样的错误error LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj)
- libcmtd.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSVCRTD.dll) 编译错误解决方法
- libcmtd.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSVCRTD.dll) 编译错误解决方法
- 【error】LNK2005: already defined in LIBCMTD.lib(typinfo.obj)
- Solve Error: nafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in libcpmt.lib(newaop.obj)
- LIBCMTD.lib(dbgheap.obj) : error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D
- vc库连接错误 error LNK2005: __alloc_osfhnd already defined in LIBC.lib(osfinfo.obj)
- error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT.
- mfcs100d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)
- libcd.lib(crt0dat.obj) : error LNK2005: _exit already defined in msvcrtd.lib(MSVCRTD.dll)
- uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案
- mfcs90d.lib(xxx.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(xxx.obj)
- uafxcw.lib(dllmodul.obj) : error LNK2005: DllMain already defined in
- already defined in LIBCMTD.lib(new.obj) nafxcwd.lib .
- error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT.
- uafxcwd.lib(afxmem.obj) : error LNK2005: 已经在 LIBCMTD.lib(new.obj) 中定义错误解决方案
- mfcs100d.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in MSVCRTD.lib(dllmain.obj)