Scott
Scott

Reputation: 10697

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?

Upvotes: 2

Views: 3255

Answers (1)

Greg Hewgill
Greg Hewgill

Reputation: 993085

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.

Upvotes: 9

Related Questions