[英]C Unknown type name 'my_structure'
I have this code:我有这个代码:
main.h
#ifndef MAINH
#define MAINH
...
#include "my_struct.h"
void some_func(my_structure *x);
...
#endif
and和
my_struct.h
#ifndef UTILSH
#define UTILSH
...
#include "main.h"
...
typedef struct abcd {
int a;
} my_structure;
...
#endif
but I'm getting this when I try to compile: error: unknown type name 'my_structure'
但是当我尝试编译时得到这个:
error: unknown type name 'my_structure'
Any idea why?知道为什么吗?
Because of how you've ordered your includes, the compiler sees void some_func(my_structure *x);
由于您订购包含的方式,编译器会看到
void some_func(my_structure *x);
before it sees typedef struct abcd { int a; } my_structure;
在它看到
typedef struct abcd { int a; } my_structure;
typedef struct abcd { int a; } my_structure;
. .
Let's walk this through.让我们来看看这个。
Assuming my_struct.h
is processed first, we get the following sequence of events:假设首先处理
my_struct.h
,我们得到以下事件序列:
UTILSH
is defined UTILSH
已定义MAINH
is defined MAINH
已定义UTILSH
is already defined, we don't process my_struct.h
again, so the typedef isn't processedUTILSH
已经定义了,我们不再处理my_struct.h
,所以 typedef 没有被处理void some_func(my_structure *x);
is processed.typedef
is processed.typedef
。 So after preprocessing, your compiler sees the following sequence of declarations:因此,在预处理之后,您的编译器会看到以下声明序列:
...
void some_func(my_structure *x);
...
typedef struct abcd {...} my_structure;
Bad juju.坏枣。 You either need a forward declaration of
my_structure
in main.h
, or you need to break that circular dependency (which is the much preferred option).您要么需要在
main.h
对my_structure
进行前向声明,要么需要打破这种循环依赖(这是更受欢迎的选项)。 Is there anything in main.h
that my_structure.h
actually uses? main.h
中是否有my_structure.h
实际使用的内容? If so, you will want to factor it out into a separate file that both main.h
and my_structure.h
include.如果是这样,您需要将其分解为
main.h
和my_structure.h
包含的单独文件。
You created a circular header inclusion.您创建了一个圆形标题包含。 Circular inclusion never achieves anything.
循环包容永远不会取得任何成就。 It is infinite.
它是无限的。 The
#ifndef
include guard will break the infinite inclusion circle at some unpredictable point (depending on which header is included into .c
file first). #ifndef
包含守卫将在某个不可预测的点打破无限包含圈(取决于哪个头文件首先包含在.c
文件中)。 This is what happened in your case.这就是你的情况。 Basically, your circular inclusion got "resolved" into including
main.h
first and my_struct.h
second.基本上,您的循环包含被“解析”为首先包含
main.h
然后包含my_struct.h
。 This is why main.h
knows nothing about my_struct
type.这就是
main.h
对my_struct
类型一无所知的原因。
Again, circular inclusion never achieves anything.同样,循环包含永远不会实现任何目标。 Get rid of circular inclusion.
摆脱循环包含。 Design your header structure hierarchically: lower-level headers included into higher-level headers, but never the other way around.
分层设计您的标题结构:将较低级别的标题包含在更高级别的标题中,但绝不会反过来。 In your case
my_struct.h
is probably a lower-level header, which means that you have to stop including main.h
into my_struct.h
.在您的情况下
my_struct.h
可能是一个较低级别的标头,这意味着您必须停止将main.h
包含到my_struct.h
。 Redesign your headers so that my_struct.h
no longer needs main.h
.重新设计您的标题,以便
my_struct.h
不再需要main.h
。
The error message is coming from main.h
while it's included in my_struct.h
, before my_structure
is defined.该错误消息是来自
main.h
而它包含在my_struct.h
,前my_structure
定义。 You should rethink your include paths since main.h
and my_struct.h
include each other.您应该重新考虑包含路径,因为
main.h
和my_struct.h
相互包含。
You probably want your main.h
file to just include my_struct.h
, and not have my_struct.h
to include anything.您可能希望
main.h
文件只包含my_struct.h
,而不要让my_struct.h
包含任何内容。 You're essentially instructing your C compiler to have an infinite co-include loop.您实际上是在指示您的 C 编译器具有无限的 co-include 循环。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.