简体   繁体   English

extern struct array error:数组类型的元素类型不完整

[英]extern struct array error: array type has incomplete element type

I have 3 files: main.c , def.c , def.h . 我有3个文件: main.cdef.cdef.h Both .c files include def.h . 这两个.c文件都包含def.h All the files are in the same directory. 所有文件都在同一目录中。 My compiler is gcc version 4.9.2 . 我的编译器是gcc版本4.9.2

In def.h : def.h中

struct _info {
    int a;
};

In def.c : def.c中

#include "def.h"
struct _info info[] = {};

And in main.c : main.c中

#include "def.h"
extern struct _info info[];

When I build def.c as an object file and then build with main.c like: 当我将def.c构建为目标文件,然后使用main.c构建时,如下所示:

gcc -c def.c
gcc main.c def.o

And I got an error message: array type has incomplete element type 我收到一条错误消息: 数组类型具有不完整的元素类型


If I use typedef to define struct _info as INFO like: 如果我使用typedefstruct _info定义为INFO例如:

typedef struct _info INFO;

And replace struct _info with INFO in .c files. 并用.c文件中的INFO替换struct _info Compile ok then. 然后编译确定。

But why and what does typedef do? 但是, 为什么什么 typedef呢?

Thanks for everybody's help. 感谢大家的帮助。 This question end up with a misspelling in main.c . 这个问题以main.c的拼写错误结束。 Something like: 就像是:

extern struct _infoo info[];

When typedef replace them, all work fine definitely. typedef替换它们时,一切肯定可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM