Home > Mobile >  Tentative definition of variable with internal linkage has incomplete non-array type: conforming imp
Tentative definition of variable with internal linkage has incomplete non-array type: conforming imp

Time:10-19

Follow-up question for: Why do conforming implementations behave differently w.r.t. incomplete array types with internal linkage?.

Context: tentative definition of variable with internal linkage has incomplete non-array type: conforming implementations show different behavior.

Sample code (t940.c):

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

Invocations:

$ 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> 

Live demo: https://godbolt.org/z/9j8E1634q.

Tool versions:

$ 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)

Question: which behavior is correct per C11?

CodePudding user response:

From the C Standard (6.9.2 External object definitions)

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.

and (4. Conformance)

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.

  • Related