As for working on a larger project, I want to throw in some own types (e.g. myType
). The "rushing in"-approach would be, to put those typedefs into an header (lets say myType.h
), together with a bunch of functions working on those types.
If I use the new type somewhere, I include myType.h
to my source-file. Fine.
But if I want to use the new type somewhere as an argument in a function-signature, I need to include the myType.h
to the header of the module containing the function. With one or another typedef, this seems to be okay for me, but the more types I have, the more includes in headers I need, possible including further header, while using type including other own types. This is resulting in what I call "dependency hell".
Is there a clever, stylish, best practice, what-so-ever way to solve this dilemma?
I'm aware of the possibility to pass those types as void-pointer, casting them back inside the function, but then I loose important type-checking from the compiler.
Furher, extern
is considered worst-practice around here..
EDIT:
In detail:
myType.h
:
#include "otherType.h"
typedef struct {
char Name[32];
int Size;
otherType someType;
} myType;
processSomeHow(myType _myType, int NewSize);
otherType.h
#define SOME_CONST 32
typedef struct { [...] } otherType;
someModule.h
:
#include "myType.h"
int specialProcessSomeHow(myType _myType);
someModule.c
:
int specialProcessSomeHow(myType _myType)
{
int Size = 64;
return(processSomeHow(_myType, Size));
}
Now I include otherType.h
indirectly to someModule.h
, even worse, I include it to every module, that includes someModule.h
. Now I have a SOME_CONST
everywhere and it's hard to figure out, from where it comes. I have to maintain two include trees.
like in the gtk library you can use one headfile and split it on your needs.
type.h
- myType.h
-- someType.h
- otherType.h
- List item
and on your CONST-Problem: If you just need it for one c.file. Don't use them in HeaderFile. And you could name them like "MY_TYPE_SOME_CONST" or "OTHER_TYPE_SOME_CONST";
//EDIT:
to make it clear: just add 'this.h' file and name it.
#ifndef TYPE_H_
#define TYPE_H_
#include myType.h
#include someType.h
#include otherType.h
#endif /* TYPE_H_ */
now you can use "#include this.h" for each file you need your types. (this.h is not real, name it to something unique)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With