1

My Files are

main.c

#include"A.h"
#include"B.h"

A.c

#include"A.h"

B.c

#include"B.h"

I have a file with a couple of structures that I have defined that I am supposed to use in all the files i.e A.c , B.c, main.c and even the header files for A and B.

Hence I have

A.h and B.h both have

#include"struct.h"

Now, I see that in my main.c

I will have multiple declaration for both the structures, how do I get rid of this problem. What shall I change in my structure?

Thanks

Kraken
  • 23,393
  • 37
  • 102
  • 162
  • You do have guard-fences in your header files to prevent the very multiple declarations your worried about, right? – WhozCraig Mar 28 '13 at 09:55
  • Read about [include guards](http://en.wikipedia.org/wiki/Include_guard). – Some programmer dude Mar 28 '13 at 09:56
  • @WhozCraig I am not sure what that is? – Kraken Mar 28 '13 at 09:56
  • 1
    @Kraken you have a load of examples below that will show you what they are. Or look [here](http://stackoverflow.com/questions/1653958/why-are-ifndef-and-define-used-in-c-header-files), or any of about three dozen similar posts. – WhozCraig Mar 28 '13 at 09:58

4 Answers4

7

Use include guards.

aheader.h:

#ifndef AHEADER_H
#define AHEADER_H

// ... rest of header here

#endif

bheader.h:

#ifndef BHEADER_H
#define BHEADER_H

// ... rest of header here

#endif
svk
  • 5,854
  • 17
  • 22
  • And of course doing the same in the `structs.h` header file (or any other header file). – Some programmer dude Mar 28 '13 at 09:56
  • 2
    @JoachimPileborg Shall I be doing this for each file ( I guess I shall to make sure that none of the declaration duplicates), but if I take my case, then I guess only struct.h should have these guards, right? Since that is the only declaration susceptible to duplicate? – Kraken Mar 28 '13 at 10:00
  • @svk `aheader.h` has `struct.h` and `bheader.h` also has `struct.h`. So both `aheader.h` and `bheader.h` will be included and hence `struct.h` will also get included twice?? – Suvarna Pattayil Mar 28 '13 at 10:02
  • 1
    @Kraken You should make it a habit of doing it for all header files. – Some programmer dude Mar 28 '13 at 10:02
  • duly noted. Thanks people. Been confused over it for quite sometime now. Cheers. – Kraken Mar 28 '13 at 10:04
  • @Kraken I think in this case it is enough to do it only for struct.h, but for the long run just do it always like Jo means – Leo Chapiro Mar 28 '13 at 10:04
1

You can use a guard as such,

#ifndef MY_STRUCT
#define MY_STRUCT
#include "struct.h"
#endif

If you want to selectively take care of which parts should not be duplicated

Suvarna Pattayil
  • 5,136
  • 5
  • 32
  • 59
1

Wrap the header files in include guards., like this:

#ifndef MYHEADER_H
#define MYHEADER_H

    // your definitions

#endif

Each header file should have its own guard with an unique name. The above preprocessor directives translated to english say something like: "If MYHEADER_H is not defined, then define it and paste the contents until #endif directive." This guarantees that a single header is included only once inside a single translation unit.

jrok
  • 54,456
  • 9
  • 109
  • 141
1

Simply use so called header guard to be sure of including "struct.h" only once:

// struct.h
#ifndef STRUCT_H
#define STRUCT_H

struct ...{

}

#endif
Leo Chapiro
  • 13,678
  • 8
  • 61
  • 92