#ifndef/#define/#endif使用详解
来源:互联网 发布:julian moore知乎 编辑:程序博客网 时间:2024/05/16 01:03
头文件被重复引用引起的后果:
有些头文件重复引用只是增加了编译工作的工作量,不会引起太大的问题,仅仅是编译效率低一些,但是对于大工程而言编译效率低下那将是一件多么痛苦的事情。
有些头文件重复包含,会引起错误,比如在头文件中定义了全局变量(虽然这种方式不被推荐,但确实是C规范允许的)这种会引起重复定义。
是不是所有的头文件中都要加入#ifndef/#define/#endif 这些代码?
答案:不是一定要加,但是不管怎样,用ifnde xxx #define xxx#endif或者其他方式避免头文件重复包含,只有好处没有坏处。个人觉得培养一个好的编程习惯是学习编程的一个重要分支。
下面给一个#ifndef/#define/#endif的格式:
#ifndef A_H意思是"if not define a.h" 如果不存在a.h
接着的语句应该#define A_H 就引入a.h
最后一句应该写#endif 否则不需要引入
--------------------------------------------------------------------------------------------------
#ifndef GRAPHICS_H // 防止graphics.h被重复引用
#define GRAPHICS_H
#include <math.h> // 引用标准库的头文件
…
#include “header.h” // 引用非标准库的头文件
…
void Function1(…); // 全局函数声明
…
class Box // 类结构声明
{
…
};
#endif
--------------------------------------------------------------------------------------------------
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- #ifndef/#define/#endif使用详解
- 叶师傅论小问题
- linux awk命令详解(二) awk选项含义
- 贪心算法----背包问题
- 位运算总结1. 二进制的逆置
- 知其然亦知其所以然-NSOperation并发编程
- #ifndef/#define/#endif使用详解
- HDU 1028 整数拆分 (母函数_模板|| 背包)
- 员工费用报销冲减员工借款怎么做账
- 虚拟机上centos7静态IP的配置
- 在内部网络为edX配置视频服务
- UITableView UICollectionView经验积累
- Java 多态总结
- Objective-C 1概述
- 【google M】开机SIM卡信息的初始化