struct - C Abstract data type pointer - Program modularity -


if module depends other sub-module, either including in header or in code file, , if modules, besides contributing main program, mutually dependent?

the figure below schematically illustrates situation in module1.h/.c requires sub-module module4.h, , module2 requires module3.

example

each header file has own typedef struct exemple:

typedef struct list * list; 

and source file related header files implement struct :

struct list {     unsigned length;     char * value; }; 

i got struct in header file:

typedef enum {start, end, middle, comments, conditions} typelistbal; typedef struct bal * bal; 

and source file :

struct bal {     typelistbal type;     list name;              // name of bal     list attributes[];          // array of type struct list }; 

i've been reading lot tonight , i'm not quite sure 1 thing. if i'm including header file in source file need in case, i'm including list.h in bal.c cause bal.c got struct definition member of type list.

to make work, have put specific #include in modules or job done @ compilation?

i mean in make makefile, have bal.o object, like:

bal.o: list.o bal.c bal.h        $(cc)   -g -w -wall -c  list.o bal.c bal.h 

so makefile try compile bal.o, compiler see dependencies, , able compile everything.

that's how did understand when try this, get:

error: dereferencing pointer incomplete type 

i'm guessing it's because in bal.c file, i'm declaring variable of type struct list , compiler doesn't have ideas struct list cause definition of struct list in list.c file.

so question again is: how connect make work?

your header file broken. incomplete type:

typedef struct bal * bal; 

it needs struct declaration make complete. ought in header file, too. if in c code *bal without complete type, compiler gives because doesn't have information expression means, i.e. fields of struct.

there should absolutely no problem dependency graph you're showing. want use guards ensure header files included once. can include 1 header another, , c preprocessor "just work" long there no cyclic dependencies. in case, header e.g. module 4 like:

// module4.h #ifndef module_4_h_included #define module_4_h_included  #include "module1.h"  // public type declarations module 4 types  #endif // module_4_h_included 

make headers (including module1.h) similar, own guard definitions. module 4 code:

// module4.c #include "module4.h"  // use module 4 types. 

in general, if need types given header file in .c file, include if included dependencies between headers. guards ensure no redundancy.

additionally, idea of make dependencies incorrect. e.g. compile module4.c obtain module4.o, need (of course) module4.c, module4.h, , module1.h. build dependencies. rule be:

module4.o : module4.c module4.h module1.h         $(cc) -g -w -wall -c module4.c 

but should not write specific rules this. make has built-ins , general rules simpler use. , can use gcc generate header file dependencies rather tracking them manually (which can lead kinds of disaster). see the -m option.


Popular posts from this blog