我应该在标头中使用 #include 吗?
如果在标头 (*.h) 内使用了该文件中定义的类型,是否有必要#include
某个文件?
例如,如果我使用 GLib 并希望在标头中定义的结构中使用 gchar
基本类型,是否有必要执行 #include
,知道我已经将它包含在我的 *.c 文件中了吗?
如果是,我是否还必须将其放在 #ifndef
和 #define
之间或在 #define
之后?
Is it necessary to #include
some file, if inside a header (*.h), types defined in this file are used?
For instance, if I use GLib and wish to use the gchar
basic type in a structure defined in my header, is it necessary to do a #include <glib.h>
, knowing that I already have it in my *.c file?
If yes do I also have to put it between the #ifndef
and #define
or after the #define
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
NASA 戈达德太空飞行中心 (GSFC) C 中的标头规则指出必须可以在源文件中包含一个标头作为唯一的标头,然后使用该标头提供的功能的代码将进行编译。
这意味着标头必须是自包含的、幂等的和最小的:
此规则的好处是,如果有人需要使用标头,他们不必费力地找出还必须包含哪些其他标头 - 他们知道标头提供了所需的一切。
可能的缺点是某些标头可能会被包含多次;这就是为什么多重包含标头保护至关重要(也是编译器尽可能避免重新包含标头的原因)。
实现
此规则意味着,如果标头使用某种类型(例如“
FILE *
”或“size_t
”),那么它必须确保适当的其他标头(例如,应包含
或
)。一个经常被遗忘的推论是,标头不应包含包用户使用包时不需要的任何其他标头。换句话说,标题应该最小化。此外,GSFC 规则提供了一种简单的技术来确保发生这种情况:
因此,假设我们有一个魔法排序。
magicsort.h
magicsort.c
请注意,标头必须包含一些定义
size_t
的标准标头;执行此操作的最小标准标头是
,尽管其他几个标头也这样做(
、)。 h>
、
,可能还有其他一些)。另外,如前所述,如果实现文件需要一些其他标头,那就这样吧,需要一些额外的标头是完全正常的。但实现文件(“magicsort.c”)本身应该包含它们,而不是依赖其标头来包含它们。标题应该只包含软件用户需要的内容;不是实施者所需要的。
配置标头
如果您的代码使用配置标头(例如 GNU Autoconf 和生成的“config.h”),您可能需要在“magicsort.c”中使用它:
这是我唯一一次知道该模块的私有header 不是实现文件中的第一个标头。然而,“config.h”的条件包含可能应该在“magicsort.h”本身中。
更新 2011-05-01
上面链接的 URL 不再有效 (404)。您可以在 EverySpec.com; C 标准 (582-2000-005) 似乎在行动中缺失。
C 标准的指导方针是:
GSFC 标准可通过互联网档案馆获取 2012-12-10
信息礼貌埃里克·S.布林顿:< /em>
可以通过 Internet 档案访问和下载引用的 NASA C 编码标准:
http://web.archive.org/web/20090412090730/http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard
排序
这个问题还问:
答案显示了正确的机制 - 嵌套包含等应该位于
#define
之后(并且#define
应该是标头中的第二个非注释行) ——但这并不能解释为什么这是正确的。考虑一下如果将
#include
放在#ifndef
和#define
之间会发生什么。假设另一个标头本身包含各种标头,甚至可能间接包含#include "magicsort.h"
。如果第二次包含magicsort.h
发生在#define MAGICSORT_H_INCLUDED
之前,则标头将在定义其定义的类型之前第二次包含。因此,在 C89 和 C99 中,任何typedef
类型名称都将被错误地重新定义(C2011 允许将它们重新定义为相同的类型),并且您将获得处理的开销文件多次,从一开始就违背了标头保护的目的。这也是为什么#define
是第二行,而不是写在#endif
之前。给出的公式是可靠的:NASA's Goddard Space Flight Center (GSFC) rules for headers in C state that it must be possible to include a header in a source file as the only header, and that code using the facilities provided by that header will then compile.
This means that the header must be self-contained, idempotent and minimal:
The benefit of this rule is that if someone needs to use the header, they do not have to struggle to work out which other headers must also be included — they know that the header provides everything necessary.
The possible downside is that some headers might be included many times; that is why the multiple inclusion header guards are crucial (and why compilers try to avoid reincluding headers whenever possible).
Implementation
This rule means that if the header uses a type — such as '
FILE *
' or 'size_t
' — then it must ensure that the appropriate other header (<stdio.h>
or<stddef.h>
for example) should be included. A corollary, often forgotten, is that the header should not include any other header that is not needed by the user of the package in order to use the package. The header should be minimal, in other words.Further, the GSFC rules provide a simple technique to ensure that this is what happens:
Hence, suppose we have a Magic Sort.
magicsort.h
magicsort.c
Note that the header must include some standard header that defines
size_t
; the smallest standard header that does so is<stddef.h>
, though several others also do so (<stdio.h>
,<stdlib.h>
,<string.h>
, possibly a few others).Also, as mentioned before, if the implementation file needs some other headers, so be it, and it is entirely normal for some extra headers to be necessary. But the implementation file ('magicsort.c') should include them itself, and not rely on its header to include them. The header should only include what users of the software need; not what the implementers need.
Configuration headers
If your code uses a configuration header (GNU Autoconf and the generated 'config.h', for example), you may need to use this in 'magicsort.c':
This is the only time I know of that the module's private header is not the very first header in the implementation file. However, the conditional inclusion of 'config.h' should probably be in 'magicsort.h' itself.
Update 2011-05-01
The URL linked above is no longer functional (404). You can find the C++ standard (582-2003-004) at EverySpec.com; the C standard (582-2000-005) seems to be missing in action.
The guidelines from the C standard were:
GSFC Standard available via Internet Archive 2012-12-10
Information courtesy Eric S. Bullington:
The referenced NASA C coding standard can be accessed and downloaded via the Internet archive:
http://web.archive.org/web/20090412090730/http://software.gsfc.nasa.gov/assetsbytype.cfm?TypeAsset=Standard
Sequencing
The question also asks:
The answer shows the correct mechanism — the nested includes, etc, should be after the
#define
(and the#define
should be the second non-comment line in the header) — but it doesn't explain why that's correct.Consider what happens if you place the
#include
between the#ifndef
and#define
. Suppose the other header itself includes various headers, perhaps even#include "magicsort.h"
indirectly. If the second inclusion ofmagicsort.h
occurs before#define MAGICSORT_H_INCLUDED
, then the header will be included a second time before the types it defines are defined. So, in C89 and C99, anytypedef
type name will be erroneously redefined (C2011 allows them to be redefined to the same type), and you will get the overhead of processing the file multiple times, defeating the purpose of the header guard in the first place. This is also why the#define
is the second line and is not written just before the#endif
. The formula given is reliable:一个好的做法是仅在包含文件需要 #includes 时才将其放入包含文件中。如果给定包含文件中的定义仅在 .c 文件中使用,则仅将其包含在 .c 文件中。
在您的情况下,我会将其包含在 #ifdef/#endif 之间的包含文件中。
这将最大限度地减少依赖性,以便在包含文件更改时不需要重新编译不需要给定包含的文件。
A good practice is to only put #includes in an include file if the include file needs them. If the definitions in a given include file are only used in the .c file then include it only in the .c file.
In your case, i would include it in the include file between the #ifdef/#endif.
This will minimize dependencies so that files that don't need a given include won't have to be recompiled if the include file changes.
在编译期间,预处理器仅用指定的文件内容替换#include指令。
为了防止无限循环,应该使用“
如果某个标头包含在文件中包含的另一个标头中”
不需要再次显式包含它,因为它将递归地包含到文件中
During compilation preprocessor just replaces #include directive by specified file content.
To prevent endless loop it should use
If some header was included into another header which was included to your file
than it is not necessary to explicitly include it again, because it will be included into the file recursively
通常,库开发人员会使用#ifndef /#define / #endif“技巧”来保护他们的包含免受多重包含的影响,因此您不必这样做。
当然,您应该检查...但无论如何编译器会在某个时候告诉您;-) 无论如何,检查多个包含项是一个很好的做法,因为它会减慢编译周期。
Usually, library developers protect their includes from multiple including with the #ifndef /#define / #endif "trick" so you don't have to do it.
Of course, you should check... but anyways the compiler will tell you at some point ;-) It is anyhow a good practice to check for multiple inclusions since it slows down the compilation cycle.
是的,这是必要的,否则编译器在尝试编译它不“知道”的代码时会抱怨。 #include 是对编译器的提示/推动/肘,告诉它选择声明、结构等以便成功编译。 jldupont 指出的 #ifdef/#endif 标头技巧是为了加快代码编译速度。
它用于具有 C++ 编译器并编译纯 C 代码的实例,如下所示下面是这个技巧的一个例子:
现在,如果多次包含它,编译器只会包含它一次,因为符号 __MY_HEADER_H__ 被定义一次,这会加快编译时间。
请注意上面示例中的符号 cplusplus,如果您有 C 代码,那么这是处理 C++ 编译的正常标准方法。
我已经包含了上面的内容来展示这一点(尽管与海报的原始问题)。
希望这有帮助,
此致,
汤姆.
PS:很抱歉让任何人对此投反对票,因为我认为这对于 C/C++ 新手来说是有用的。欢迎留下评论/批评等。
Yes it is necessary or the compiler will complain when it tries to compile code that it is not "aware" of. Think of #include's are a hint/nudge/elbow to the compiler to tell it to pick up the declarations, structures etc in order for a successful compile. The #ifdef/#endif header trick as pointed out by jldupont, is to speed up compilation of code.
It is used in instances where you have a C++ compiler and compiling plain C code as shown hereHere is an example of the trick:
Now, if this was included multiple times, the compiler will only include it once since the symbol
__MY_HEADER_H__
is defined once, which speeds up compilation times.Notice the symbol cplusplus in the above example, that is the normal standard way of coping with C++ compiling if you have a C code lying around.
I have included the above to show this (despite not really relevant to the poster's original question).
Hope this helps,
Best regards,
Tom.
PS: Sorry for letting anyone downvote this as I thought it would be useful tidbit for newcomers to C/C++. Leave a comment/criticisms etc as they are most welcome.
您需要包含标头中的标头,并且无需将其包含在 .c 中。包含应该在 #define 之后,这样就不会不必要地多次包含它们。例如:
和
You need to include the header from your header, and there's no need to include it in the .c. Includes should go after the #define so they are not unnecessarily included multiple times. For example:
and
我使用以下构造来确保所需的包含文件在此包含之前包含。我仅将所有头文件包含在源文件中。
I use the following construct to be sure, that the needed include file is included before this include. I include all header files in source files only.
我通常做的是创建一个包含文件,其中以正确的顺序包含所有必要的依赖项。所以我可能有:
全部在project.h中。现在,project.h 可以包含在任何地方,没有顺序要求,但我仍然可以将依赖项、类型和 API 函数原型放在不同的标头中。
What I normally do is make a single include file that includes all necessary dependencies in the right order. So I might have:
All in project.h. Now project.h can be included anywhere with no order requirements but I still have the luxury of having my dependencies, types, and API function prototypes in different headers.
只需将所有外部头文件包含在项目中的一个通用头文件中,例如global.h,并将其包含在所有 c 文件中:
它可能如下所示:
此文件使用include Guard 以避免多重包含、非法多重定义等。
Just include all external headers in one common header file in your project, e.g. global.h and include it in all your c files:
It can look like this:
This file uses include guard to avoid multiple inclusions, illegal multiple definitions, etc.