MinGW中的消息“unknown type name 'uint8_t'”

96

在MinGW中使用C时,我遇到了类似于“未知类型名称'uint8_t'”等的问题。

我该如何解决?


13
你是否包含了 stdint.h 头文件? - Necrolis
4个回答

186
尝试包含 stdint.hinttypes.h

2
我仍然收到uint32_t的错误,但包含stdint.h解决了其他问题。 - RobotRock
1
对于前者,为了避免警告“warning: incompatible implicit declaration of built-in function ‘printf’”,可能需要在#include <stdint.h>之前显式添加include <stdio.h> - Peter Mortensen

24
为了使用uint8_t类型别名,您需要包含标准头文件stdint.h

为了避免警告,warning: incompatible implicit declaration of built-in function ‘printf’,可能需要在 #include <stdint.h> 之前显式添加 #include <stdio.h> - Peter Mortensen

8
明确一点:如果你的#include顺序很重要,而且它不是你的设计模式的一部分(也就是说,你不知道为什么),那么你需要重新考虑你的设计。很可能,这只意味着你需要将导致问题的#include添加到头文件中。
目前,我对讨论/捍卫示例的优点没有兴趣,但我会将其保留,因为它展示了编译过程中的一些细微差别以及为什么会导致错误。
在你#include任何其他需要它的库接口之前,你需要#includestdint.h例子: 我的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移到需要它的任何库的上面,就不会再出现此错误了。

22
这只是设计不良,Display.h 应该包含 #include <stdint.h>。不要依赖其他文件来替你包含内容,这就是头文件保护存在的原因。 - Jerska
这有点不完整。你能否扩展一下或提供一个参考,说明为什么包含文件不应该在源文件中?我的包含文件没有提供库函数所需的类型,因此我认为它们不需要在头文件中。 - LanchPad
3
抱歉,我的表述不够清晰。'Display.h'中提供的函数并不需要'stdint.h'。这些函数也可以接受在'Display.h'中定义的类型。由于实现'display.h'的程序并不一定需要实现'stdint.h',所以我认为没有必要在程序员未明确输入'#include <stdint.h>'的情况下暴露'stdint.h'库。 - LanchPad
这是一篇关于C语言模块化编程的教程。虽然已经有一段时间了,但我相信这就是我设计库时所遵循的教程。 - LanchPad
如果在 #include <stdint.h> 之前没有显式的 #include <stdio.h> ,则可能会出现警告:warning: incompatible implicit declaration of built-in function ‘printf’ - Peter Mortensen
显示剩余6条评论

0

我必须包含 "PROJECT_NAME/osdep.h",这将包括特定于操作系统的配置。

我会查看使用您感兴趣的类型的其他文件,并查找它们是如何定义的(通过查看包含)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接