Error #include nested too deeply

转载:http://blog.csdn.net/ysdaniel/article/details/7043395

出现 Error #include nested too deeply 原因是:

头文件相互包含。

例如,一个工程中bsp.h 包含 LocDongle.h, LocDongle.h又包含bsp.h,

编译时就会报Error #include nested too deeply 。

 

解决办法:

1、将两个头文件共用的那一部分抽出来单独建一个头文件。

2、加预处理#ifndef.. #define...#endif

 

  1. //bsp.h  
  2. #ifndef _BSP_H_  
  3. #define _BSP_H_  
  4. #include "LocDongle.h"  
  5. #endif  
  6.   
  7. //LocDongle.h  
  8. #ifndef _LOCDONGLE_H_  
  9. #define _LOCDONGLE_H_  
  10. #include "bsp.h"  
  11. #endif  
  1.   
  1. <span style="font-family: simsun; line-height: 23px; "></span><pre><div><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">bsp.h</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  2. </span><span style="line-height: 18px; color: rgb(0, 0, 0); ">#ifndef _BSP_H_</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">文件开始、第一行</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  3. </span><span style="line-height: 18px; color: rgb(0, 0, 255); ">#define</span><span style="line-height: 18px; color: rgb(0, 0, 0); "> _BSP_H_</span><span style="line-height: 18px; color: rgb(0, 0, 0); ">  
  4. #include </span><span style="line-height: 18px; color: rgb(128, 0, 0); ">"</span><span style="line-height: 18px; color: rgb(128, 0, 0); ">LocDongle.h</span><span style="line-height: 18px; color: rgb(128, 0, 0); ">"</span><span style="line-height: 18px; color: rgb(0, 0, 0); ">  
  5. ...</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">内容,所有的函数声明等等放这里</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  6. </span><span style="line-height: 18px; color: rgb(0, 0, 255); ">#endif</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">文件末尾</span><span style="line-height: 18px; color: rgb(0, 0, 0); ">  
  7.   
  8. </span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">LocDongle.h</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  9. </span><span style="line-height: 18px; color: rgb(0, 0, 0); ">#ifndef _LOCDONGLE_H_</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">文件开始、第一行</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  10. </span><span style="line-height: 18px; color: rgb(0, 0, 255); ">#define</span><span style="line-height: 18px; color: rgb(0, 0, 0); "> _LOCDONGLE_H_</span><span style="line-height: 18px; color: rgb(0, 0, 0); ">  
  11. #include </span><span style="line-height: 18px; color: rgb(128, 0, 0); ">"</span><span style="line-height: 18px; color: rgb(128, 0, 0); ">bsp.h</span><span style="line-height: 18px; color: rgb(128, 0, 0); ">"</span><span style="line-height: 18px; color: rgb(0, 0, 0); ">  
  12. ...</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">内容,所有的函数声明等等放这里</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  13. </span><span style="line-height: 18px; color: rgb(0, 0, 255); ">#endif</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">//</span><span style="line-height: 18px; color: rgb(0, 128, 0); ">文件末尾</span></div><div><span style="line-height: 18px; color: rgb(0, 128, 0); ">  
  14. </span></div></pre><br>  
  15. <p></p>  
  16. <pre></pre>  
  17. <br>  
  18. <span style="font-family:simsun; font-size:14px; line-height:23px">头文件保护有用, 可以这样重复包含<br>  
  19. 头文件包含其实就是在包含的位置展开它而已,<br>  
  20. 你如果a.h包含了b.h<br>  
  21. b.h又包含了a.h<br>  
  22. 如果你使用了#ifndef.. #define...#endif的话<br>  
  23. 你在一个.c文件中包含a.h那么它里面包含的b.h中包含的a.h将不会重复包含。</span><br>  
  24. <p></p>  
  25. <p><span style="font-family:simsun; font-size:14px; line-height:23px"><span style="font-family:simsun; font-size:14px; line-height:23px"><span style="font-family:simsun; font-size:14px; line-height:23px"><span style="font-family:simsun; font-size:14px; line-height:23px">头文件的嵌套一定要防止不断的包含,处于一个无限的循环!</span><br>  
  26. </span></span></span></p>  
原文地址:https://www.cnblogs.com/yunsicai/p/3472965.html