C语言 C 未知类型名称“my_structure”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18903300/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 07:29:10  来源:igfitidea点击:

C Unknown type name 'my_structure'

cgccstruct

提问by alexandernst

I have this code:

我有这个代码:

main.h

main.h

#ifndef MAINH
#define MAINH
...
#include "my_struct.h"
void some_func(my_structure *x);
...
#endif

and

my_struct.h

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?

知道为什么吗?

回答by John Bode

Because of how you've ordered your includes, the compiler sees void some_func(my_structure *x);before it sees typedef struct abcd { int a; } my_structure;.

由于您对包含的排序方式,编译器会void some_func(my_structure *x);先看到typedef struct abcd { int a; } my_structure;.

I think.

我认为。

Let's walk this through.

让我们来看看这个。

Assuming my_struct.his processed first, we get the following sequence of events:

假设my_struct.h先处理,我们得到以下事件序列:

  1. UTILSHis defined
  2. MAINHis defined
  3. Because UTILSHis already defined, we don't process my_struct.hagain, so the typedef isn't processed
  4. void some_func(my_structure *x);is processed.
  5. Nowthe typedef is processed.
  1. UTILSH被定义为
  2. MAINH被定义为
  3. 因为UTILSH已经定义了,我们不再处理my_struct.h,所以不处理typedef
  4. void some_func(my_structure *x);被处理。
  5. 现在处理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_structurein main.h, or you need to break that circular dependency (which is the much preferred option). Is there anything in main.hthat my_structure.hactually uses? If so, you will want to factor it out into a separate file that both main.hand my_structure.hinclude.

坏枣。您要么需要my_structurein的前向声明main.h,要么需要打破这种循环依赖(这是更受欢迎的选项)。是否有什么main.hmy_structure.h实际使用?如果是这样,您需要将其分解为一个单独的文件,同时包含main.hmy_structure.h

回答by AnT

You created a circular header inclusion. Circular inclusion never achieves anything. It is infinite. The #ifndefinclude guard will break the infinite inclusion circle at some unpredictable point (depending on which header is included into .cfile first). This is what happened in your case. Basically, your circular inclusion got "resolved" into including main.hfirst and my_struct.hsecond. This is why main.hknows nothing about my_structtype.

您创建了一个圆形标题包含。循环包容永远不会取得任何成就。它是无限的。在#ifndef包括防护件将打破在一些不可预测点的无限列入圆(取决于报头被包括在.c文件中的第一)。这就是你的情况。基本上,您的循环包含被“解决”为包含main.h第一个和my_struct.h第二个。这就是为什么main.hmy_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.his probably a lower-level header, which means that you have to stop including main.hinto my_struct.h. Redesign your headers so that my_struct.hno longer needs main.h.

同样,循环包含永远不会实现任何目标。摆脱循环包含。分层设计您的标题结构:将较低级别的标题包含在更高级别的标题中,但绝不会反过来。在您的情况下my_struct.h可能是一个较低级别的标题,这意味着您必须停止包含main.hinto my_struct.h。重新设计您的标题,以便my_struct.h不再需要 main.h.

回答by yan

The error message is coming from main.hwhile it's included in my_struct.h, before my_structureis defined. You should rethink your include paths since main.hand my_struct.hinclude each other.

错误消息来自于main.h它包含在my_struct.h, beforemy_structure被定义。您应该重新考虑您的包含路径,main.hmy_struct.h相互包含。

You probably want your main.hfile to just include my_struct.h, and not have my_struct.hto include anything. You're essentially instructing your C compiler to have an infinite co-include loop.

您可能希望您的main.h文件只包含my_struct.h,而不必my_struct.h包含任何内容。您实际上是在指示您的 C 编译器具有无限的 co-include 循环。