【C】.h头文件的重复包含问题

.h头文件存在的意义就是封装,可以方便多个.c源文件使用,但要防止.h头文件被同一个.c源文件多次包含。  

例如,

io.h文件

1 #ifndef _IO_H_
2 #define _IO_H_
3 #define HOLENUM   15
4 int  HoleTemp;
5 void  Get_Temp(unsigned char HoleID);
6 #endif

uart.h文件

#ifndef  _UART_H_
#define _UART_H_
#include <io.h>
#endif

main.c文件

 1 #include<io.h>
 2 #include<usb.h> 
 3 void AutoCtrlTemp(void)
 4 {
 5 //...
 6 }
 7 main()
 8 {
 9    AutoCtrlTemp();
10 } 

其中,在.h中定义的这个#ifndef ...#define...#endif的作用域是多大呢?/整个project。

usb.h包含了io.h,main.c包含了usb.h又包含了io.h,明显的重复包含了io.h,此时如果不加#ifdef,会出现大量的重复定义错误。

/*生命如此美好。认真工作之余,不要忘了认真对待生活,认真对待身边人!*/
原文地址:https://www.cnblogs.com/isha2088/p/6252008.html