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

问题描述 投票:0回答:1

我有3个文件:main.cdef.cdef.h。两个.c文件都包括def.h。所有文件都在同一目录中。我的编译器是gcc版本4.9.2。

在def.h中:

struct _info {
    int a;
};

在def.c中:

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

在main.c:

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

当我将def.c构建为目标文件,然后使用main.c构建时:

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

我收到一条错误消息:数组类型具有不完整的元素类型


如果我使用typedefstruct _info定义为INFO,如:

typedef struct _info INFO;

并在struct _info文件中用INFO替换.c。然后编译确定。

但是为什么和typedef做什么?

c struct typedef extern
1个回答
1
投票

谢谢大家的帮助。这个问题最终导致main.c拼写错误。就像是:

extern struct _infoo info[];

typedef取代它们时,一切都工作正常。

© www.soinside.com 2019 - 2024. All rights reserved.