开发者

header file using another header - c [duplicate]

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

How to declare a structure in a header that is to be used by multiple files in c?

c code, header file issue.

I have a header (list.h) file defining two linked list structures, and another queue.h which includes the definition of a queue.

There is a struct that includes the lists and queue together, defined in list.h, which therefore depends on the queue.h file.

A struct containing all the others is defined in the list.h file and the functions that deal with it are defined in the list.c file. Consequently both files need to include queue.h.

However if i include it in both the list.h and list.c files i get the following error.

..\/queue.h:13:16: error: redefinition of 'struct qqq'
..\/queue.h:13:16: note: originally defined here

if not in one or the 开发者_如何学运维other then other errors to the effect that the header is missing: it doesn't define the structure containing the queue.

Is there any way to do this...?


You should use the #ifndef preprocessor statement to prevent the content of your headers to be included twice :

queue.h:

#ifndef QUEUE_H
#define QUEUE_H

// QUEUE_H can be anything, but must be a unique constant specifiqu to your file

typedef struct {
    // ...
} queue;

#endif

Simply to this for all your header files (with different constants each time), and it will work.


Include Guards will be helpful in this case.


use this to define the both list and queue header files

#ifndef HEADERNAME_H_
#define HEADERNAME_H_
// your code for header file    
#endif 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜