环境:Windows7+VS2010
环境:在非MFC程序(使用standard windows library)中,添加MFC支持,静态链接MFC库。
发生以下错误:
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.
解决方法一:
我的问题是在添加了一个类的(.h和.cpp)文件后,我将cpp文件中添加预编译头文件#include “stdafx.h”错误消失
解决方法二:
在vs2010项目设置中【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或Debugg版本的uafxcwd.lib。
暂无评论内容