【问题标题】:Typedef error after including header file multiple times多次包含头文件后的Typedef错误
【发布时间】:2021-07-23 00:48:09
【问题描述】:

我正在编写针对基于 SAME51 的 Atmel/Microchip 处理器的 c 代码。我正在使用 Microchip 的 MPLABX 编译和运行代码。

我的头文件多次包含在多个 .c 文件中。当我将triac.h 包含到keypad.c 中时,我遇到了错误:

In file included from ../src/keypad.c:23:0:
../src/triac.h:39:54: error: expected ')' before 'uintptr_t'
 typedef void (*p_triac_isr_reg)(TC_COMPARE_CALLBACK, uintptr_t);
                                                      ^~~~~~~~~
../src/triac.h:78:5: error: unknown type name 'p_triac_isr_reg'
     p_triac_isr_reg isr_register;

triac.h:

#ifndef TRIAC_H
#define TRIAC_H

#include <stdint.h>
#include <stdbool.h>
#include "./config/Test/peripheral/eic/plib_eic.h"
#include "./config/Test/peripheral/tc/plib_tc_common.h"  // << Needed to add this

typedef void (*p_triac_funct)();
typedef void (*p_triac_isr_reg)(TC_COMPARE_CALLBACK, uintptr_t);

typedef struct triac_t
{
    bool is_enabled;
    uint16_t current_setting;
    uint16_t min_period;
    uint16_t max_period;
    p_triac_funct isr;
    p_triac_isr_reg isr_register;
    bool (*set_output)(uint16_t);
    uint16_t (*get_output)();
} triac_t;

extern triac_t *p_ac_triac;      
extern frequency_t lineFrequency;   // Line Frequency (FREQ_50HZ or FREQ_60HZ)

#endif // TRIAC_H

我已将triac.h 包含在triac.ckeypad.c 和其他几个文件中。如果我从keypad.c 中删除#include "triac.h",则一切编译正常,没有任何错误。在这一点上我很困惑。

更新

我需要将#include "./config/Test/peripheral/tc/plib_tc_common.h" 添加到我的triac.h 谢谢@tstanisl!我以为它包含在"./config/Test/peripheral/eic/plib_eic.h" 中,但我错了。我很抱歉没有提供所有必要的部分,但代码依赖于使用 MPLAB Harmony 框架。

plib_tc_common.h

typedef void (*TC_COMPARE_CALLBACK) (TC_COMPARE_STATUS status, uintptr_t context);

我被这个错误吓跑了,因为同一个头文件包含在多个其他 .c 文件中而没有任何错误。其他.c 文件没有定义TC_COMPARE_CALLBACK 的任何#includes,所以我仍然不确定为什么它以前可以工作。

【问题讨论】:

  • 看起来缺少 TC_COMPARE_CALLBACK 的定义。缺少标题?
  • 你要关闭#ifndef TRIAC_H吗?
  • @jtilles 发布的任何代码都应该能够自行复制问题。
  • 请创建一个合适的minimal reproducible example 向我们展示。不相关的问题只会分散您所询问的实际问题的注意力。
  • 您应该将问题保留原样,然后发布您自己问题的答案(或者,如果该问题对社区资源没有任何好处,则直接删除该问题)。不要编辑问题以包含解决方案。现在作为一个问题,它没有任何意义,因为您已经更正了代码,它不再显示错误。

标签: c embedded microchip atmel mplab


【解决方案1】:

现在已经在 cmets 中确定了错误原因,只剩下这种不明确的地方:

我被这个错误吓跑了,因为同一个头文件包含在多个其他 .c 文件中而没有任何错误。其他.c 文件没有定义TC_COMPARE_CALLBACK 的任何#includes,所以我仍然不确定为什么它以前有效。

原因肯定是之前你没有在之前添加&lt;stdint.h&gt;

typedef void (*p_triac_isr_reg)(TC_COMPARE_CALLBACK, uintptr_t);

(如您在更新帖子之前的原始问题);这样,TC_COMPARE_CALLBACKuintptr_t 只是旧式的、仍然合法的函数参数列表中的任意参数名称(没有类型)。

【讨论】:

  • 你错了。 A parameter type list specifies the types of, and may declare identifiers for, the parameters of the function.。所以像int foo(int, double); 这样的声明是有效的。
  • 这不是关于int foo(int, double),而是关于void (*p_triac_isr_reg)(TC_COMPARE_CALLBACK, uintptr_t),你错过了.c文件没有任何#includes定义TC_COMPARE_CALLBACK,因此这不是一种类型。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-04
  • 1970-01-01
  • 2018-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多