The other answers cover your problem pretty well. However, allow me to add to them and answer your latest comment:
I'm getting compile error: in public.h: redefinition of typedef PRIVATE_...
While the error is self-explanatory it's probably less clear why that's happening. Consider what happens when you include public.h:
#include "struct.h"
#include "func.h"
typedef struct _my_private_struct PRIVATE_;
If you trace through this and fully expand the preprocessor, this is what you'll get:
// struct.h
struct _my_private_struct
{
int i;
};
// func.h
typedef struct _my_private_struct PRIVATE_;
extern PRIVATE_ * get_new(int);
// public.h
typedef struct _my_private_struct PRIVATE_;
It should now be obvious why you're running into problems. Without the typedef in func.h, your get_new
prototype fails because it hasn't seen PRIVATE
yet. 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:
int main()
{
PRIVATE_ *p = get_new(2);
p->i = 1337; // HAHA, I just modified your private i.
// what are you going to do about it?
}
If you want data privacy in C consider an opaque pointer design. I recommend restructuring your source like this:
// public.h
#ifndef PUBLIC_H_
#define PUBLIC_H_
#include "func.h"
#endif
// func.h
#ifndef FUNC_H_
#define FUNC_H_
struct PRIVATE_NOT_ACCESSIBLE;
typedef struct PRIVATE_NOT_ACCESSIBLE myint_t;
// declare your struct methods here
myint_t* get_new(int);
// ..
#endif
// func.c
#include <stdlib.h>
#include "func.h"
// define this only with functions
// that's suppose to work with its internal data
struct PRIVATE_NOT_ACCESSIBLE
{
int i;
};
myint_t * get_new(int i)
{
// ...
}
Now if you try this:
#include "public.h"
int main()
{
myint_t *p = get_new(2);
p->i = 1337; // Aw, doesn't work anymore :(
}
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:
// func_implementation.h
#include "func.h"
struct PRIVATE_NOT_ACCESSIBLE
{
int i;
};
// internal methods, helper functions you don't want exposed should go here too.
// eg.
void helper_method(myint_t *);
Source files that implement your struct private 'object' will include 'func_implementation.h'. External client code that uses private will include 'func.h' only.