#program 与 #ifndef #endif区别
来源:互联网 发布:淘宝最近没生意 编辑:程序博客网 时间:2024/05/22 05:05
#program 与 #ifndef #endif区别
转自:http://blog.csdn.net/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能够更快速。
- #program 与 #ifndef #endif区别
- #program 与 #ifdef #endif区别
- #program 与 #ifdef #endif区别
- #pragma与#ifndef #endif相同点、区别、缺陷
- #ifndef 与 #program once 的区别
- #ifndef 与 #program once 的区别
- (转)#ifndef 与 #program once 的区别
- #ifndef 与 #program once 的区别
- #ifndef 与 #program once 的区别
- #ifndef 与 #program once 的区别
- (转)#ifndef 与 #program once 的区别
- #ifndef 与 #program once 的区别
- #ifndef 与 #program once 的区别
- #pragma once与#ifndef #define ...#endif的区别
- #pragma once与#ifndef #define ...#endif的区别
- #pragma once与#ifndef #define ...#endif的区别
- #pragma once与#ifndef #define ...#endif的区别
- #pragma once与#ifndef #define #endif的区别
- makefile教程
- visual studio 2010 sp1 安装出现DotfuscatorCE_5.0.2500.msi 未找到错误!
- cts测试的一些总结
- old bird
- 利用JS获取本机IP并提交的方法
- #program 与 #ifndef #endif区别
- Why 802.11ac Will Kill The 5 GHz Wi-Fi Band
- Linux系统日志管理
- hdu Employment Planning (dp)
- 总结c++的“编译、链接”时引发的血案——动态、静态链接库
- 驱动笔记
- 总结extern “C”时引发的血案——c++的“编译、链接”原理与过程
- Java 线程总结
- extern “C”再次总结