error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) already defined in LIBCMT.lib(new.obj)链接错误!

  环境:在非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.

  具体解决方法:(另一种解决方法参见最后链接,本人未测试)

  在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

posted @ 2010-09-28 11:56  友学友  阅读(14253)  评论(0编辑  收藏  举报