c – #include and mulitple typedefs-Collection of common programming errors

I’m writing an embedded C program in eclipse with gcc and can’t see how to get around a certain issue. I have a typedef in a header file which is protected by an include guard. Since I use this typedef in multiple headers, I need to include it in other headers, however when I try to compile, no matter what I do, one of the headers cannot see the typedef and complains of an unknown type name.

I believe this illustrates the problem:

header a.h:

#ifndef _a_h
#define _a_h

typedef enum {
  USBD_OK   = 0,
  USBD_BUSY,
  USBD_FAIL,
}USBD_Status;

#endif

header b.h:

#ifndef _b_h
#define _b_h

#include "a.h"

extern USBD_Status USB_getStatus(void);

#endif

header c.h:

#ifndef _c_h
#define _c_h

#include "a.h"

extern USBD_Status USBD_Sync(void);

#endif

This always seems to result in the error “unknown type name ‘USBD_Status'” as whichever header is compiled second cannot see the typedef. Removing the include guard from a.h results in the complaint that USBD_Status is being redeclared.

edit: I have double checked all include paths, all includes, all file names and all include guards – there are no duplicates or typos.

  1. It could be that you have another header that uses the same header guard name.

    You could add some code to the top of your a.h that does this:

    #ifdef _a_h_
    #error this header is already defined
    #endif
    

    This way you can track down everywhere that a.h is included and see where any oddities may occur.

    As stated in the comments, your above example works so the problem must lie somewhere else …

Originally posted 2013-11-09 21:06:19.