struct和typedef问题 [英] struct and typedef problems

查看:88
本文介绍了struct和typedef问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

i我在linux下用c编写程序并用make

和gcc编译我的代码。


现在我有4个文件:init.c / h和packets.c / h。每个头文件包含

一些:


init.h:

struct xyz

{

int a;

};

typedef struct xyz XYZ;


void fkt_1(XYZ *,ABC *);


packets.h:

struct abc

{

int b;

}

typedef struct abc ABC;


void fkt_2(XYZ *,ABC *);


因为一个文件正在另一个文件之前进行编译,所以这个文件不知道另一个文件的结构,并且我之前得到了一个解析器错误,让我们回答

说,ABC。


我在这个新闻组和互联网上做了一些研究,尝试使用

" extern struct abc"或类似的东西,但没有任何效果。


有谁能告诉我有什么问题以及如何解决它?


非常感谢


bj?

Hello,
i am writing a program under linux in c and compile my code with make
and gcc.

Now i have 4 files: init.c/h and packets.c/h. Each header-file contains
some:

init.h:
struct xyz
{
int a;
};
typedef struct xyz XYZ;

void fkt_1(XYZ *, ABC *);

packets.h:
struct abc
{
int b;
}
typedef struct abc ABC;

void fkt_2(XYZ *, ABC *);

Because one file is compiling before the other this one does not know
the structure from the other and i get an parser error before, let''s
say, ABC.

I did some research in this newsgroup and in the internet, tried to use
"extern struct abc" or something like that but nothing worked.

Can anybody tell me what is wrong and how to fix it?

Many thanks

bj?rn

推荐答案

burn写道:

您好,我正在用Linux编写一个程序,用make
和gcc编译我的代码。

现在我有4个文件:init.c / h和packets.c /小时。每个头文件包含
一些:

init.h:
struct xyz
{
int a;
};
typedef struct xyz XYZ;

void fkt_1(XYZ *,ABC *);

packets.h:
struct abc
{
int b;
}
typedef struct abc ABC;

void fkt_2(XYZ *,ABC *);

因为一个文件正在编译之前另一个这个人不知道另一个结构,我之前得到一个解析器错误,让我们说,ABC。

我做了一些研究新闻组和在互联网上,试图使用
extern struct abc或类似的东西,但没有任何效果。

任何人都可以告诉我有什么问题以及如何解决它?

Hello,
i am writing a program under linux in c and compile my code with make
and gcc.

Now i have 4 files: init.c/h and packets.c/h. Each header-file contains
some:

init.h:
struct xyz
{
int a;
};
typedef struct xyz XYZ;

void fkt_1(XYZ *, ABC *);

packets.h:
struct abc
{
int b;
}
typedef struct abc ABC;

void fkt_2(XYZ *, ABC *);

Because one file is compiling before the other this one does not know
the structure from the other and i get an parser error before, let''s
say, ABC.

I did some research in this newsgroup and in the internet, tried to use
"extern struct abc" or something like that but nothing worked.

Can anybody tell me what is wrong and how to fix it?




这将有效,

如果还没有为其他东西定义宏:


/ * BEGIN init.h * /


#ifndef H_INIT

#define H_INIT


#include" packets.h"


struct xyz {

int a;

};


typedef struct xyz XYZ;


void fkt_1(XYZ *,ABC *);


#endif


/ * END init.h * /

/ * BEGIN packets.h * /


#ifndef H_PACKETS

#define H_PACKETS


#include" init.h"

struct abc {

int b;

};


typedef struct abc ABC;


void fkt_2(XYZ *,ABC *);


void fkt_1(XYZ * ,ABC *);


#endif


/ * END packets.h * /


这两个文件很可能会更好用

合并为一个。

-

pete



This will work,
if the macros haven''t already been defined for something else:

/* BEGIN init.h */

#ifndef H_INIT
#define H_INIT

#include "packets.h"

struct xyz {
int a;
};

typedef struct xyz XYZ;

void fkt_1(XYZ *, ABC *);

#endif

/* END init.h */
/* BEGIN packets.h */

#ifndef H_PACKETS
#define H_PACKETS

#include "init.h"

struct abc {
int b;
};

typedef struct abc ABC;

void fkt_2(XYZ *, ABC *);

void fkt_1(XYZ *, ABC *);

#endif

/* END packets.h */

There''s a good chance that these two files might be better off
combined as one.
--
pete


不幸的是 - 整个事情都是计划不良的指标。


如果两个模块共享一个共同的界面 - 那么它最好

让他们共享一个公共头文件到该界面,并在该界面中使用



Unfortunately - the entire thing is an indicator of bad planning.

If the two modules are sharing a common interface - then it''s best to
have them share a common header file to that interface and types used
within that interface.


感谢您的快速回复。你们两个都是对的,我应该可以

重组我的代码。


问题是我有多个文件在
$上做多件事b $ b相同类型的结构 - 但我认为应该有点思考

得到那个;-)

thank you for the prompt reply. you both are right, i should probably
restructure my code.

the problem is that i have multiple files doing multiple things on the
same kind of structures - but i think a little bit of thinking should
get that straight ;-)


这篇关于struct和typedef问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆