C语言 将包含语句、标题或源文件放在哪里?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3943352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Where to put include statements, header or source?
提问by Mohit Deshpande
Should I put the includes in the header file or the source file? If the header file contains the include statements, then if I include that header file in my source, then will my source file have all of the included files that were in my header? Or should I just include them in my source file only?
我应该将包含文件放在头文件还是源文件中?如果头文件包含 include 语句,那么如果我在源文件中包含该头文件,那么我的源文件是否会包含头文件中的所有包含文件?或者我应该只将它们包含在我的源文件中?
回答by schot
Only put includes in a header if the header itself needs them.
如果标题本身需要它们,则仅将包含放在标题中。
Examples:
例子:
- Your function returns type
size_t. Then#include <stddef.h>in the headerfile. - Your function uses
strlen. Then#include <string.h>in the sourcefile.
- 您的函数返回 type
size_t。然后#include <stddef.h>在头文件中。 - 您的函数使用
strlen. 然后#include <string.h>在源文件中。
回答by Jerry Coffin
There's been quite a bit of disagreement about this over the years. At one time, it was traditional that a header onlydeclare what was in whatever module it was related to, so manyheaders had specific requirements that you #includea certain set of headers (in a specific order). Some extremely traditional C programmers still follow this model (religiously, in at least some cases).
多年来,关于这一点存在相当多的分歧。曾经,传统上一个头文件只声明它所关联的任何模块中的内容,所以许多头文件都有特定的要求,你需要#include一组特定的头文件(按特定顺序)。一些非常传统的 C 程序员仍然遵循这个模型(至少在某些情况下是宗教上的)。
More recently, there's a movement toward making most headers standalone. If that header requires something else, the header itself handles that, ensuring that whatever it needs is included (in the correct order, if there are ordering issues). Personally, I prefer this -- especially when the order of headers can be important, it solves the problem once, instead of requiring everybody who uses it to solve the problem yet again.
最近,有一种使大多数标题独立的运动。如果该标头需要其他内容,则标头本身会处理该问题,确保包含所需的任何内容(以正确的顺序,如果存在排序问题)。就我个人而言,我更喜欢这个——尤其是当标题的顺序很重要时,它解决了一次问题,而不是要求每个使用它的人再次解决问题。
Note that most headers should only contain declarations. This means adding an unnecessary header shouldn't (normally) have any effect on your final executable. The worst that happens is that it slows compilation a bit.
请注意,大多数标题应该只包含声明。这意味着添加不必要的标头(通常)不会对您的最终可执行文件产生任何影响。最糟糕的情况是它会稍微减慢编译速度。
回答by David Thornley
Your #includes should be of header files, and each file (source or header) should #includethe header files it needs. Header files should #includethe minimum header files necessary, and source files should also, though it's not as important for source files.
你的#includes 应该是头文件,每个文件(源或头)应该#include是它需要的头文件。头文件应该#include是必需的最少头文件,源文件也应该是必要的,尽管它对于源文件并不重要。
The source file will have the headers it #includes, and the headers they #include, and so on up to the maximum nesting depth. This is why you don't want superfluous #includes in header files: they can cause a source file to include a lot of header files it may not need, slowing compilation.
源文件将具有标题 it #includes 和标题 they #include,依此类推,直到最大嵌套深度。这就是为什么你不想#include在头文件中使用多余的s:它们会导致源文件包含大量它可能不需要的头文件,从而减慢编译速度。
This means that it's entirely possible that header files might be included twice, and that can be a problem. The traditional method is to put "include guards" in header files, such as this for file foo.h:
这意味着头文件完全有可能被包含两次,这可能是一个问题。传统的方法是在头文件中放置“包含守卫”,例如文件 foo.h:
#ifndef INCLUDE_FOO_H
#define INCLUDE_FOO_H
/* everything in header goes here */
#endif
回答by David Thornley
The approach I have evolved into over twenty years is this;
我二十多年来演变成的方法是这样的;
Consider a library.
考虑一个图书馆。
There are multiple C files, one internal H file and one external H file. The C files include the internal H file. The internal H file includes the external H file.
有多个C文件,一个内部H文件和一个外部H文件。C 文件包括内部 H 文件。内部H文件包括外部H文件。
You see that from the compilers POV, as it compiles a C file, there is a hierarchy;
您可以从编译器 POV 中看到,当它编译 C 文件时,存在层次结构;
external -> internal -> C code
外部 -> 内部 -> C 代码
This is the correct ordering, since that which is external is everything a third party needs to use the library. That which is internal is required to compile the C code.
这是正确的顺序,因为外部是第三方使用库所需的一切。编译C代码需要内部的东西。
回答by supercat
In some environments, compilation will be fastest if one only includes the header files one needs. In other environments, compilation will be optimized if all source files can use the same primary collection of headers (some files may have additional headers beyond the common subset). Ideally, headers should be constructed so multiple #include operations will have no effect. It may be good to surround #include statements with checks for the file-to-be-included's include-guard, though that creates a dependency upon the format of that guard. Further, depending upon a system's file caching behavior, an unnecessary #include whose target ends up being completely #ifdef'ed away may not take long.
在某些环境中,如果只包含所需的头文件,则编译速度将是最快的。在其他环境中,如果所有源文件都可以使用相同的主要头文件集合(某些文件可能具有超出公共子集的附加头文件),则编译将得到优化。理想情况下,应构造标头,以便多个 #include 操作无效。用检查要包含的文件的包含保护来包围 #include 语句可能会很好,尽管这会产生对该保护格式的依赖。此外,根据系统的文件缓存行为,目标最终被完全#ifdef 删除的不必要的#include 可能不会花很长时间。
Another thing to consider is that if a function takes a pointer to a struct, one can write the prototype as
另一件要考虑的事情是,如果一个函数接受一个指向结构的指针,那么可以将原型写为
void foo(struct BAR_s *bar);
without a definition for BAR_s having to be in scope. A very handy approach for avoiding unnecessary includes.
没有 BAR_s 的定义必须在范围内。避免不必要的包含的一种非常方便的方法。
PS--in many of my projects, there will be a file which it's expected that every module will #include, containing things like typedefs for integer sizes and a few common structures and unions [e.g.
PS--在我的许多项目中,都会有一个文件,预计每个模块都将#include,其中包含诸如整数大小的 typedef 和一些常见结构和联合之类的内容 [例如
typedef union {
unsigned long l;
unsigned short lw[2];
unsigned char lb[4];
} U_QUAD;
(Yes, I know I'd be in trouble if I moved to a big-endian architecture, but since my compiler doesn't allow anonymous structs in unions, using named identifiers for the bytes within the union would require that they be accessed as theUnion.b.b1 etc. which seems rather annoying.
(是的,我知道如果我转移到大端架构,我会遇到麻烦,但由于我的编译器不允许联合中的匿名结构,对联合中的字节使用命名标识符将要求它们被访问为theUnion.b.b1 等,这看起来很烦人。
回答by Vicky
If header file A #includesheader files B and C, then every source file that #includesA will also get B and C #included. The pre-processor literally just performs text substitution: anywhere it finds text that says #include <foo.h>it replaces it with the text of foo.hfile.
如果头文件 A#includes头文件 B 和 C,那么每个源文件#includesA 也会得到 B 和 C #included。预处理器实际上只是执行文本替换:任何它找到的文本表示#include <foo.h>它用foo.h文件的文本替换它。
There are different opinions on whether you should put #includesin headers or source files. Personally, I prefer to put all #includesin source file by default, but any header files that cannot compile without other pre-requisite headers should #includethose headers themselves.
关于应该放入#includes头文件还是源文件,有不同的意见。就个人而言,我更喜欢#includes默认情况下将所有内容放在源文件中,但是如果没有其他先决条件头文件则无法编译的任何头文件都应该#include是这些头文件本身。
And every header file should contain an include guard to prevent it being included multiple times.
并且每个头文件都应该包含一个包含保护以防止它被多次包含。
回答by rerun
Make all of your files so that they can be built using only what they include. If you don't need an include in your header remove it. In a big project if you don't maintain this discipline you leave yourself open to breaking an entire build when someone removes an include from a header file that is being used by a consumer of that file and not even by the header.
制作所有文件,以便仅使用它们包含的内容来构建它们。如果您不需要包含在标题中,请将其删除。在一个大项目中,如果你不遵守这个纪律,当有人从一个头文件中删除一个头文件时,你会破坏整个构建,而头文件的使用者正在使用该文件,甚至没有被头文件使用。
回答by JRam930
You should only include files in your header that you need to declare constants and function declarations. Technically, these includes will also be included in your source file, but for clarity sake, you should only include in each file the files you actually need to use. You should also protect them in your header from multiple inclusion thusly:
你应该只在你的头文件中包含你需要声明常量和函数声明的文件。从技术上讲,这些包含也将包含在您的源文件中,但为了清楚起见,您应该只在每个文件中包含您实际需要使用的文件。您还应该保护它们在您的标题中不被多次包含:
#ifndef NAME_OF_HEADER_H
#define NAME_OF_HEADER_H
...definition of header file...
#endif
This prevents the header from being included multiple times, resulting in a compiler error.
这可以防止多次包含标头,从而导致编译器错误。
回答by Splashdust
Your source file will have the include statements if your put it in the header. However, in some cases it would be better to put them in the source file.
如果你把它放在标题中,你的源文件将包含包含语句。但是,在某些情况下,最好将它们放在源文件中。
Remember that if you include that header in any other sources, they will also get the includes from the header, and that is not always desirable. You should only include stuff where it is used.
请记住,如果您在任何其他来源中包含该标头,它们也会从标头中获取包含,这并不总是可取的。你应该只包含使用它的东西。

