我需要定义一个结构,比方说
//globalstruct.h
typedef struct _GlobalStruct {
int a, b;
} GlobalStruct
然后只需包含 globalstruct.h
GlobalStruct
例如:
//test.c
#include globalstruct.h
void test(GlobalStruct *gs){...}
我该怎么做?
问候
----编辑----
我想我需要再澄清一点我的问题,因为我完全被困住了。
//main.c
#include "gstruct.h"
#include "a.h"
#include "b.h"
...
void something(GlobalStruct *gs){...}
.
//gstruct.h
#ifdef gstruct_h
#define gstruct_h
typedef struct _GlobalStruct{
int a, b;
} GlobalStruct;
#endif
.
//a.h
#ifdef a_h
#define a_h
#include "gstruct.h"
GlobalStruct a_something(...);
#endif
.
//a.c
#include "gstruct.h"
#include "a.h"
GlobalStruct a_something(...){...}
.
//b.h
#ifdef b_h
#define b_h
#include "gstruct.h"
GlobalStruct b_something(...);
#endif
.
//b.c
#include "gstruct.h"
#include "b.h"
GlobalStruct b_something(...){...}
.
这样可以吗?因为如果是的话,我就错过了一些非常愚蠢/小/愚蠢的东西。
顺便说一句,我正在使用 gcc main.c a.c b.c -o the_thing
---第二次编辑----
我刚刚创建了一个在线示例,您可以查看、下载并试用。 它已准备好编译,但会在编译时失败。
最佳答案
你快到了。需要修复三个错误:
为包含指令添加引号:
#include "globalstruct.h"
在 typedef 声明的末尾需要一个分号。
您不能使用下划线大写的名称,因为它们是保留的。而是使用类似的东西:
typedef struct GlobalStruct_struct { /* ... */ } GlobalStruct;
(感谢@chris 发现了第二名!)
关于c - 多个文件的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12101167/