c - 具有内部链接的变量的暂定定义具有不完整的非数组类型 : conforming implementations show different behavior

标签 c language-lawyer c11 standards-compliance incomplete-type

后续问题:Why do conforming implementations behave differently w.r.t. incomplete array types with internal linkage? .

上下文:具有内部链接的变量的暂定定义具有不完整的非数组类型:一致的实现显示不同的行为。

示例代码(t940.c):

static struct s foo;
static struct s {int a;} foo;

调用:

$ gcc t940.c -c -std=c11 -pedantic -Wall -Wextra -Wno-unused-variable
<nothing>    

$ clang t940.c -c -std=c11 -pedantic -Wall -Wextra -Wno-unused-variable
t940.c:1:17: warning: tentative definition of variable with internal linkage has incomplete non-array type 'struct s' [-Wtentative-definition-incomplete-type]
static struct s foo;
                ^
t940.c:1:15: note: forward declaration of 'struct s'
static struct s foo;
              ^
1 warning generated.

$ cl t940.c /c /std:c11 /Za
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x64
Copyright (C) Microsoft Corporation.  All rights reserved.

t940.c
t940.c(1): error C2079: 'foo' uses undefined struct 's'
t940.c(2): error C2371: 'foo': redefinition; different basic types
t940.c(1): note: see declaration of 'foo'

$ icl t940.c -c -std=c11 -pedantic -Wno-unused-variable
<nothing> 

现场演示:https://godbolt.org/z/9j8E1634q .

工具版本:

$ gcc --version
gcc (GCC) 11.2.0

$ clang --version
clang version 13.0.0

$ cl
Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30133 for x64

# icl is x86-64 icc 2021.1.2 (from godbolt.org)

问题:根据 C11,哪种行为是正确的?

UPD1。创建:https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102821 .

最佳答案

来自 C 标准(6.9.2 外部对象定义)

3 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.

和(4.一致性)

1 In this International Standard, ‘‘shall’’ is to be interpreted as a requirement on an implementation or on a program; conversely, ‘‘shall not’’ is to be interpreted as a prohibition.

关于c - 具有内部链接的变量的暂定定义具有不完整的非数组类型 : conforming implementations show different behavior,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69616044/

相关文章:

c++ - 聚合初始化,将成员指针设置为相同的结构成员

c - 线程共享的全局变量的原子性

c - GCC 中如何维护递归调用堆栈?

c++ - 为什么 reverse_iterator 双重定义其嵌套类型?

c++ - 这种模板函数重载的案例让我无法理解

c - 为什么我的宏中的 `_Generic` 关键字不起作用?

c - 是否可以将winsock2动态链接到我的c/c++程序中

c - 下面的链接列表代码给出了有趣的错误,你能检查一下吗?

c - 当我在全局范围内将 char 变量分配给 int 变量时,为什么编译器会给出错误?

c++ - 删除了默认构造函数。仍然可以创建对象......有时