缺少具有非保留标识符的对象/函数的定义是否会导致诊断?
示例代码:
void accept(int x);
int main(void)
{
accept(0);
return 0;
}
调用:
$ gcc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
$ clang t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
$ cl t719.c /std:c11 /Za
t719.obj : error LNK2019: unresolved external symbol accept referenced in function main
$ icc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
考虑用户忘记定义 accept
。我们看到可能不会产生任何诊断结果。是否需要诊断?
UPD:另一个例子:
extern int y0;
int main(void)
{
return y0;
}
# linux (begin)
$ gcc t719.c -std=c11 -pedantic -Wall -Wextra
undefined reference to `y0'
$ clang t719.c -std=c11 -pedantic -Wall -Wextra
undefined reference to `y0'
$ icc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 243
# linux (end)
# windows (begin)
# gcc in cygwin
$ gcc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 255
# clang in cygwin
$ clang t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 255
$ cl t719.c /std:c11 /Za
unresolved external symbol _y0 referenced in function _main
$ LLVM/12.0.0/bin/clang t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 72
$ icl -Qstd=c11 t719.c
<nothing>
# program returned: 65
# windows (end)
Sample code:
void accept(int x);
int main(void)
{
accept(0);
return 0;
}
Invocations:
$ gcc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
$ clang t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
$ cl t719.c /std:c11 /Za
t719.obj : error LNK2019: unresolved external symbol accept referenced in function main
$ icc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
Consider that the user forgot to define accept
. We see that no diagnostics may be produced. Is diagnostics required?
UPD: Another example:
extern int y0;
int main(void)
{
return y0;
}
# linux (begin)
$ gcc t719.c -std=c11 -pedantic -Wall -Wextra
undefined reference to `y0'
$ clang t719.c -std=c11 -pedantic -Wall -Wextra
undefined reference to `y0'
$ icc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 243
# linux (end)
# windows (begin)
# gcc in cygwin
$ gcc t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 255
# clang in cygwin
$ clang t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 255
$ cl t719.c /std:c11 /Za
unresolved external symbol _y0 referenced in function _main
$ LLVM/12.0.0/bin/clang t719.c -std=c11 -pedantic -Wall -Wextra
<nothing>
# program returned: 72
$ icl -Qstd=c11 t719.c
<nothing>
# program returned: 65
# windows (end)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
C 2018 §6.9 ¶5 说:
由于违反了“应”,但它不在约束段落中,C 2018 §4 ¶2 做出了这种未定义的行为:
有关诊断的条款,第 5.1.1.3 节,不需要对此进行诊断:
C 2018 §6.9 ¶5 says:
Since a “shall” is violated, but it is not in a constraint paragraph, C 2018 §4 ¶2 makes this undefined behavior:
The clause on diagnostics, §5.1.1.3, does not require a diagnostic for this: