天天看點

error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已經在 LIBCMTD.lib(new.obj) 中

版權聲明:本文為 testcs_dn(微wx笑) 原創文章,非商用自由轉載-保持署名-注明出處,謝謝。 https://blog.csdn.net/testcs_dn/article/details/46284967

在編譯文章:

使用GetAdaptersAddresses函數擷取實體MAC位址

中的代碼時,出現以下錯誤:

錯誤 1 error LNK2005: “void * __cdecl operator new(unsigned int)” (??2@YAPAXI@Z) 已經在 LIBCMTD.lib(new.obj) 中定義 F:\CcProjects\擷取網卡實體位址MAC位址\擷取網卡實體位址MAC位址\uafxcwd.lib(afxmem.obj) 擷取網卡實體位址MAC位址

環境: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.

  具體解決方法:(另一種解決方法參見最後連結,本人未測試)

  在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。

操作方法如下圖:

在解決方案資料總管中選中對應的項目

點選項目菜單》屬性,或者是直接在項目上單擊滑鼠右鍵》屬性

展開配置屬性》連結器,選中輸入,點選附加依賴項右側的下拉框,在彈出的菜單中點選編輯

輸入附加依賴項,點選确定。

繼續閱讀