【问题标题】:C header file include errorC头文件包含错误
【发布时间】:2010-12-11 03:02:02
【问题描述】:

希望这是一个简单的问题...这是我重现此问题的过程。首先我创建我的源文件:

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 不起作用?

【问题讨论】:

    标签: c gcc compiler-construction header


    【解决方案1】:

    试试:

    #ifndef _T_H
    #define _T_H
    

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

    【讨论】:

    • 宾果游戏。哇,是的,那一点点'n'搞砸了一切。好吧,希望有这个问题的其他人会看到这个帖子:)
    • 顺便说一句,不要使用以下划线开头或包含双下划线的内容作为标题保护。此类名称保留供标准库实现使用。只需T_H 就可以了。
    猜你喜欢
    • 1970-01-01
    • 2014-01-04
    • 2018-10-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-12
    相关资源
    最近更新 更多