Here is my setup:
In public.h:
#ifndef PUBLIC_H_
#define PUBLIC_H_
#include "func.h"
/*extern typedef struct _my_private_struct PRIVATE_;*/
typedef struct _my_private_struct PRIVATE_; /* Thanks to larsmans and Simon Richter */
#endif
In struct.h
#ifndef STRUCT_H_
#define STRUCT_H_
struct _my_private_struct {
int i;
};
#endif
In func.h:
#ifndef FUNC_H_
#define FUNC_H_
#include "struct.h"
/* typedef struct _my_private_struct PRIVATE_; */
extern PRIVATE_ * get_new(int);
#endif
In func.c:
#include <stdlib.h>
#include "func.h"
PRIVATE_ * get_new(int i)
{
PRIVATE_ *p = (PRIVATE_ *) malloc(sizeof(PRIVATE_));
if (p == NULL) return NULL;
p->i = i;
return p;
}
In main.c:
#include "public.h"
int main(int argc, char ** argv)
{
PRIVATE_ *p = get_new(2);
return 0;
}
When I compile those file with GCC I’m getting this error:
OLD COMPILE ERROR
multiple storage classes in declaration specifiers
COMPILE ERROR AFTER EDIT
expected ‘=’, ‘,’, ‘;’, ‘asm’, or ‘__attribute__’ before ‘*’ token
Can someone help me out/explain why I’m getting this and how to fix it?
The other answers cover your problem pretty well. However, allow me to add to them and answer your latest comment:
While the error is self-explanatory it’s probably less clear why that’s happening. Consider what happens when you include public.h:
If you trace through this and fully expand the preprocessor, this is what you’ll get:
It should now be obvious why you’re running into problems. Without the typedef in func.h, your
get_newprototype fails because it hasn’t seenPRIVATEyet. OTOH, if you leave the typedef in you’ve defined it twice.Also, it looks like you’re trying to keep that structure private from other code and modules. Even if you do fix the build errors you haven’t really achieved that encapsulation. Consider this:
If you want data privacy in C consider an opaque pointer design. I recommend restructuring your source like this:
Now if you try this:
Edit: To answer the OP’s comment below.
If you have private struct’s methods implemented in more than one compilation unit you can still make it work by moving private’s definition to a dedicated header:
Source files that implement your struct private ‘object’ will include ‘func_implementation.h’. External client code that uses private will include ‘func.h’ only.