控制台程序如何使用MFC,CString
Window下编程,CString带来很大的方便,但是会出现如下异常:error LNK2005: “void * __cdecl operator new(unsigned int)” (??2@YAPAXI@Z) already defined in LIBCMT.
在非MFC程序(使用standard windows library)中,添加MFC支持(#include <afx.h>)。使用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.
解决方案:
在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。