2010-12-11 114 views
2

希望这是一个直截了当的问题......以下是我重现此问题的过程。首先,我创建我的源文件:C头文件包含错误

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

int main() 
{ 
    ABC abc; 
} 

然后创建我相应的头文件:

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 

然后,我尝试编译:

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’ 

这是怎么回事?如果我使用'struct abc'而不是'ABC'作为t.c中的类型,那么它会编译。为什么不能使用typedef?

回答

9

尝试:

#ifndef _T_H 
#define _T_H 

我碰巧注意到这一点,因为_T_H没有排队,我的大脑潜意识知道它应该。

+0

宾果。哇,是的,一个小小的'n'搞砸了一切。希望其他人有这个问题会看到这个线程:) – Scott 2010-12-11 03:06:55

+0

顺便说一句,不要使用以下划线开头,或包含双下划线的东西为您的标头警卫。这些名称保留供标准库实现使用。只需'T_H'就可以做到这一点。 – 2010-12-11 09:45:55