Visual Studio Tips: error LNK2005: ... already defined in LIBCMTD.lib(new.obj)
来源:互联网 发布:淘宝哪些卖aj 编辑:程序博客网 时间:2024/06/17 00:26
遇到的问题
一个很老的C++工程,之前编译一切正常,就在我引入了几个新的 .h 和 .cpp 文件之后,编译死活通不过了,报以下错误:
(当然这里有个前提,就是我新引入的那几个文件经反复检查绝无问题,否则我就该从那几个文件入手了,就不会存在本文了。)
Error2error 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-XP1>------ Build started: Project: DemoApplication, Configuration: Debug Win32 ------1> text.cpp1>gbk.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/SAFESEH' specification1>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' specification1>gbk.obj : warning LNK4075: ignoring '/EDITANDCONTINUE' due to '/OPT:LBR' specification1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMTD.lib(new.obj); second definition ignored1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMTD.lib(dbgdel.obj); second definition ignored1>nafxcwd.lib(afxmem.obj) : warning LNK4006: "void __cdecl operator delete[](void *)" (??_V@YAXPAX@Z) already defined in LIBCMTD.lib(delete2.obj); second definition ignored1>Debug\DemoApplication.exe : warning LNK4088: image being generated due to /FORCE option; image may not run1> 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
0 0
- Visual Studio Tips: error LNK2005: ... already defined in LIBCMTD.lib(new.obj)
- 【error】LNK2005: already defined in LIBCMTD.lib(typinfo.obj)
- error LNK2005: _atoi already defined in libcmtd.lib(atox.obj)
- error LNK2005: __vsnwprintf already defined in libcmtd.lib(vsnwprnt.obj)
- nafxcwd.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" already defined in LIBCMTD.lib(new.obj)
- already defined in LIBCMTD.lib(new.obj)
- VC6错误:libcmtd.lib(ccccc.obj) :error lnk2005:xxxx already defined in libcd.lib(ccccc.obj)
- LIBCMTD.lib(dbgheap.obj) : error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D
- msvcrtd.lib(MSVCRTD.dll) : error LNK2005: __setmbcp already defined in libcmtd.lib(mbctype.obj)
- LIBCMTD.lib(dbgheap.obj) : error LNK2005: __CrtSetCheckCount already defined in MSVCRTD.lib(MSVCR90D
- LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj)
- LNK2005 __CRT_RTC_INIT already defined in LIBCMTD.lib(_init_.obj)
- 碰到这样的错误error LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj)
- error LNK2005: _DllMain@12 already defined in LIBCMTD.lib(dllmain.obj) 解决方案
- already defined in LIBCMTD.lib(new.obj) nafxcwd.lib
- already defined in LIBCMTD.lib(new.obj) nafxcwd.lib
- already defined in LIBCMTD.lib(new.obj) nafxcwd.lib .
- error LNK2005: __*** already defined in LIBCMTD.lib
- PCL 库中的pcl::visualization::PCLVisualizer类彩色显示点云
- 九章算法面试题66 2 sum
- android异常处理:未处理异常->线程退出->检查异常->处理
- 九章算法面试题67 3 sum
- 九章算法面试题68 4 sum
- Visual Studio Tips: error LNK2005: ... already defined in LIBCMTD.lib(new.obj)
- Symmetric Key Cryptography 对称密钥加密
- 程序员不得不知道的技术面试资料大全
- 【系统设计】短URL系统是怎么设计的?
- 系统设计面些啥?Palantir 面官教你如何准备系统设计面试!
- Leetcode: Jump Game
- 人造太阳:清洁能源的未来
- 迟迟未曾出现而
- 局面对于他们而