后續問題:為什么符合要求的實作與具有內部鏈接的不完整陣列型別的行為不同?.
背景關系:具有內部鏈接的變數的暫定定義具有不完整的非陣列型別:符合要求的實作表現出不同的行為。
示例代碼(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,哪種行為是正確的?
uj5u.com熱心網友回復:
來自 C 標準(6.9.2 外部物件定義)
3 如果物件的識別符號宣告是臨時定義并具有內部鏈接,則宣告的型別不應是不完整的型別。
和(4. 一致性)
1 在本國際標準中,“應”被解釋為對實作或程式的要求;相反, “不應”應解釋為禁止。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/322573.html
下一篇:在c中獲取鏈接指向的檔案的名稱
