#program 与 #ifndef #endif区别

本文探讨了在C/C++编程中,使用#ifndef与#pragma once防止头文件重复包含的不同之处。通过具体示例展示了两者在处理过程上的差异,以及#pragma once在提高编译速度方面的优势。

#program 与 #ifndef #endif区别

转自:http://blog.youkuaiyun.com/WINCOL/archive/2009/10/30/4748225.aspx
在用VC6.0向导生成的头文件中,经常可以看见如下的代码段:
    #ifndef AFX_RESIZABLELAYOUT_H__INCLUDED_
    #define AFX_RESIZABLELAYOUT_H__INCLUDED_
  
    #if _MSC_VER > 1000
    #pragma once
    #endif // _MSC_VER > 1000
  
    ...   
  
    #endif // !defined(AFX_RESIZABLELAYOUT_H__INCLUDED_)
对于宏有基本了解的朋友应该都知道,头文件中如下的宏定义,是为了避免同样的头文件在同一个.C文件或者.CPP文件多次包含。
    #ifndef XXX
    #define XXX
  
    #endif

这很好理解,但接下来的一段,尤其是#pragma once的意思,我就不是很清楚了。从MSDN得到pragma once的解释是:
    "Specifies that the file will be included (opened) only once by the compiler when compiling a source code file."
英文注释的大意也是说#pragma once是为了避免文件重复包含。疑惑就此产生了,既然宏"#ifndef"已经有这个作用了,为何还要一个"#pragma once"呢? 我接着在网上搜到了几份答案,但大家的回答都很模糊,于是我想放弃,不再想这个问题,但还是不太甘心,就接着看了看别人的解释。突然间,好像灵犀一点,开窍了。虽然"#ifndef"和"#pragma once"都有避免重复包含的功能,但是在实现上还是有区别的。举一例如下:
    // Test1.h
    #ifndef __TESTONE_H_
    #define __TESTONE_H_
    ...
    #endif
  
    // Test2.h
    #pragma once      
    ...
  
    // Test.cpp
    #include "Test1.h"      // line 1
    #include "Test1.h"      // line 2
    #include "Test2.h"      // line 3
    #include "Test2.h"      // line 4
  
    ...
  
     头文件Test1.h中用宏来避免重复,头文件Test2.h中用#pragma once来避免重复。编译Test.cpp,将需要打开Test1.h两次,第一次发现宏__TESTONE_H_没有定义,接着就处理宏定义;第二次打开Test1.h时,发现宏__TESTONE_H_已经定义过了,编译器就会略过宏定义部分,知道处理完Test1.h末尾的#endif。
     而由于头文件Test2.h使用#pragma once来避免重复定义的,在编译Test.cpp的过程中,Test2.h只会被打开一次,也就是处理到第3行的时候。因为Test2.h用的是#pragma once,所以在处理完第3行后,编译器已经知道包含了一次Test2.h,在它(编译器)处理第4行代码时,发现Test2.h已经包含过了,忽略掉第 4行代码,也就不需要再次打开Test2.h进行判断了。
      总结一下,除了#pragma once是微软编译器所特有的之外,用宏和#pragma once的办法来避免重复包含头文件,主要区别在于宏处理的方法会多次打开同一头文件,而#pragma once则不会重复打开,从而#pragma once能够更快速。

把下面的window代码修改成移植到linux代码#if defined (_MSC_VER) && (_MSC_VER >= 1000) #pragma once #endif #ifndef _HEADER_BASESYSTEMCONFIG #define _HEADER_BASESYSTEMCONFIG #if _MSC_VER >= 1400 # ifndef _CRT_SECURE_NO_DEPRECATE # define _CRT_SECURE_NO_DEPRECATE # endif // [11/8/2006 Tgame.Tang] //# ifndef _DEBUG //# ifndef _SECURE_SCL //# define _SECURE_SCL 0//在release下关闭STL的安全特性,提高效率 //# else //# if _SECURE_SCL != 0 //# error("_SECURE_SCL must be defined as 0 while compile no debug version,You must include stl after include this file") //# endif //# endif //# endif #endif #pragma warning(disable:4995) #pragma warning (disable:4251) //!系统平台 #if defined(_WIN32) // // Comment out the following block if you want to run on Windows 9x // or Windows NT 3.51. // # ifndef _WIN32_WINNT // // Necessary for TryEnterCriticalSection. // # define _WIN32_WINNT 0x0500 # endif #define WIN32_LEAN_AND_MEAN // 从 Windows 头中排除极少使用的资料 # include <windows.h> # include <winsock2.h> # pragma comment( lib, "wsock32.lib" ) # pragma comment( lib, "Ws2_32.lib" ) #elif (defined(__sun) && defined(__sparc)) || (defined(__hpux)) # include <inttypes.h> #else // // The ISO C99 standard specifies that in C++ implementations the // macros for minimum/maximum integer values should only be defined if // explicitly requested with __STDC_LIMIT_MACROS. // # ifndef __STDC_LIMIT_MACROS # define __STDC_LIMIT_MACROS # endif # include <stdint.h> #endif //!字节序 #if defined(__i386) || defined(_M_IX86) || defined (__x86_64) # define SERIAL_LITTLE_ENDIAN #elif defined(__sparc) || defined(__sparc__) || defined(__hppa) || defined(__ppc__) || defined(_ARCH_COM) # define SERIAL_BIG_ENDIAN #else # error "Unknown architecture" #endif //!STL #include <vector> #include <string> #include <cassert> #include <iostream> #include <sstream> #include <string> #include <map> #include <queue> #include <stack> #include <list> #ifdef _Foundation_Import_ # define _Foundation_Export_ __declspec( dllimport ) #else # define _Foundation_Export_ __declspec( dllexport ) //# pragma warning (disable:4251) #endif //#define _Disable_Vt_ #endif
09-28
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值