发生以下错误:
uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT.lib(new.obj)
uafxcw.lib(afxmem.obj) : error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z) already defined in LIBCMT.lib(delete.obj)
uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new[](unsigned int)" (??_U@YAPAXI@Z) already defined in LIBCMT.lib(new2.obj)
问题产生原因,CRT库和MFC库链接时顺序有误:
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.
具体解决方法:(另一种解决方法参见最后链接,本人未测试)
在vs2005项目设置中【Linker】-【input】-【additional dependencies】追击nafxcw.lib,libcmt.lib,Debug下,追加Nafxcwd.lib,libcmtd.lib。
再次编译通过,但出现
LINK : warning LNK4098: defaultlib 'uafxcw.lib' conflicts with use of other libs; use /NODEFAULTLIB:library
在【Linker】-【input】-【Ignore specific library】中追加uafxcw.lib。
详细请参考:
http://support.microsoft.com/default.aspx?scid=kb;en-us;q148652
http://www.codeguru.com/forum/archive/index.php/t-330203.html
http://blog.csdn.net/huguohu2006/article/details/8486817

本文详细解释了在非MFC程序中添加MFC支持时遇到的错误及解决方法,包括如何正确链接CRT库和MFC库以避免冲突,并提供了VS2005项目设置中的具体步骤。

1万+

被折叠的 条评论
为什么被折叠?



