【发布时间】:2012-08-19 12:46:00
【问题描述】:
我需要定义一个结构,比如说
//globalstruct.h
typedef struct _GlobalStruct {
int a, b;
} GlobalStruct
然后在我想要的任何地方使用GlobalStruct,只需包含globalstruct.h
例如:
//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文件之外,它看起来还可以工作...... -
呃...正如您在问题中所说的那样? (只记得包含守卫,
typedef末尾的分号和#include中的引号) -
Kerrek 谈到了如何保留结构名称。以下是更多案例的备份链接:stackoverflow.com/questions/228783/…
-
你的一个标题中没有
#endif。 -
这只是一个错字,抱歉。我的文件中确实有所有 #endif