开发者

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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜