0

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...?

Community
  • 1
  • 1
commentator8
  • 629
  • 1
  • 7
  • 11
  • 2
    http://en.wikipedia.org/wiki/Include_guard Is all you need ;) – Chris Mar 31 '11 at 11:31
  • I like the idea (thanks all), it makes perfect sense, but when i implemented it i get `D:\workspace\swap\Debug/../pre boolean remote/swap.c:22: multiple definition of 'main' swap.o:D:\workspace\swap\Debug/../swap.c:22: first defined here` for many functions (prob all). Trying to find any obvious cause but no luck so far – commentator8 Mar 31 '11 at 11:47

3 Answers3

3

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.

Wookai
  • 20,883
  • 16
  • 73
  • 86
  • thanks @Wookai! re my comment above, i have included such guards (now) in every header file and am getting that error for each function - twice each. On a potentially tangential note in the makefile should i have header.h: queue.h as a dependency (with all the x.o: y.h 's)? – commentator8 Mar 31 '11 at 12:03
  • And in answer to my own comments, it was eclipse playing havoc with itself. No clue why. But a fresh project with the same files works fine. Thanks! – commentator8 Mar 31 '11 at 12:44
0

use this to define the both list and queue header files

#ifndef HEADERNAME_H_
#define HEADERNAME_H_
// your code for header file    
#endif 
Badr
  • 10,384
  • 15
  • 70
  • 104
0

Include Guards will be helpful in this case.

Rumple Stiltskin
  • 9,597
  • 1
  • 20
  • 25