Hiding C struct definition

2019-05-22 22:58发布

问题:

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?

回答1:

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.



回答2:

  • You must end a typedef statement with a ;
  • extern typedef makes no sense, just do a typedef.


回答3:

The current syntax is incorrect, you need to put a semicolon after the typedefs and the structs.



回答4:

There is a ';' missing after the typedef.

EDIT:

struct _my_private_struct {...};

Don't use names with a leading underscore. They are reserved for the language or the implementation.