Visual Studio Tips: error LNK2005: ... already defined in LIBCMTD.lib(new.obj)

46 篇文章 0 订阅
44 篇文章 0 订阅

遇到的问题


一个很老的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 ==========

运行也没啥问题。


当然这不是从根本上解决问题,但是鉴于原先那个旧的工程文件我实在不方便过多地改动,所以,就这样不完美地解决吧。


参考文献





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值