You can build sample.c
without including apiInternal.h
and the structure definition.
Within api.h
drop the #include "apiInternal.h"
. Instead, declare the structure, this is called a forward declaration, (easiest to drop the typdef) so api.h
will look like:
struct internalStruct;
struct internalStruct* API_init();
extern void API_set_member1(struct internalStruct* apiStruct, int value);
extern void API_set_member2(struct internalStruct* apiStruct, int value);
This works because the compiler can build sample.c
as it only needs to know the size of the pointer rather than the complete structure declaration. You then only need to include apiInternal.h
within your library code. You won't need to distribute apiInternal.h
to your users.
If you library code has multiple files, then you will need apiInternalh.h
. Each of you library source files will include apiInternal.h
. If you library code is a single file, then I typically just define the structure at the top of the library C file, the extra header is not necessary.
This is called an opaque pointer
. The Wikipedia article provides an additional example. This is nice way of achieving information-hiding from within C.