【问题标题】:unknown type name error with typedef struct and extern declaration - C languagetypedef struct 和 extern 声明的未知类型名称错误 - C 语言
【发布时间】:2017-03-23 00:21:19
【问题描述】:

我想了解我的 C 语言编译器的错误。

在文件 application.h 中,我创建了一个 typedef 结构:

typedef struct
{
    FLOAT64 CoefficientA1_F64;
    FLOAT64 CoefficientA2_F64;
    FLOAT64 CoefficientB0_F64;
    FLOAT64 CoefficientB1_F64;
    FLOAT64 CoefficientB2_F64;
    FLOAT32 OldOldRawValue_F32;
    FLOAT32 OldRawValue_F32;
    FLOAT32 RawValue_F32;
    FLOAT32 OldOldFilteredValue_F32;
    FLOAT32 OldFilteredValue_F32;
    FLOAT32 FilteredValue_F32;
}ButterwothSecondOrderFilterParameter_str;

然后我在另一个文件 temperature.c 中创建变量:

ButterwothSecondOrderFilterParameter_str TMP_TemperatureLowPassFilterParameter_STR;

然后我在 temperature.h 中将这个新变量声明为 extern,以便在另一个文件中使用它:

extern ButterwothSecondOrderFilterParameter_str TMP_TemperatureLowPassFilterParameter_STR;

对于 *.c 文件,我只包含他的相关 *.h 文件,如果我需要另一个 *.h 文件中的全局变量,我将其包含在 *.h 文件中

例如 temperature.c 仅包含 temperature.h 并且要访问 application.h 的 typedef 结构,我将 application.h 包含在 temperature.h 中。

对于我的 *.h 文件,我总是用 :

封装
#ifndef xxxx
#define xxxx
#endif

这是最后一个产生错误的声明:

Description Resource    Path    Location    Type
unknown type name 'ButterwothSecondOrderFilterParameter_str'

我不知道我的错误在哪里......?

【问题讨论】:

  • 如果只包含temperature.h 头文件,而不包含application.h 头文件,那么编译器如何知道ButterwothSecondOrderFilterParameter_str 是什么符号?
  • temperature.h 必须包含 application.h 才能知道类型。您应该能够在temperature.c 的顶部包含temperature.h(仅)并且文件应该编译(这使得标题自包含)。目前,该文件无法编译;标头不是独立的。标头保护用于确保标头是幂等的;即使多次包含,效果也与包含一次相同。
  • 不要用文字解释,用代码解释:请提供minimal reproducible example。目前尚不清楚您的问题来自哪里。

标签: c struct types


【解决方案1】:

我只是在 temperature.h 中包含了 application.h。那么 temperature.h 是 temperature.c 中包含的唯一文件。我补充说 temperature.h 也通常包含在 application.h 中,没有任何后果。

我尝试将 application.h 直接添加到 temperature.c 中,但我遇到了同样的错误。

我试图删除 temperature.h 中的 extern 声明,它可以工作。

所以对于 temperature.h 的类型是未知的,但对于 temperature.c 则不是......我不明白。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多