Reputation: 79
The .h file:
#ifndef _WORTHLESS_LIB_H_
#define _WORTHLESS_LIB_H_
typedef struct somestuff stuff_type;
#endif
The .c file:
#include "WorthlessLib.h"
struct somestuff
{
bool didOne;
bool didTwo;
};
When I go to compile I get this output:
Error 1 error C2016: C requires that a struct or union has at least one member e:\users\robert\documents\visual studio 2012\projects\worthlesslib\worthlesslib\worthlesslib.c 8 1 WorthlessLib
Error 2 error C2061: syntax error : identifier 'bool' e:\users\robert\documents\visual studio 2012\projects\worthlesslib\worthlesslib\worthlesslib.c 8 1 WorthlessLib
Error 3 error C2061: syntax error : identifier 'didTwo' e:\users\robert\documents\visual studio 2012\projects\worthlesslib\worthlesslib\worthlesslib.c 9 1 WorthlessLib
Error 4 error C2059: syntax error : ';' e:\users\robert\documents\visual studio 2012\projects\worthlesslib\worthlesslib\worthlesslib.c 9 1 WorthlessLib
Error 5 error C2059: syntax error : '}' e:\users\robert\documents\visual studio 2012\projects\worthlesslib\worthlesslib\worthlesslib.c 11 1 WorthlessLib
And all I know to do is check what the syntax is on the web. This seems as bare-bones as it gets. What am I doing wrong?
Upvotes: 1
Views: 1178
Reputation: 490108
You have things out of order. First you need to define (or at least declare) your struct, then you can use the typedef to create an alias to that name. Your header is pretty much useless as it stands anyway (doesn't contain enough for the typedef to compile when/if you include that header somewhere). I'd arrange the header something like this:
#ifndef _WORTHLESS_LIB_H_
#define _WORTHLESS_LIB_H_
struct somestuff
{
bool didOne;
bool didTwo;
};
typedef struct somestuff stuff_type;
#endif
This should compile (i.e., when you try to use struct somestuff
in your typedef, the name is already known) which makes the header usable.
If you're using this in C (as opposed to C++) you'll need to either use _Bool
as the type for the members, or else #include <stdbool.h>
to get bool
defined as an alias for _Bool
, which is the name for Booleans that's built into the language. That assumes C99 or newer -- an older (C89/90) compiler, you'll need to define bool
entirely on your own.
Edit: Oops -- the comments are quite right. You don't really need to declare/define the name before using it in the typedef, so putting the struct
definition in the header isn't necessary. Personally, I think I'd declare it first anyway, so the header would look like:
struct somestuff;
typedef struct somestuff stuff_type;
This leaves somestuff
as an incomplete type for the client of the code, so they can't mess with the contents, but seems (at least to me) a little less likely to leave a reader completely befuddled at what you're doing.
Upvotes: 1
Reputation: 158469
C does not have a bool
type, so declaring your two variables as bool
won't work. This previous thread goes into the various options for boolean values in C, although if using C99
is an option the simple fix would be to add this include:
#include <stdbool.h>
Upvotes: 1