i have a problem in C which i don't know how to solve. Suppuse i have 4 c files, a.c ; b.c ; c.c ; d.c, and for each one of them there is an .h file: a.h ; b.h ; c.h ; d.h, which of course they include. I want to do the following:
a.h will contain:
#include "b.h"
#include "c.h"
...
b.h will contain:
#include "d.h"
...
and c.h will also contain:
#include "d.h"
...
In d.h I define a struct, for example, the content of d.h will be:
typedef struct address {
int address;
} address;
The problem is that i get the error (when i compile the files):
In file included from c.h:1:0,
from a.h:2,
from a.c:1:
d.h:3:16: error: redefinition of ‘struct address’
d.h:3:16: note: originally defined here
d.h:5:3: error: conflicting types for ‘address’
d.h:5:3: note: previous declaration of ‘address’ was here
I understand why is it happening (because the preproccesor import the definition twice), but how do i solve it? I need the includes to be that's way (because of course i have more files, this was just an example). What can i do?
note: my makefile was:
project: a.o b.o c.o d.o
gcc -g a.o b.o c.o d.o -ansi -Wall -pedantic -o output
a.o: a.c a.h b.c b.h c.c c.h d.c d.h
gcc -c a.c -ansi -Wall -pedantic -o a.o
b.o: b.c b.h d.c d.h
gcc -c b.c -ansi -Wall -pedantic -o b.o
c.o: c.c c.h d.c d.h
gcc -c c.c -ansi -Wall -pedantic -o c.o
d.o: d.c d.h
gcc -c d.c -ansi -Wall -pedantic -o d.o
and i think it's okay.. It workd for me. Thank you for the help.