将所有包含内容放在一个头文件中是个好主意吗?

发布于 2024-10-19 06:36:46 字数 117 浏览 6 评论 0原文

对于 C 来说,放入 C 头文件中的内容的最佳实践是什么?

将跨多个源文件的程序使用的所有包含内容放在一个头文件中是否有用?

几乎每个文件(即 stdio.h)中都使用的包含内容又如何呢?

What is the best practice for C what you put into a C header file?

Is it useful to put all the includes used for a program across multiple source files in one header file?

What about includes that are used in practically every file (i.e. stdio.h)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

走野 2024-10-26 06:36:46

不会。它只会增加麻烦和开销。

作为维护者,您将面临的最大痛苦之一是弄清楚哪些标头不需要包含并删除它们。当您看到包含 20 多个标头的列表时,您开始考虑丑陋的事情,例如暴力破解(一次取下一个,看看是否有任何损坏)。

善待将来必须维护您的东西的人。使用每个模块所需的标头,不多..不少:)

No. It simply adds cruft and overhead.

One of the biggest pains that you'll face as a maintainer is figuring out what headers don't need to be included and get rid of them. Whe you get to a list of 20+ headers, you start contemplating ugly things like brute forcing through it (removing one at a time and seeing if anything breaks).

Be kind to people who have to maintain your stuff in the future. Use the headers you need per module, no more .. no less :)

小姐丶请自重 2024-10-26 06:36:46

将所有可能的标头放在一个应用程序标头中是非常错误的。

这种懒惰付出了巨大的代价。它使构建变得脆弱。它使得很难理解真正的依赖关系在哪里,因此很难重构或以其他方式重用代码。

这使得测试变得困难。

但最大的问题是,它代表了智力上的懒惰,并鼓励更多人这样做。

正如所有编程问题一样,做需要做的事情,不多也不少。考虑维护。考虑构建管理。

想想吧。

Putting all possible headers in one application header is as wrong as wrong can be.

This is laziness that comes at a great price. It makes builds brittle. It makes it hard to understand where true dependencies are, and thus hard to refactor or otherwise re-use code.

It makes it hard to test.

But the biggest problem is that it represents intellectual laziness and encourages more of the same.

As will all programming issues, do what is needed, no more, no less. Think about maintainence. Think about build management.

Just THINK.

樱花落人离去 2024-10-26 06:36:46

我个人赞同“把它放在你使用的地方”的理念。它使哪些文件使用什么、依赖项在哪里等变得更加清晰。

假设您有一个标头 MyHeader.h。如果您对其进行更改,需要更改依赖于它的代码,那么如果使用它的每个文件都有 #include "MyHeader.h" ,那么很容易找到依赖代码 - 您可以这样做全局搜索 include 语句。

另一方面,如果您仅在其他标头 MyHugeHeader.h 中包含 MyHeader.h,然后在您的文件中包含,则可以'不必执行相同的操作,因为使用 MyHeader.h 的文件中的所有内容都是 #include "MyHugeHeader.h",与所有其他文件相同。

I personally subscribe to a "put it where you use it" philosophy. It makes it much clearer which files use what, where dependencies are, etc.

Imagine you have a header MyHeader.h. If you make a change in it that requires changing code that relies on it, it's easy to find that dependent code if each file that uses it has a #include "MyHeader.h" - you can just do a global search for the include statement.

If on the other hand you only include MyHeader.h in some other header MyHugeHeader.h, and then include that in your files you can't do the same, since all that's in the files that use MyHeader.h is #include "MyHugeHeader.h", same as every other file.

叹梦 2024-10-26 06:36:46

有些事情尚未指出:

  • 不需要的依赖项会增加编译时间。当您修改标头时,您必须重新编译直接或间接包含该标头的所有编译单元。如果不需要包含,则也不需要重新编译。您的程序越大,问题就越大,特别是如果您将程序分解为必须手动触发重新编译的组件。

  • 当您添加不需要的依赖项时,预编译器头可能会更有效。

Some things not already pointed out:

  • unneeded dependancies add to the compile time. When you modify an header, you'll have to recompile all compilation units which include it directly or indirectly. If the inclusion is not needded, the recompilation isn't either. The larger your program is, the bigger will be the problem, especially if you have broken your programs into composants whose recompilation your have to trigger manually.

  • pre-compiler headers may be more efficient when you add unneeded dependencies.

一抹淡然 2024-10-26 06:36:46

个人喜好确实...
只要保持一致(这使其易于阅读),格式如何并不重要。您可以将其全部放入 1 个头文件中,也可以将其放入每个需要它的文件中。它还取决于其他包含文件的加载方式,主文件之后的任何内容都不需要它的包含文件等等,因此对所有其他 C 文件使用相同的头文件可能会或可能不会(取决于编译器)包含相同的包含多个次。

编辑:我也同意Mac的观点,把它放在你使用的地方也是一件非常非常好的事情

Personal preference really...
It doesn't matter how it is formatted as long as you are consistent (which makes it easy to read). You could put it all in 1 header file or you can just put it in every file that needs it. It also depends on how other includes are loaded anything that comes after the main doesn't need its includes and so on so using the same header file for ALL your other C files may or may not (depends on compiler) include the same include multiple times.

Edit: I have to agree with Mac too, put it where you use it is a very, very nice thing to do as well

亚希 2024-10-26 06:36:46

我不会故意将所有包含文件放入一个头文件中,因为如果您更改这些包含头文件之一,则必须重新编译包含“主”包含文件的所有内容。这可能会导致单行更改的编译时间过长。

也就是说,我不会花太多时间来确保我不会对 include 语句过于自由。一些工程师会花费大量时间尝试减少包含以节省编译时间,我认为他们的时间最好用在解决问题或开发新功能上。

I wouldn't purposely put all my includes into a single header file, simply because if you change one of these included header files, you'll have to recompile everything that has included your "master" include file. This can lead to unnecessarily long compilation times for a single line change.

That said, I wouldn't put too much time in making sure I'm not being too liberal with include statements. Some engineers will spend lots of time trying to reduce includes to save on compilation time, and I think their time is better served solving problems or working on new features.

鼻尖触碰 2024-10-26 06:36:46

头文件实际上是您从 C 程序中包含的内容。它包含结构、宏、函数定义等数据结构。有时单个头文件就可以了,如果你的程序成长为逻辑组件,你可能需要更多。

A header file is actually what you include from your c program. It containts data structures like structs, macros, function definitions etc.. Sometimes a single header file can be ok, buy if your program grows into logical components, you may need more.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文