C语言“conflicting types”编译错误

快速解答:

啊,看来你也遇到了“conflicting types”——类型冲突编译错误。

如果你不是遇到:

  • 循环引用而没有用宏定义来解决。

  • 声明或定义在调用后面。

  • 声明和定义冲突。

  • .h.gch未更新。

那么我想告诉你,你可跟我一样忘了C语言不支持“函数重载”,即你的函数名不能重复。

所以不是你的类型声明(typedef)冲突了,而是函数名冲突了。

报错记录

1
2
3
4
5
6
7
8
9
10
$ mingw32-make.exe test-program
gcc -finput-charset=UTF-8 -std=c99 -c test_main.c -o test_main.o
In file included from test_main.c:3:
ctzidn.h:98:6: error: conflicting types for 'IsNullCId'; have '_Bool(const Citiz
98 | bool IsNullCId(const CitizenIdZip1 cid);
| ^~~~~~~~~
ctzidn.h:97:6: note: previous declaration of 'IsNullCId' with type '_Bool(const
97 | bool IsNullCId(const CitizenId* const cid);
| ^~~~~~~~~
mingw32-make: *** [makefile:62: test_main.o] Error 1

-std=c99这个参数是我后加上去。我注意到stdbool.hbool最终指向_Bool,一想C语言之前好像没这个东西,就查了一下,是C99标准中才支持的,所以就加上了,但是错误并不是在这里。

我甚至多次注意到他说的类型冲突所指向的是函数名,甚至有一次开始想“好像C语言是不是不支持函数重名?”,但是直接否定了,然后兜了一圈回来,才查了一下。这才确定了我C语言白学了啊。

最近好几个C语言项目编译不通过大概就是因为这个。