C++中防止头文件被多次include 的常见方式有:
1) 用#ifndef ... #define ... #endif 宏
#ifndef __MYHEADER_H__
#define __MYHEADER_H__
// Here is my class ...
#endif
2) 用 #pragma once
#pragma once
// Here is my class ...
建议使用 #pragma once ,原因:
1) 代码简洁,维护性比较好。 #pragma once 显然比 #ifndef ... 要简短许多,而且避免了__MYHEADER_H__ 重定义 或者 #endif 包含范围错误的情况
2) 编译速度快。 使用 #ifndef 的话,编译器每次看到#include这个文件都需要读入文件,解析代码。 而使用#pragma once 编译器根本不会重复打开文件, 大大提高了效率。
以上两点在大型C++工程中表现得尤为突出。
注: #pragma once 不是国际标准, 只是微软C++ 的一个功能, 但现在多数主流C++编译器也都实现了这个功能。
分享到:
相关推荐
2 #pragma once方式 在能够支持这两种方式的编译器上,二者并没有太大的区别,但是两者仍然还是有一些细微的区别。 方式一: #ifndef __SOMEFILE_H__ #define __SOMEFILE_H__ … … // 一些声明语句 #endif 方式二...
C++ 中pragma once 与 #ifndef _XXX_H_ #define _XXX_H_的区别 pragma once 之前一直用的很好,今天和同事的配合中发现自己没有真正理解pragma once。 原因:同事喜欢把公共的头文件通过生成后事件复制到一个公共的...
#ifndef SPMETA_DLL_H #include "spmeta.h" #endif #ifndef _SLA_CHALLENGE_H_ #include "sla_challenge.h" #endif #ifndef _MULTIPORT_COMMON_H_ #include "Multiport_Common.h" #endif #ifndef _FORM_MULTILOAD...
#pragma once #endif #ifndef __cplusplus #error You must use C++ compiler, or you need filename with '.cpp' suffix #endif #if defined(_INC_CONIO) || defined(_CONIO_H_) #error can not include "conio.h...
#pragma once #endif #ifndef _INC_EH #define _INC_EH #if !defined(_WIN32) && !defined(_MAC) #error ERROR: Only Mac or Win32 targets supported! #endif #ifdef _MSC_VER // Currently, all MS C ...
c++/c中 externC. ifndef define endif 的用法
//#pragma once #ifndef _clock_8563_pic24FJ #define _clock_8563_pic24FJ #define SDA_OUT LATDbits.LATD9 #define SDA_IN PORTDbits.RD9 #define SDA_DDR TRISDbits.TRISD9 #define SCK ...
使用这个小工具,可将指定目录及其子目录下的全部LIB文件自动生成为含完整路径的“#pragma comment(lib,”语句,不需要在VS环境设置LIB目录。 示例: #ifndef D__VTK820_Debug_lib #define D__VTK820_Debug_lib ...
#pragma once #endif // _MSC_VER >= 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC ...
#pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC ...
#pragma once #endif // _MSC_VER > 1000 #define VC_EXTRALEAN // Exclude rarely-used stuff from Windows headers #include <afxwin.h> // MFC core and standard components #include <afxext.h> // MFC ...
#pragma once #endif // _MSC_VER >= 1000 // Return values #ifndef BTNST_OK #define BTNST_OK 0 #endif #ifndef BTNST_INVALIDRESOURCE #define BTNST_INVALIDRESOURCE 1 #endif #ifndef BTNST_FAILEDMASK #...
浙江海康威视硬盘录像机播放器DEMO / stdafx.h : 标准系统包含文件的包含文件, // 或是经常使用但不常...#pragma once #ifndef VC_EXTRALEAN #define VC_EXTRALEAN // 从 Windows 标头中排除不常使用的资料 #endif
#pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols //////////////////////////////...
#if #define #undef #if #ifdef #ifndef #else #elif #endif #define USE_LIGHT #define LIGHT_NUMBER 10 ... #ifdef USE_LIGHT .... #else ... #endif #error #pragma #pragma STDGL invariant(all) //保证所有...
#pragma once #endif // _MSC_VER >= 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols ////////////////////////////...
#pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols //////////////////////////////...
#pragma once #endif // _MSC_VER > 1000 #ifndef __AFXWIN_H__ #error include 'stdafx.h' before including this file for PCH #endif #include "resource.h" // main symbols /////////////////////////////...