单个头文件包含所有必需的#include语句 [英] Single header file with all the necessary #include statements

查看:151
本文介绍了单个头文件包含所有必需的#include语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在开发具有大量源文件的程序。有时很难跟踪我已经有 #include d的库。理论上,我可以做一个单一的头文件 Headers.h 只包含所有 #include 然后使所有其他头文件 #includeHeaders.h

I am currently working on program with a lot of source files. Sometimes it is difficult to keep track of what libraries I have already #included. Theoretically, I could make a single header file called Headers.h that just contains all the #include statements I need, then make all other header files #include "Headers.h".

为什么这是一个好主意?

Why is this a good/bad idea?

推荐答案

优点:


  • 稍微少了维护,因为您不必跟踪哪些文件包含来自哪些库或其他组件的标题。

缺点:


  • 包含的文件中的定义可能会彼此冲突。特别是在C中,你没有命名空间(用C和C ++标记)

  • 宏特别会导致难以调试的问题,其中宏定义意外地与您的文件或其他包含的文件之一

  • 根据使用的编译器,编译时间可能会爆炸。如果使用预编译头的编译器,它实际上可能会减少编译时间,但如果没有相反的结果,则会出现

  • 您经常会不必要地触发文件的重建。如果您的构建系统设置正确,那么每个源文件将被重建,如果任何包含的文件被修改。如果您始终在项目中包含所有头文件,那么对任何头文件的更改都将强制重新编译所有源文件。不太可能是系统标题的问题,但是如果您在主文件中也包含自己的标题,则也是如此。

  • Definitions in included files might conflict with each other. Especially in C where you don't have namespaces (you tagged with C and C++)
  • Macros in particular can cause hard to debug problems, where a macro definition unexpectedly conflicts with some name in your file or one of the other included files
  • Depending on which compiler you use, compilation times might blow out. If using a compiler that pre-compiles headers it might actually reduce compilation time, but if not the opposite will happen
  • You will often unnecessarily trigger rebuilds of files. If you have your build system set up correctly, then each source file will get rebuilt if any of the included files gets modified. If you always include all headers in your project, then a change to any of your headers will force recompilation of all your source files. Not likely to be an issue for system headers but it will be if you include your own headers in the master file as well.

开整个我不会推荐这种方法。上面列出的最后一个特别重要。

On the whole I would not recommend that approach. The last con listed above it particularly important.

最佳做法是只包含每个文件中代码所需的标题。

Best practice would be to include only headers that are needed for the code in each file.

这篇关于单个头文件包含所有必需的#include语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆