如何修复 C 错误:unknown type name 'bool'

问题:

你有类似这样的 C 代码

main.c
bool mybool;

但当你尝试编译它时,你看到类似这样的错误消息

unknown_type_bool_output.txt
main.c: In function ‘main’:
main.c:3:5: error: unknown type name ‘bool’; did you mean ‘_Bool’?
     bool mybool;
     ^~~~
     _Bool

解决方案

添加

main_fixed.c
#include <stdbool.h>

在错误发生的源文件顶部。

此错误的原因是 bool 不是 C 中的标准类型(如 intchar),而只在 stdbool.h 中。


Check out similar posts by category: C/C++, GCC Errors