#ifdef、#ifndef、#endif使用说明
目的:防止头文件重复include
示例说明:
a.h
1  | #include <stdio.h>  | 
b.h
1  | #include "a.h"  | 
c.c
1  | #include "a.h"  | 
如果你程序是这样写的话,编译器就会出现Error #include nested too deeply的错误。
因为这里 b.h 和 a.h 都互相include,c.c文件在include的时候重复include了a.h,我们希望c.c文件中执行#include "b.h"的时候 b.h 能进行判断,如果没有#include "a.h"则include,如果已经include了,则不再重复定义。
可以将b.h修改为:
1  | #ifndef _A_H  | 
原因是: > c.c中先include了a.h文件,其中a.h中又包括了b.h,所以会定义宏_A_H,当c,c中又includeb,h时判断_A_H是否已经被定义了,如果被定义了,则不再includea.h
Author: Mrli
Link: https://nymrli.top/2018/12/08/ifdef、-ifndef、-endif/
Copyright: All articles in this blog are licensed under CC BY-NC-SA 3.0 unless stating additionally.