C语言 在另一个头文件中包含一个头文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5343916/
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
Include a header in another header file
提问by bluehallu
I've defined a struct itemin a .h file. Now I'm defining another struct tPCBin another .h which is part of the same project, and I need the tPCBto have an item. I thought that just making part of the same TurboC project would allow me to use item in the other header file, but the compiler throws me "undefined type: ite".
我struct item在 .h 文件中定义了a。现在我struct tPCB在另一个 .h 中定义另一个,它是同一个项目的一部分,我需要tPCB有一个.h item。我以为只是制作同一个 TurboC 项目的一部分就可以让我在另一个头文件中使用 item,但是编译器抛出了我“ undefined type: ite”。
I guess I somehow have to include the first header on the second one, However I have seen the same a similar code which works without doing so.
我想我必须以某种方式在第二个标题中包含第一个标题,但是我看到了相同的类似代码,但没有这样做。
Is there any other way than just adding an #includeline to make it work?
除了添加#include一行使其工作之外,还有其他方法吗?
采纳答案by Mark Ransom
If your .c #includes the two .h files in the proper order, it will work. That's probably what happened in the case you remember. The safest course is to #includeevery file that defines your dependencies, and rely on the include guards in each .h to keep things from being multiply defined.
如果您的 .c#include以正确的顺序包含两个 .h 文件,它将起作用。这可能是你记得的案例中发生的事情。最安全的方法是对#include定义您的依赖项的每个文件,并依靠每个 .h 中的包含守卫来防止事物被多重定义。
回答by Toast
Sorry, there is no way in C that you can access a definition of a structure, in another header file without including that file (through an #include). Instructions for the #include follow.
抱歉,在 C 中,您无法在不包含该文件的情况下访问另一个头文件中的结构定义(通过 #include)。#include 的说明如下。
So, lets say that the header file that contains the definition of the item structure is called "item.h", and the header file that contains the definition of the tPCB structure in "tPCB.h". At the top of tPCB.h, you should put the following statement:
因此,假设包含项目结构定义的头文件称为“item.h”,而包含“tPCB.h”中的 tPCB 结构定义的头文件称为“item.h”。在 tPCB.h 的顶部,您应该放置以下语句:
#include "item.h"
This should give the tPCB.h file access to all the definitions in item.h.
这应该允许 tPCB.h 文件访问 item.h 中的所有定义。
回答by Lundin
Never ever put variable definitions (that is, allocating them) in a header file. That is bad for many different reasons, the two major ones being poor program design and floods of linker errors.
永远不要将变量定义(即分配它们)放在头文件中。由于许多不同的原因,这很糟糕,两个主要的原因是程序设计不佳和链接器错误泛滥。
If you need to expose a variable globally (there are not many cases where you actually need to do that), then declare it as externin the h-file and allocate it in the corresponding C file.
如果您需要全局公开变量(实际需要这样做的情况并不多),则将其声明为extern在 h 文件中并在相应的 C 文件中分配。
回答by nmichaels
In your "another .h", #include <a .h file>.
在您的“另一个 .h”中,#include <a .h file>.
Elaboration:
细化:
In the file that defines struct tPCB, you need to #includethe file that defines struct item.
在定义的文件中struct tPCB,您需要#include定义struct item.
回答by metamatt
You need to use #include. In C, everything is explicit; don't expect it to work by magic.
您需要使用#include。在 C 中,一切都是明确的;不要指望它会神奇地起作用。

