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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 红米2a忘了登陆账号怎么办 qq封了密保手机没用了怎么办 乐视手机重置账号密码忘了怎么办 此版本的ios不支持银联怎么办 单反m档拍出来照片是黑色怎么办 从兴趣部落老发骚扰信息怎么办 在厂里辞一个月厂长不批怎么办 在厂里做管理被员工恐吓怎么办 在葡京娱乐输了很多钱怎么办 从珠海入镜澳门北京往返签注怎么办 艾艾灸灸了一身小子子怎么办? 微信视频已过期或已清理怎么办 视频已过期或已被清理怎么办 小孩作业不会老婆天天吵骂打怎么办 苹果手机在太阳下屏幕变暗怎么办 斗鱼的鱼丸竞猜主播结算了怎么办 附近有个小姐姐想加她好友怎么办 孩子出现听别人说话语速很快怎么办 苹果手机上的邮件删了怎么办 手机qq邮箱独立密码忘记了怎么办 哺乳期吃了人参回奶了怎么办? 扣扣邮箱里的邮件过期了怎么办 一体机的管理员账号被删除了怎么办 手机里的邮箱重要吗删除了怎么办 华为荣耀10账号邮箱忘记了怎么办 大陆微信号在台湾登录不上怎么办 威纶触摸屏被禁止到反编译了怎么办 微信公众号邮箱被占用怎么办 京东绑定的手机号不用了怎么办 绑定微信的手机号不用了怎么办 绑定支付宝的手机号不用了怎么办 百度账号手机号换了密码忘了怎么办 换手机好了华为账号密码忘了怎么办 苹果手机忘了id账号和密码怎么办 金立手机账号密码忘了怎么办 乐视手机账号密码忘了怎么办 企业邮箱发出去邮件撤不回来怎么办 餐厅加热保温设备零线带电怎么办 小米手机不小心把照片删了怎么办 华为手机不小心把照片删了怎么办 网易邮箱被改成别人的姓名怎么办