VC常用項目參數設置比較

VC常用項目參數設置比較

Posted on 2008-11-06 13:49 kevin 閱讀(51) 評論(0)  編輯 收藏 引用

/////////////////////////////////////////////////////////////////////////////////
1.基於對話框(/單文檔/多文檔)的MFC程序
預編譯頭文件stdafx.h:
#define VC_EXTRALEAN     // Exclude rarely-used stuff from Windows headers

// afxwin.h中聲明瞭MFC封裝的一些很基本的類(CWnd、CView、CButton、CDC等)
#include <afxwin.h>         // MFC core and standard components
// afxext.h中聲明瞭MFC的一些擴展類(CBitmapButton、CControlBar、CSplitterWnd等)
#include <afxext.h>         // MFC extensions
// afxdisp.h中聲明瞭Ole的幾個類(COleException、COleVariant等)
#include <afxdisp.h>        // MFC Automation classes
// afxdtctl.h中聲明瞭幾個控件類(CImageList、CMonthCalCtrl、CDateTimeCtrl等)
#include <afxdtctl.h>       // MFC support for Internet Explorer 4 Common Controls

#ifndef _AFX_NO_AFXCMN_SUPPORT
// afxcmn.h中聲明瞭MFC常用的一些控件類(CListCtrl、CProgressCtrl、CToolTipCtrl等)
#include <afxcmn.h>            // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT

(1.1)Use MFC in a Shared DLL
Debug版本:
預定義:WIN32,_DEBUG,_WINDOWS,_AFXDLL,_MBCS
編譯參數:/nologo /MDd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /FR"Debug/" /Fp"Debug/ExeDlg.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ   /c
連接參數:/nologo /subsystem:windows /incremental:yes /pdb:"Debug/ExeDlg.pdb" /debug /machine:I386 /out:"Debug/ExeDlg.exe" /pdbtype:sept

Release版本:
預定義:與Debug版本相比,將_DEBUG替換成了NDEBUG
編譯參數:/nologo /MD /W3 /GX /O2 /D "WIN32" /D "NDEBUG" /D "_WINDOWS" /D "_AFXDLL" /D "_MBCS" /Fp"Release/ExeDlg.pch" /Yu"stdafx.h" /Fo"Release/" /Fd"Release/" /FD /c
連接參數:/nologo /subsystem:windows /incremental:no /pdb:"Release/ExeDlg.pdb" /machine:I386 /out:"Release/ExeDlg.exe"

(1.2)Use MFC in a Static Library
Debug版本:
預定義:與(1.1)相比,少了_AFXDLL
編譯參數:將/MDd(使用Run-time library: Debug Multithreaded DLL)換成了/MTd(使用Run-time library: Debug Multithreaded)
連接參數:與(1.1)相同

Release版本:
編譯參數/MD(使用Run-time library: Multithreaded DLL)換成了MT(使用Run-time library: Multithreaded)

***備註:以上編譯/連接參數含義如下(更多的,請參考Msdn):
/nologo:抑制信息在編譯或者連接時在Output Window輸出;   /MD:運行時庫使用MSVCRT.DLL;   /W3:編譯時顯示爲Warning的級別爲3;   /Gm:Enable Minimal Rebuild,一種減少重編譯的選項;  /GX:Enable Exception Handling;     /ZI:設置Debug信息保存的數據庫文件.PDB中;    /Od:Disable代碼優化;     /FR:生成.SBR文件,包含有符號信息;       /Fp:命名生成的預編譯頭文件;     /Yu:指定預編譯頭文件。




/////////////////////////////////////////////////////////////////////////////////
2.MFC DLL項目
預編譯頭文件stdafx.h:
#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

#include <afxwin.h>         // MFC core and standard components
#include <afxext.h>         // MFC extensions

#ifndef _AFX_NO_OLE_SUPPORT
#include <afxole.h>         // MFC OLE classes
#include <afxodlgs.h>       // MFC OLE dialog classes
#include <afxdisp.h>        // MFC Automation classes
#endif // _AFX_NO_OLE_SUPPORT


#ifndef _AFX_NO_DB_SUPPORT
#include <afxdb.h>            // MFC ODBC database classes
#endif // _AFX_NO_DB_SUPPORT

#ifndef _AFX_NO_DAO_SUPPORT
#include <afxdao.h>            // MFC DAO database classes
#endif // _AFX_NO_DAO_SUPPORT

#include <afxdtctl.h>        // MFC support for Internet Explorer 4 Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#include <afxcmn.h>            // MFC support for Windows Common Controls
#endif // _AFX_NO_AFXCMN_SUPPORT
增加了兩個OLE的頭文件和兩個數據庫的頭文件

(2.1) Use MFC in a Shared DLL
Debug版本:
預定義:WIN32,_DEBUG,_WINDOWS,_WINDLL,_AFXDLL,_MBCS,_USRDLL,與MFC Exe程序相比,增加了_WINDLL和_USRDLL
編譯參數:與MFC Exe沒有太大區別
連接參數:/nologo /subsystem:windows /dll /incremental:yes /pdb:"Debug/MFCDll.pdb" /debug /machine:I386 /def:"./MFCDll.def" /out:"Debug/MFCDll.dll" /implib:"Debug/MFCDll.lib" /pdbtype:sept
與MFC Exe相比,增加了/dll定義,以及/def:"./MFCDll.def"和/implib:"Debug/MFCDll.lib"。注意:其中MFCDll是測試的項目名字,非標準DLL名字。
從項目的文件上看,這個項目比MFC Exe多產生一個.def的文件用於定義導出函數。

Release版本與Debug版本的區別類似項目1中的比較(上了_AFXDLL定義)。

(2.2) Use MFC in a Static DLL
與(2.1)的區別,主要在使用的Run-time library類型上,與項目1中的比較。




/////////////////////////////////////////////////////////////////////////////////
3.MFC Extension DLL項目
預編譯頭文件stdafx.h內容與項目2相同。

(3.1) Use MFC in a Shared DLL
Debug版本:
預定義:WIN32,_DEBUG,_WINDOWS,_MBCS,_AFXEXT,_WINDLL,_AFXDLL,與項目2相比,將_USRDLL換成了_AFXEXT。
編譯參數:與上述項目沒有太大區別
連接參數:與MFC DLL項目相似

Release版本與Debug版本的區別類似項目1中的比較(上了_AFXDLL定義)。

(3.2) Use MFC in a Static DLL
類似以上項目的比較。




(注:以下項目均以Debug版本論述。)
/////////////////////////////////////////////////////////////////////////////////
4.Win32 DLL項目
預編譯頭文件stdafx.h:
#define WIN32_LEAN_AND_MEAN        // Exclude rarely-used stuff from Windows headers

#include <windows.h>

出現項目入口函數DllMain的實現。

(4.1) Not Using MFC
預定義:WIN32,_DEBUG,_WINDOWS,_MBCS,_USRDLL,WIN32DLLDEMO_EXPORTS,與項目2(MFC DLL)相比,少了_WINDLL,_AFXDLL,而僅保留了_USRDLL。另外,WIN32DLLDEMO_EXPORTS自定義的導出宏。
編譯參數:沒有太大區別。
連接參數:kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /dll /incremental:yes /pdb:"Debug/Win32DllDemo.pdb" /debug /machine:I386 /out:"Debug/Win32DllDemo.dll" /implib:"Debug/Win32DllDemo.lib" /pdbtype:sept
與MFC DLL項目相比,多了很多庫的連接,少了/subsystem:windows的定義。

(4.2) Use MFC in a Shared DLL
預定義:與(4.1)相比,增加了_WINDLL,_AFXDLL的定義
編譯參數:沒有太大區別。
連接參數:/nologo /dll /incremental:yes /pdb:"Debug/Win32DllDemo.pdb" /debug /machine:I386 /out:"Debug/Win32DllDemo.dll" /implib:"Debug/Win32DllDemo.lib" /pdbtype:sept
可以看出,(4.1) 裏連接的很多庫消失了,這時,項目4變成了類似於項目2中的設置。
***但是,編程時需要注意,項目4的stdafx.h僅包含了<windows.h>,此時如果要用MFC的類,需要自己加入MFC的幾個頭文件(<afxwin.h>、<afxext.h>等),並且將#include <windows.h>和DllMain入口函數註釋掉!

(4.3) Use MFC in a Static DLL
使用MFC DLL的方式Shared和Static之間的區別與上述項目類似,不再做比較。



/////////////////////////////////////////////////////////////////////////////////
5.Win32 Static Library項目
預編譯頭文件stdafx.h(可能沒有這個文件):
如果不使用MFC的話,僅僅#define WIN32_LEAN_AND_MEAN,而如果使用MFC,內容如下:
#define VC_EXTRALEAN        // Exclude rarely-used stuff from Windows headers

#include <afx.h>
#include <afxwin.h>

(5.1) Not Using MFC    
預定義:WIN32,_DEBUG,_MBCS,_LIB,新出現了符號_LIB
編譯參數:/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /Fp"Debug/W32StaPrehead.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ  /c
注意到使用的Run-time library參數爲/MLd。
庫參數:這個項目沒有Link設置頁,代替的是Library頁,參數如下:/nologo /out:"Debug/W32StaPrehead.lib"

(5.2) Use MFC in a Shared DLL
預定義:WIN32,_DEBUG,_WINDOWS,_MBCS,_AFXDLL,與項目1(MFC Exe)設置相同。
編譯參數:注意使用的Run-time library參數爲/MDd。
庫參數:沒有太大區別。

(5.3) Use MFC in a Static DLL
編譯參數:注意使用的Run-time library參數爲/MTd。



/////////////////////////////////////////////////////////////////////////////////
6.Win32 Application項目
預編譯頭文件stdafx.h
#define WIN32_LEAN_AND_MEAN    // Exclude rarely-used stuff from Windows headers

// Windows Header Files:
#include <windows.h>

// C RunTime Header Files
#include <stdlib.h>
#include <malloc.h>
#include <memory.h>
#include <tchar.h>

出現了Win32程序的入口函數WinMain。

這個項目,Project->settings->General中設置使用MFC的方式,一般設爲Not Using MFC,否則程序結構的改動比較大。從Not Using MFC到使用MFC的改變,對連接的庫的影響類似於項目4(Win32 DLL)。

(6.1) Not Using MFC
預定義:WIN32,_DEBUG,_WINDOWS,_MBCS
編譯參數:/nologo /MLd /W3 /Gm /GX /ZI /Od /D "WIN32" /D "_DEBUG" /D "_MBCS" /D "_LIB" /Fp"Debug/W32StaPrehead.pch" /Yu"stdafx.h" /Fo"Debug/" /Fd"Debug/" /FD /GZ  /c
注意到使用的Run-time library參數爲/MLd。
連接參數:kernel32.lib user32.lib gdi32.lib winspool.lib comdlg32.lib advapi32.lib shell32.lib ole32.lib oleaut32.lib uuid.lib odbc32.lib odbccp32.lib /nologo /subsystem:windows /incremental:yes /pdb:"Debug/W32AppDemo.pdb" /debug /machine:I386 /out:"Debug/W32AppDemo.exe" /pdbtype:sept

(6.2) Use MFC in a Shared DLL
編譯參數:注意使用的Run-time library參數爲/MDd。

(6.3) Use MFC in a Static DLL
編譯參數:注意使用的Run-time library參數爲/MTd。




小結:
1.MFC的使用方式對默認情況選擇的Run-time library的影響(以Debug版本爲例):
Not Using MFC ---〉/MLd: Debug Single-Threaded(靜態連接LIBCD.LIB庫)
Use MFC in a Shared DLL ---〉/MDd: Debug Multithreaded DLL(動態連接MSVCRTD.DLL庫)
Use MFC in a Static DLL ---〉/MTd: Debug Multithreaded(靜態連接LIBCMTD.LIB庫)

2.如果不使用MFC,在Link一欄一般會連接一系列Windows API的庫文件;如果使用MFC,這些連接庫就會“消失”。

3.Debug版本一般會有_DEBUG的預定義,而Release版本則定義NDEBUG。

4.使用Shared MFC和Static MFC相比,前者一般多一個_AFXDLL的定義。默認使用的Run-time Library也不一樣,前者爲/MDd,後者爲/MTd。

5.MFC的普通DLL項目比MFC的EXE項目,一般多_WINDLL和_USRDLL預定義;連接參數多一個/dll定義。而MFC擴展DLL項目與MFC普通DLL項目相比,預定義將_USRDLL換成了_AFXEXT。

6.不使用MFC的Win32 DLL與MFC DLL相比,預定義少了_WINDLL和_AFXDLL,而僅保留了_USRDLL。

7.不使用MFC的靜態庫有_LIB的預定義。

8.#include <afxwin.h> 和#include <windows.h>不能重複包含,前者用於MFC程序,後者用於程序。

9.爲了去掉Windows頭文件中很少用到的定義,一般在stdafx.h中,Win32程序會定義#define WIN32_LEAN_AND_MEAN,而MFC程序會定義#define VC_EXTRALEAN。

10.作爲本文的應用,改變項目參數設置,實現不同類型項目之間的項目轉換,如下:
MFC Exe   <======> MFC DLL
  ||                  ||
  ||                  ||
  ||                  ||
Win32 Exe <======> Win32 DLL

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章