If you #include <stdio.h>
you should get the FILE
typedef with it. That's the only really safe and portable way -- you can't have a typedef without a type to alias, and there's no guarantee about what type FILE
aliases, so every compiler or libc or whatever can have a different one. But you'd need the type to be correct in case anyone actually wants to #include <stdio.h>
, lest the inconsistent definitions cause an error.
Edit:
Now that i think about it, there might be one other way i can think of. It's not a typedef, it's evil macro stuff that works by hijacking the definition of "FILE". I wouldn't recommend it for that reason alone. But it might work for what you need.
#ifdef USES_REAL_FILE_TYPE
#include <stdio.h>
#else
#define FILE void
#endif
/* declare your struct here */
#ifndef USES_REAL_FILE_TYPE
#undef FILE
#endif
Then #define USES_REAL_FILE_TYPE
before you include the file in any code where you need a real FILE *
, and the rest of the code will just see the pointer as a void *
.
I make no guarantees that this won't mess stuff up. In particular, it will break in any case where you want to know anything real about such a fake type, and all code that touches the pointer may need that #define. But if you're dead set against "unnecessary" #includes, it's the only way you're gonna get a FILE *
without interfering with stdio. You're not going to be able to forward declare a typedef.
Edit2:
OK, i checked just to make sure. Not sure how standard it is,or what you can do with it, but...
typedef FILE;
works in Visual C and GCC both, but only when compiling C code. It would appear that the C++ standard explicitly says somewhere that you can't have a typedef without a type. The C one, though, doesn't.
However, it doesn't seem to allow a type to be forward declared, not in GCC anyway. If you try to typedef int FILE;
right afterward, it throws an error about conflicting typedefs. VS, however, seems to allow it, as long as it's to an integer type. Seems typedef X
really means typedef int X
in VS (and apparently, in C99). Either way, GCC won't let you redo the typedef, even to the exact same type.