typedef struct, circular dependency, forward definitions
Posted
by
BlueChip
on Stack Overflow
See other posts from Stack Overflow
or by BlueChip
Published on 2013-06-17T06:27:03Z
Indexed on
2013/06/29
16:21 UTC
Read the original article
Hit count: 212
The problem I have is a circular dependency issue in C header files ...Having looked around I suspect the solution will have something to do with Forward Definitions, but although there are many similar problems listed, none seem to offer the information I require to resolve this one...
I have the following 5 source files:
// fwd1.h
#ifndef __FWD1_H
#define __FWD1_H
#include "fwd2.h"
typedef
struct Fwd1 {
Fwd2 *f;
}
Fwd1;
void fwd1 (Fwd1 *f1, Fwd2 *f2) ;
#endif // __FWD1_H
.
// fwd1.c
#include "fwd1.h"
#include "fwd2.h"
void fwd1 (Fwd1 *f1, Fwd2 *f2) { return; }
.
// fwd2.h
#ifndef __FWD2_H
#define __FWD2_H
#include "fwd1.h"
typedef
struct Fwd2 {
Fwd1 *f;
}
Fwd2;
void fwd2 (Fwd1 *f1, Fwd2 *f2) ;
#endif // __FWD2_H
.
// fwd2.c
#include "fwd1.h"
#include "fwd2.h"
void fwd2 (Fwd1 *f1, Fwd2 *f2) { return; }
.
// fwdMain.c
#include "fwd1.h"
#include "fwd2.h"
int main (int argc, char** argv, char** env)
{
Fwd1 *f1 = (Fwd1*)0;
Fwd2 *f2 = (Fwd2*)0;
fwd1(f1, f2);
fwd2(f1, f2);
return 0;
}
Which I am compiling with the command: gcc fwdMain.c fwd1.c fwd2.c -o fwd -Wall
I have tried several ideas to resolve the compile errors, but have only managed to replace the errors with other errors ...How do I resolve the circular dependency issue with the least changes to my code? ...Ideally, as a matter of coding style, I would like to avoid putting the word "struct" all over my code.
© Stack Overflow or respective owner