在另一个头文件中包含标头

时间:2021-10-29 13:13:14

I've defined a struct item in a .h file. Now I'm defining another struct tPCB in another .h which is part of the same project, and I need the tPCB to 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".

我在.h文件中定义了一个struct项。现在我在另一个.h中定义另​​一个struct tPCB,它是同一个项目的一部分,我需要tPCB才能有一个项目。我认为只是制作相同的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 #include line to make it work?

除了添加#include行以使其工作之外,还有其他方法吗?

5 个解决方案

#1


8  

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 #include every file that defines your dependencies, and rely on the include guards in each .h to keep things from being multiply defined.

如果您的.c#包含正确顺序的两个.h文件,它将起作用。这可能就是你记得的情况。最安全的方法是#include定义依赖关系的每个文件,并依赖每个.h中的包含保护来防止事物被多重定义。

#2


1  

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 extern in the h-file and allocate it in the corresponding C file.

如果需要全局公开变量(实际上不需要这么做的情况),那么在h文件中将其声明为extern并将其分配到相应的C文件中。

#3


0  

You need to use #include. In C, everything is explicit; don't expect it to work by magic.

你需要使用#include。在C中,一切都是明确的;不要指望它能通过魔法发挥作用。

#4


0  

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结构的定义。在tPCB.h的顶部,您应该输入以下语句:

#include "item.h"

This should give the tPCB.h file access to all the definitions in item.h.

这应该让tPCB.h文件访问item.h中的所有定义。

#5


0  

In your "another .h", #include <a .h file>.

在“另一个.h”中,#include

Elaboration:

In the file that defines struct tPCB, you need to #include the file that defines struct item.

在定义struct tPCB的文件中,您需要#include定义struct item的文件。

#1


8  

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 #include every file that defines your dependencies, and rely on the include guards in each .h to keep things from being multiply defined.

如果您的.c#包含正确顺序的两个.h文件,它将起作用。这可能就是你记得的情况。最安全的方法是#include定义依赖关系的每个文件,并依赖每个.h中的包含保护来防止事物被多重定义。

#2


1  

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 extern in the h-file and allocate it in the corresponding C file.

如果需要全局公开变量(实际上不需要这么做的情况),那么在h文件中将其声明为extern并将其分配到相应的C文件中。

#3


0  

You need to use #include. In C, everything is explicit; don't expect it to work by magic.

你需要使用#include。在C中,一切都是明确的;不要指望它能通过魔法发挥作用。

#4


0  

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结构的定义。在tPCB.h的顶部,您应该输入以下语句:

#include "item.h"

This should give the tPCB.h file access to all the definitions in item.h.

这应该让tPCB.h文件访问item.h中的所有定义。

#5


0  

In your "another .h", #include <a .h file>.

在“另一个.h”中,#include

Elaboration:

In the file that defines struct tPCB, you need to #include the file that defines struct item.

在定义struct tPCB的文件中,您需要#include定义struct item的文件。