我需要在源文件中定义一个结构,其他源也应该能够看到它。 我在一个源文件中声明结构,然后在 header 中声明 extern 声明以包含在其他源中,但编译器出现以下错误:
lcd.c(24): error: #147: declaration is incompatible with "struct graph_obj arrow_right" (declared at line 45 of "lcd.h")
.C文件
struct graph_obj
{
const u16 *id;
int x,y;
u16 w, h;
};
u32 lcdid;
struct graph_obj btn0;
struct graph_obj btn1;
struct graph_obj btn2;
struct graph_obj btn3;
然后是.h文件
extern struct graph_obj
{
const u16 *id;
int x,y;
u16 w, h;
};
extern u32 lcdid;
extern struct graph_obj btn0;
extern struct graph_obj btn1;
extern struct graph_obj btn2;
extern struct graph_obj btn3;
那么,我该怎么办呢?
最佳答案
in header:
typedef struct
{
const u16 *id;
int x,y;
u16 w, h;
}GRAPH_OBJ;
extern GRAPH_OBJ graph_obj, *pGraph_obj;
在 c 模块中:
GRAPH_OBJ graph_obj, *pGraph_obj;
与您的代码类似,除了在这里,您创建了一个新类型 (GRAPH_OBJ),它将表现为任何外部类型。而且,您不必完全重新创建结构定义,它都包含在 GRAPH_OBJ 中。顺便说一下, Here 是一篇关于如何正确使用外部表达式的好文章。
关于c - 在 Header 中声明 Struct 并在多个 Source 中使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19323507/