预编译指令与相关宏小结

1.预编译指令

01) #
  空指令,无任何效果

02) #include     
  包含一个源代码文件

03) #define
  定义宏
 
04) #undef
  取消已定义的宏

05) #if
  如果给定条件为真,则编译下面代码
 
06) #else                                  
   作为其他预处理的剩余选项进行编译

07) #ifdef
  如果宏已经定义,则编译下面代码

08) #ifndef
  如果宏没有定义,则编译下面代码

09) #elif
  如果前面的#if给定条件不为真,当前条件为真,则编译下面代码

10) #endif
  结束一个#if……#else条件编译块
 
11) #line
  改变当前的行数和文件名称
 
12) #pragma
  为编译程序提供非常规的控制流信息

13) #error
  停止编译并显示错误信息
 
 
 
 
 
2.与编译器相关的宏

    文中只列出部分相关宏,如果想查看完整的宏列表,可参考MSDN的"Predefined Macros"一节

01) _MSC_VER
    Microsoft 编译器的版本宏.   
    各版本编译器相应的数值如下表所示:
   
        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++, 32-bit, version 2002                1300
   Visual C++, 32-bit, version 2003                1310
   Visual C++, 32-bit, version 2005                1400
   Embedded Visual C++, 32-bit,version 4.0        Cross 1200 - 1202
     
  
02) _MFC_VER
    MFC版本宏.
       
       Version                        _MFC_VER value
    --------------                  -----------------
        4.21                             0x0421
        6.0                              0x0600
        7.0                              0x0700


03) __TIME__
    编译当前源文件的时间,格式为 "hh:mm:ss" 样式的字符串.
   

04) __DATE__
     编译当前源文件的日期,格式为 "Mmm dd yyyy" 样式的字符串.
    
         
05) __FILE__
    编译的当前源文件名.   
   
   
 
 
   
3.实例解说
 
01) 根据_MFC_VER值判断当前的编译环境.
    #if _MSC_VER >= 1400
       // this is Visual C++ 2005
    #elif _MSC_VER >= 1310
       // this is Visual c++ .NET 2003
    #elif _MSC_VER > 1300
       // this is Visual C++ .NET 2002
    #endif
 
   
02) #else if 和 #elif 的细微差别
    #if _MSC_VER < 1202
      //EVC 4.0 complier is cross 1200 - 1202
    #else if _MSC_VER >= 1400
      //Visual C++ 2005 complier is 1400
    #else
      //Visual C++ 2005 complier is 1400
    #endif
   
    这段预编译代码在Evc4.0和visual studio 2005中编译会出错,提示错误为"unexpected #else".此时只要将"#else if"置换成"#elif"即可:
   
    #if _MSC_VER < 1202
      //EVC 4.0 complier is cross 1200 - 1202
    #elif _MSC_VER >= 1400
      //Visual C++ 2005 complier is 1400
    #else
      //Visual C++ 2005 complier is 1400
    #endif
   
    上面代码可以顺利编译通过.
   

03) 包含上级目录的某个头文件
    如果当前文件需要包含上级目录的某个头文件,可采用"..\\"形式,比如:
    #include "..\\Configure.h"
   
    甚至还可以层层递推,定位于上上级目录:
    #include "..\\..\\Configure.h"
   
   
04) 包含当前目录的某个文件夹下的头文件
    如果当前文件需要包含当前目录下的某个文件夹中的某个头文件,可采用".\\"形式,比如:
    #include ".\\Include\\Configure.h"
   
   
05) 判断当前CPU类型
 #ifdef _X86_
    //x86
 #endif

 #ifdef _MIPS_
   //mips

#endif

你可能感兴趣的:(C++,c,windows,C#,mfc)