0

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
  • 2
    Code is correct, double and triple check for typos: USDB instead of USBD, accidental used of the same guards, etc. – chill Nov 20 '12 at 17:46
  • Good job using a simple self contained correct example. But that's the thing, this is correct, so it should work. You either have a typo, or you're not including both `b.h` and `c.h` – durron597 Nov 20 '12 at 18:00
  • I've managed to sort of fix the issue for now. I moved the typedef into a new header file and included this one separately. this initially didn't fix the issue until I started messing with the order of my include files. It seems to be compiling for now. – user1839626 Nov 21 '12 at 16:55

1 Answers1

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 ...

Goz
  • 61,365
  • 24
  • 124
  • 204
  • This isn't the issue. I've tried changing the header guard name and searching all files for duplicates and all is fine. I've now commented out the section of code that causes this error and i'm getting similar problems elsewhere in the code (they are libraries by ST, not my code). The only other option I can think of is a compiler optimisation issue, where the compiler doesn't think the typedef is being used anywhere and so isn't storing it. – user1839626 Nov 21 '12 at 09:27
  • 1
    @user1839626: I would be willing to bet money its not a compiler issue. Genuine compiler bugs are VERY rare. – Goz Nov 21 '12 at 10:14