[C++] 我發誓! 我什麼都沒動, 原本可以動的程式, 竟然幾天後就不行了.

来源:互联网 发布:mac海淘攻略 编辑:程序博客网 时间:2024/05/17 22:47
 

[C++] 我發誓! 我什麼都沒動, 原本可以動的程式, 竟然幾天後就不行了.

這種事情很詭異. 但是在 Vistual Studio 上面開發程式卻有可能發生這種狀況.

當完成 code 了以後, make clean 然後 svn 上去之後.

以為沒事了, 可是過幾天再回來編譯 code 的時候, 卻發生下面的 error message:

--------------------------------------------------------

1>uafxcw.lib(dllmodul.obj) : error LNK2005: _DllMain@12 already defined in LIBCMT.lib(dllmain.obj)
1>uafxcw.lib(afxmem.obj): error LNK2005: "void * __cdecl operator new(unsigned int)"(??2@YAPAXI@Z) already defined in LIBCMT.lib(new.obj)
1>uafxcw.lib(afxmem.obj): error LNK2005: "void __cdecl operator delete(void *)" (??3@YAXPAX@Z)already defined in LIBCMT.lib(delete.obj)
1>uafxcw.lib(afxmem.obj): error LNK2005: "void * __cdecl operator new[](unsigned int)"(??_U@YAPAXI@Z) already defined in LIBCMT.lib(new2.obj)
1>uafxcw.lib(afxmem.obj): error LNK2005: "void __cdecl operator delete[](void *)"(??_V@YAXPAX@Z) already defined in LIBCMT.lib(delete2.obj)

--------------------------------------------------------

我發誓! 我什麼都沒動, 原本可以動的程式, 竟然幾天後就不行了.

可是把以前的版本拿出來卻沒問題可以正常編譯.  最新版本的程式就是不行. 明明之前是 ok 的才會 commit.    暈倒!

 

根據標準答案的描述:  http://support.microsoft.com/default.aspx?scid=kb;EN-US;Q148652

主要發生的原因: 在於我新的 mfc 程式碼加了一個用 c 寫的 function. 剛好 C run-time library 與 MFC 同時都有實作 new , delete, DllMain.

而其中 link 必須要按照順序來, MFC 的版本要先被 link 然後才是 c run-time library. 如果不按照指定的順序那就會發生 link 錯誤的問題. 而連結的時機是由你的編譯環境動態決定. 所以這樣造成了有時候可以正常 link 有時不行.

 

解決方法其實就是在 出問題的 .cpp module 加上 #include "stdafx.h"

如果是 .c module 則加入下面的程式碼

--------------- in forelib.h -------------

#ifndef _AFX_NOFORCE_LIBS

/////////////////////////////////////////////////////////////////////////////
// Win32 libraries

#ifndef _AFXDLL
#ifndef _UNICODE
#ifdef _DEBUG
#pragma comment(lib, "nafxcwd.lib")
#else
#pragma comment(lib, "nafxcw.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "uafxcwd.lib")
#else
#pragma comment(lib, "uafxcw.lib")
#endif
#endif
#else
#ifndef _UNICODE
#ifdef _DEBUG
#pragma comment(lib, "mfc80d.lib")
#pragma comment(lib, "mfcs80d.lib")
#else
#pragma comment(lib, "mfc80.lib")
#pragma comment(lib, "mfcs80.lib")
#endif
#else
#ifdef _DEBUG
#pragma comment(lib, "mfc80ud.lib")
#pragma comment(lib, "mfcs80ud.lib")
#else
#pragma comment(lib, "mfc80u.lib")
#pragma comment(lib, "mfcs80u.lib")
#endif
#endif
#endif

#ifdef _DLL
#if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
#pragma comment(lib, "msvcrtd.lib")
#else
#pragma comment(lib, "msvcrt.lib")
#endif
#else
#ifdef _MT
#if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
#pragma comment(lib, "libcmtd.lib")
#else
#pragma comment(lib, "libcmt.lib")
#endif
#else
#if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
#pragma comment(lib, "libcd.lib")
#else
#pragma comment(lib, "libc.lib")
#endif
#endif
#endif

#pragma comment(lib, "kernel32.lib")
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#pragma comment(lib, "msimg32.lib")
#pragma comment(lib, "comdlg32.lib")
#pragma comment(lib, "winspool.lib")
#pragma comment(lib, "advapi32.lib")
#pragma comment(lib, "shell32.lib")
#pragma comment(lib, "comctl32.lib")
#pragma comment(lib, "shlwapi.lib")

// force inclusion of NOLIB.OBJ for /disallowlib directives
#pragma comment(linker, "/include:__afxForceEXCLUDE")

// force inclusion of DLLMODUL.OBJ for _USRDLL
#ifdef _USRDLL
#pragma comment(linker, "/include:__afxForceUSRDLL")
#endif

// force inclusion of STDAFX.OBJ for precompiled types
#ifdef _AFXDLL
#pragma comment(linker, "/include:__afxForceSTDAFX")
#endif

#endif //!_AFX_NOFORCE_LIBS
--------------- end of forelib.h -------------
然後, 再把 forelib.h 放到 c module 的前面.
ex:
#include "forelib.h"
by Jing
原创粉丝点击