【发布时间】:2012-01-21 13:20:29
【问题描述】:
我在 MinGW 中使用 C 得到“未知类型名称 'uint8_t'”和其他类似的东西。
我该如何解决这个问题?
【问题讨论】:
-
您是否包含
stdint.h?
我在 MinGW 中使用 C 得到“未知类型名称 'uint8_t'”和其他类似的东西。
我该如何解决这个问题?
【问题讨论】:
stdint.h?
尝试包含stdint.h 或inttypes.h。
【讨论】:
warning: incompatible implicit declaration of built-in function ‘printf’,可能需要显式添加include <stdio.h>(#include <stdint.h>之前)。
要使用uint8_t 类型别名,您必须包含stdint.h 标准头。
【讨论】:
warning: incompatible implicit declaration of built-in function ‘printf’,可能需要显式添加#include <stdio.h>(在#include <stdint.h> 之前)。
要明确:如果您的 #includes 的顺序很重要并且它不是您的设计模式的一部分(阅读:您不知道为什么),那么您需要重新考虑您的设计。很可能,这只是意味着您需要将#include 添加到导致问题的头文件中。
在这一点上,我没有兴趣讨论/捍卫该示例的优点,但我将保留它,因为它说明了编译过程中的一些细微差别以及它们导致错误的原因。
你需要#include stdint.h 在你 #include 任何其他需要它的库接口。
示例:
我的 LCD 库使用 uint8_t 类型。我用一个接口 (Display.h) 和一个实现 (Display.c) 编写了我的库。
在 display.c 中,我有以下包含。
#include <stdint.h>
#include <string.h>
#include <avr/io.h>
#include <Display.h>
#include <GlobalTime.h>
这行得通。
但是,如果我像这样重新排列它们:
#include <string.h>
#include <avr/io.h>
#include <Display.h>
#include <GlobalTime.h>
#include <stdint.h>
我得到了你描述的错误。这是因为Display.h 需要来自stdint.h 的东西,但它无法访问它,因为该信息是在编译Display.h 之后编译的。
所以将 stdint.h 移到任何需要它的库上方,您应该不会再收到错误了。
【讨论】:
Display.h 应该包含一个#include <stdint.h>。不要依赖包含文件为您包含内容。这就是守卫在这里的目的。
Display.h需要stdint.h,不直接把include放在里面是不正常的。
我必须包含“PROJECT_NAME/osdep.h”,其中包括特定于操作系统的配置。
我会使用您感兴趣的类型查看其他文件,并找到它们的定义位置/方式(通过查看包含)。
【讨论】: