J
JCB
Hi,
I have two C file which are referencing in a circular way.
To simplify I use an example with car :
Suppose I have a program for cars.
I have car.c, structure car and functions prototypes of car.c are in a
file car.h
I have also engine.c, a structure engine and functions prototypes of
engin.c are in a file engine.h.
Now the structure car in car.c use an engine structure, so I have to
write in car.c :
#include car.h
#include engine.h
.... (code)
Note :in car.h
struct car {
struct engine engine ;
etc...
} ;
Up to there, it's standard and there is no problem.
The problem is if I have to write a function in engine.c which
reference a car, in this case, I will have to write in engine.c :
#include engine.h
#include car.h
The problem is that engine depends on car and car depends on engine,
so either I write
(case 1)
#include engine.h
#include car.h
or
(case 2)
#include car.h
#include engine.h
it's not appropriated :
case 1
Function of car.h use the structure engine.h, so it's good, but
engine.h use structure car in car.h, not yet defined (because #include
car.h is after #include engine.h)
case 2:
Functions of engine.h that use structure car it's good, because
defined before, but in car.h , I need a reference to struct engine,
not yet defined...
Some people kwow what to do in this particular case ?
I have two C file which are referencing in a circular way.
To simplify I use an example with car :
Suppose I have a program for cars.
I have car.c, structure car and functions prototypes of car.c are in a
file car.h
I have also engine.c, a structure engine and functions prototypes of
engin.c are in a file engine.h.
Now the structure car in car.c use an engine structure, so I have to
write in car.c :
#include car.h
#include engine.h
.... (code)
Note :in car.h
struct car {
struct engine engine ;
etc...
} ;
Up to there, it's standard and there is no problem.
The problem is if I have to write a function in engine.c which
reference a car, in this case, I will have to write in engine.c :
#include engine.h
#include car.h
The problem is that engine depends on car and car depends on engine,
so either I write
(case 1)
#include engine.h
#include car.h
or
(case 2)
#include car.h
#include engine.h
it's not appropriated :
case 1
Function of car.h use the structure engine.h, so it's good, but
engine.h use structure car in car.h, not yet defined (because #include
car.h is after #include engine.h)
case 2:
Functions of engine.h that use structure car it's good, because
defined before, but in car.h , I need a reference to struct engine,
not yet defined...
Some people kwow what to do in this particular case ?