不完整类型结构的暂定定义

Tentative definition of struct with incomplete type

将以下内容视为 C 文件:

static struct S a;

int main() {
  return (long)&a;
}

struct S {
  int b;
} s;

根据我对 C11 spec 的阅读,我认为这是未定义的行为。 6.9.2 状态:

A declaration of an identifier for an object that has file scope without an initializer, and without a storage-class specifier or with the storage-class specifier static, constitutes a tentative definition.

在语义标题下(不是约束):

If the declaration of an identifier for an object is a tentative definition and has internal linkage, the declared type shall not be an incomplete type.

第一行的声明好像是暂定的,objecta有内联,但struct S当时的类型不完整声明。所以,我希望这会违反第二个引号,从而导致未定义的行为。

但是,当 运行 带有 --std=c11 -Wall -pedantic 标志时,GCC 不会打印任何诊断信息。我是不是误解了标准,或者 GCC 没有为这种未定义的行为打印诊断信息?

是的,这是未定义的。

未定义的行为正是该术语所表示的,它没有被标准定义。任何编译器都可以添加自己的定义,从而扩展标准,并且没有义务诊断其中的任何一个。特别是,gcc 对暂定定义有一些特殊的想法。使用这些的代码不可移植。