1.
基于
对话
框(
/
单
文档
/
多文档)的
MFC
程序
#include <afxdtctl.h> // MFC support for Internet Explorer 4 Common
#include <afxwin.h> // MFC core and standard components
#include <afxext.h> // MFC extensions
#include <afxdisp.h> // MFC Automation classes
#define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers
stdafx.h
预编译头
文件
afxext.h
声明MFC
一些
扩
展
类
(CBitmapButton
、CControlBar
、CSplitterWnd
等)
afxdisp.h
中声明了Ole
的几个
类
(COleException
、COleVariant
等)
afxwin.h
声明MFC
封装的一些很基本的
类
(CWnd
、CView
、CButton
、CDC
等)afxdtctl.h
声明几个控件
类
(CImageList
、CMonthCalCtrl
、CDateTimeCtrl
等)Controls
afxcmn.h
声明MFC
一些控件(CListCtrl
、CProgressCtrl
、CToolTipCtrl
等)
#include <afxcmn.h> // MFC support for Windows Common Controls
#ifndef _AFX_NO_AFXCMN_SUPPORT
#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.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
#include <windows.h> // Windows Header Files:
#include <stdlib.h> #include <malloc.h> #include <memory.h> #include <tchar.h> // C RunTime Header Files
出
现
了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