浅谈#ifndef~~~~#define~~~~~#endif~~~~~的作用
来源:互联网 发布:台州网络平台赌博 编辑:程序博客网 时间:2024/05/21 20:04
/*
#ifndef _STDAFX_H
#define _STDAFX_H
#include<iostream.h> //cin cout
#include<malloc.h> //malloc free
#include<string.h> //strcpy
#include<stdlib.h> //exit()
#endif
*/
(1)内容:
在头文件(StdAfx.h)开头写上两行:
(2)作用:
i 主要目的是防止头文件的重复包含和编译
例如:
假设你的工程里面有4个文件,分别是 a.cpp , b.h, c.h, d.h。
a.cpp的头部是:
#include
#include
b.h和c.h的头部都是:
#include
而d.h里面有int a,即a的定义。
这样一来,
编译器编译a.cpp的时候,先根据#include
然后再根据a.cpp的第二句#include
(3)另外还有一点需要注意:
在其他文件如main.cpp在使用StdAfx.h的同时又用到其他函数库时,要注意函数库放的位置
如:
main.cpp:
#include<stdio.h>
#include"StdAfx.h"
.........
运行时会出现错误!!!!原因:
编译器通过一个头文件stdafx.h来使用预编译头文件。编译器认为,所有在指令#include"stdafx.h"前的代码都是预编译的,它跳过#include"stdafx.h"指令,使用projectname.pch编译这条指令之后的所有代码。
因此,所有的MFC实现文件第一条语句都是:#include"stdafx.h"。在它前面的所有代码将被忽略,所以其他的头文件应该在这一行后面被包含。否则,你将会得到“Nosuchfileordirectory”这样让你百思不得其解的错误提示。
所以要把#include"StdAfx.h"放在最前面(下面是正确的形式)
main.cpp:
#include"StdAfx.h"
#include<stdio.h>
.........
- 浅谈#ifndef~~~~#define~~~~~#endif~~~~~的作用
- 浅谈#ifndef~#define~#endif~的作用
- ifndef define endif的作用
- ifndef/define/endif的作用
- #ifndef #define #endif的作用
- #ifndef #define #endif的作用
- ifndef/define/endif的作用
- 浅谈C语言中"#ifndef/#define/#endif"的作用
- #ifndef,#define #endif作用
- #ifndef, #define, #endif 作用
- #ifndef #define #endif作用
- #ifndef, #define, #endif 作用
- #ifndef, #define, #endif 作用
- #ifndef, #define, #endif 作用
- C++中 #ifndef #define #endif的作用
- #ifndef、#ifdef、#define、#endif的作用
- 头文件中的ifndef/define/endif的作用
- 头文件中的ifndef/define/endif的作用
- Java并发学习笔记(二)-Executor捕获异常机制
- iOS项目的完整重命名方法图文教程
- shell实例浅谈之产生随机数七种方法
- struts2中 JFreeChart使用
- shell实例浅谈之二批量创建文件并重新命名
- 浅谈#ifndef~~~~#define~~~~~#endif~~~~~的作用
- 收集关于Github的一些资料
- 算是第一天吧【健身】
- makefile 模版
- shell实例浅谈之判断是可执行文件并存在PATH路径中
- nginx的spdy协议
- 模板类 stack 的理解 积累
- 【官方文档】Cocos2d-x 3.0 Json用法
- 学习SinglePHP