变量声明和定义不匹配

Variable declaration and definition mismatch

我正在使用 C89 编译器(嵌入式系统)。

I 运行 到一些 C 代码中,其中一个 t运行slation 单元将变量定义为 bool varName;,其中 booltypedef unsigned char。另一个t运行slation单元转发声明变量如下:extern char varName;.

这显然是类型不匹配,是一个错误。我的问题是,这具体违反了什么规则?我下意识的反应是这是 ODR 违规,但只有一个定义,所以我不确定这是 ODR 违规。

6.2.7p2

All declarations that refer to the same object or function shall have compatible type; otherwise, the behavior is undefined.

C89 standard有同款

引用同一对象的声明在有关链接的段落中进一步解释:

An identifier declared in different scopes or in the same scope more than once can be made to refer to the same object or function by a process called linkage . There are three kinds of linkage: external, internal, and none.

In the set of translation units and libraries that constitutes an entire program, each instance of a particular identifier with external linkage denotes the same object or function. Within one translation unit, each instance of an identifier with internal linkage denotes the same object or function. Identifiers with no linkage denote unique entities.

兼容类型本质上意味着相同的类型,但有一些小警告(例如,extern int foo[];extern int foo[3]; 兼容)。