0

Consider the following code:

header.h

typedef struct foo_tag__ foo;

int do_something(foo data);

private.h

struct foo_tag__
  {
    /* Some fields go here. */
    int foo1;
    int foo2;
    double some_value;
  };

program.c

#include "header.h"
#include "private.h"

int main()
  {
    /* Do something */
    return(0);
  }

Now when I do this, I get errors all over the place. So is it possible to define opaque types using two different header files? One is public and the other is private which is not placed in the include directory.

EDIT: Based on additional research, I've opted to use void * pointers instead to provide an opaque interface to the library. This question is ok to close. Additionally, the linked question, although similar, is not the same as I was trying to define an opaque type in one header file, and then fill out the definition in a different header file because it is used in more than one file. Furthermore, the linked question does not have an answer marked.

Daniel Rudy
  • 1,411
  • 12
  • 23
  • 2
    possible duplicate of [Opaque Data Type in C without opaque pointer](http://stackoverflow.com/questions/17642911/opaque-data-type-in-c-without-opaque-pointer) – Anonymous Coward Sep 13 '15 at 09:08
  • I don't think this is a duplicate since I'm asking about defining the opaque type in two header files where header.h is the public file and private.h has the actual definition. The definition is not in the implementation of program.c. That is a little different than what you linked. – Daniel Rudy Sep 15 '15 at 03:16
  • 1
    You can't use `do_something` unless you include private.h, but then your type is no longer opaque. This is because the compiler needs to generate code to copy the `foo` parameter passed to `do_something` to the local parameter received by it; and can only do that if it knows the size of `foo`, which can't when the structure is forward declared but not defined. Hence why opaque types need to resort to opaque pointers. So it's the same; where you are trying to hide the definition of `foo` is irrellevant, without it you can't declare a variable of type `foo` or pass a parameter of `type` foo – Anonymous Coward Sep 15 '15 at 17:30
  • Regardless, it's a moot point now since I've opted to use void pointers to solve the issue. – Daniel Rudy Sep 17 '15 at 23:14

0 Answers0