編譯器和windows作業系統的幾個主要預定義值對程式的編譯會有影響,以下為基本的列表
(關於詳細的c++編譯預定義,請在msdn中搜尋“Predefined Macros”以擷取更準備更全面的描述)
一、c++預定義
__cplusplus
一般是在c庫的標頭檔中使用,方法如下:
#ifdef __cplusplus
extern "C" {
#endif
... // C庫的函數和資料類型聲明
#ifdef __cplusplus
}
#endif
二、c++編譯器的版本定義
這裡的C++是指vs(visual studio、vc++)的編譯器cl的版本定義,表面也可以說是VS IDE的版本預定義.(有助於搜尋,vs2005,vs2008)
版本預定義:_MSC_VER
Compiler _MSC_VER value
-------- --------------
C Compiler version 6.0 600
C/C++ compiler version 7.0 700
Visual C++, Windows, version 1.0 800
Visual C++, 32-bit, version 1.0 800
Visual C++, Windows, version 2.0 900
Visual C++, 32-bit, version 2.x 900
Visual C++, 32-bit, version 4.0 1000
Visual C++, 32-bit, version 5.0 1100
Visual C++, 32-bit, version 6.0 1200
Visual C++, version 2003 .Net 1310
Visual C++, version 2005 1400
Visual C++, version 2008 1500
三、檔案的編譯選項
3.1 編譯日期預定義:__DATE__
表明當前檔案的編譯日期,結果為字串,格式為: Mmm dd yyyy,其中的月份名稱 Mmm 為time.h中asctime()函數產生的日期中的月份名稱
編譯時間:__TIME__
結果是字串,格式為:hh:mm:ss
最後的修改時間定義:__TIMESTAMP__
格式為:Ddd Mmm Date hh:mm:ss yyyy,其中日為1 ~ 31
3.2 檔案名稱預定義:__FILE__
可以在檔案的輸出位置列印本檔案名稱,這個在寫異常調試日誌時比較有用
檔案行號預定義:__LINE__
當前代碼所處的行號,它是一個整數
四、windows版本問題
(註:本節的翻譯是我對這些英文的理解基礎上進行的翻譯,可能不準確)
對這些版本有兩種方法定義,一種是老的預定義,一種是新的預定義。
新的預定義(The following table describes the preferred macros in use by the Windows header files.):
Minimum system required |
Macros to define |
Windows Server 2008 |
NTDDI_VERSION >= NTDDI_LONGHORN |
Windows Vista |
NTDDI_VERSION >= NTDDI_VISTA |
Windows Server 2003 SP1 |
NTDDI_VERSION >= NTDDI_WS03SP1 |
Windows Server 2003 |
NTDDI_VERSION >= NTDDI_WS03 |
Windows XP SP2 |
NTDDI_VERSION >= NTDDI_WINXPSP2 |
Windows XP SP1 |
NTDDI_VERSION >= NTDDI_WINXPSP1 |
Windows XP |
NTDDI_VERSION >= NTDDI_WINXP |
Windows 2000 SP4 |
NTDDI_VERSION >= NTDDI_WIN2KSP4 |
Windows 2000 SP3 |
NTDDI_VERSION >= NTDDI_WIN2KSP3 |
Windows 2000 SP2 |
NTDDI_VERSION >= NTDDI_WIN2KSP2 |
Windows 2000 SP1 |
NTDDI_VERSION >= NTDDI_WIN2KSP1 |
Windows 2000 |
NTDDI_VERSION >= NTDDI_WIN2K |
老的預定義(The following table describes the legacy macros in use by the Windows header files.):
Minimum system required |
Macros to define |
Windows Server 2008 |
_WIN32_WINNT>=0x0600,WINVER>=0x0600 |
Windows Vista |
_WIN32_WINNT>=0x0600,WINVER>=0x0600 |
Windows Server 2003 |
_WIN32_WINNT>=0x0502,WINVER>=0x0502 |
Windows XP |
_WIN32_WINNT>=0x0501,WINVER>=0x0501 |
Windows 2000 |
_WIN32_WINNT>=0x0500,WINVER>=0x0500 |
Windows NT 4.0 |
_WIN32_WINNT>=0x0400,WINVER>=0x0400 |
Windows Me |
_WIN32_WINDOWS=0x0500,WINVER>=0x0500 |
Windows 98 |
_WIN32_WINDOWS>=0x0410,WINVER>=0x0410 |
Windows 95 |
_WIN32_WINDOWS>=0x0400,WINVER>=0x0400 |
Internet Explorer 7.0 |
_WIN32_IE>=0x0700 |
Internet Explorer 6.0 SP2 |
_WIN32_IE>=0x0603 |
Internet Explorer 6.0 SP1 |
_WIN32_IE>=0x0601 |
Internet Explorer 6.0 |
_WIN32_IE>=0x0600 |
Internet Explorer 5.5 |
_WIN32_IE>=0x0550 |
Internet Explorer 5.01 |
_WIN32_IE>=0x0501 |
Internet Explorer 5.0, 5.0a, 5.0b |
_WIN32_IE>=0x0500 |
Internet Explorer 4.01 |
_WIN32_IE>=0x0401 |
Internet Explorer 4.0 |
_WIN32_IE>=0x0400 |
Internet Explorer 3.0, 3.01, 3.02 |
_WIN32_IE>=0x0300 |
(此文以後可能會不斷更新)