1

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?

CANTPRO
  • 79
  • 9

2 Answers2

1

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.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • Pasting the struct above the typedef results in the same. – CANTPRO Jun 11 '13 at 02:42
  • 1
    The whole point is, presumably, to conceal the details of the structure type from the users of the header. As such, the original is fine. – Jonathan Leffler Jun 11 '13 at 02:50
  • Funnily enough (or not) the typedef compiles fine in the order presented, at least in VS2012. The problem is the bool, which another typedef and #defines took care of. – CANTPRO Jun 11 '13 at 02:50
1

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>
Community
  • 1
  • 1
Shafik Yaghmour
  • 154,301
  • 39
  • 440
  • 740
  • Nitpick: with any other programming language, if you don't mention a version number, the most recent stable version is assumed. Why then is C so commonly interpreted as C89 and not C11 (or at least C99)? Let's stop doing that. Your first sentence should be "C89 does not have a bool type". – Will Jun 11 '13 at 05:16
  • The reason that C89 is assumed, @Will, is that MSVC only supports C89 and the error messages are from MSVC. It's not a good reason, but it requires more knowledge than the average beginner has to know otherwise. – Jonathan Leffler Jun 11 '13 at 05:50
  • 1
    @JonathanLeffler Fair enough.... but something like this would still be good to tell a beginner though: "The MSVC compiler you're using only supports the old C89 standard version of C. C89 does not have a bool type.". Probably better that C programmers abandon the MSVC ship sooner rather than later anyway. – Will Jun 11 '13 at 06:01
  • @JonathanLeffler This is an interesting point about mentioning versions, I have to think about it more. I think most compilers default to some flavor of `C89`, are there any that default to `C99`? – Shafik Yaghmour Jun 11 '13 at 09:31