cgccheader-files

C header file include error


Hopefully this is a straightforward question... Here's my process to reproduce this issue. First I create my source file:

bash $ cat t.c
#include "t.h"

int main()
{
  ABC abc;
}

Then I create my corresponding header file:

bash $ cat t.h
#ifdef _T_H
#define _T_H

#ifdef __cplusplus
extern "C" {
#endif

typedef struct abc { 
  int a;
} ABC;

#ifdef __cplusplus
}
#endif

#endif

Then, I try to compile it:

bash $ gcc -o t t.c
t.c: In function ‘main’:
t.c:5: error: ‘ABC’ undeclared (first use in this function)
t.c:5: error: (Each undeclared identifier is reported only once
t.c:5: error: for each function it appears in.)
t.c:5: error: expected ‘;’ before ‘abc’

What is going on? If I use 'struct abc' instead of 'ABC' as the type in t.c, then it does compile. Why aren't the typedefs working?


Solution

  • Try:

    #ifndef _T_H
    #define _T_H
    

    I happened to notice this because the _T_H didn't line up, and my subconscious brain knew it should.