缺少具有非保留标识符的对象/函数的定义是否会导致诊断?

发布于 2025-01-09 06:40:21 字数 1380 浏览 1 评论 0原文

示例代码:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

许你一世情深 2025-01-16 06:40:21

C 2018 §6.9 ¶5 说:

...如果在表达式中使用通过外部链接声明的标识符(而不是作为结果为整数常量的 sizeof_Alignof 运算符的操作数的一部分),在整个程序的某处应该有一个标识符的外部定义;...

由于违反了“应”,但它不在约束段落中,C 2018 §4 ¶2 做出了这种未定义的行为:

如果违反约束或运行时约束之外出现的“应”或“不应”要求,则行为未定义......

有关诊断的条款,第 5.1.1.3 节,不需要对此进行诊断:

如果预处理翻译单元或翻译单元包含违反任何语法规则或约束的行为,即使该行为也明确指定为未定义,一致的实现也应生成至少一条诊断消息(以实现定义的方式标识)或实现定义的。其他情况下不需要生成诊断消息。

C 2018 §6.9 ¶5 says:

… If an identifier declared with external linkage is used in an expression (other than as part of the operand of a sizeof or _Alignof operator whose result is an integer constant), somewhere in the entire program there shall be exactly one external definition for the identifier;…

Since a “shall” is violated, but it is not in a constraint paragraph, C 2018 §4 ¶2 makes this undefined behavior:

If a "shall" or "shall not" requirement that appears outside of a constraint or runtime-constraint is violated, the behavior is undefined…

The clause on diagnostics, §5.1.1.3, does not require a diagnostic for this:

A conforming implementation shall produce at least one diagnostic message (identified in an implementation-defined manner) if a preprocessing translation unit or translation unit contains a violation of any syntax rule or constraint, even if the behavior is also explicitly specified as undefined or implementation-defined. Diagnostic messages need not be produced in other circumstances.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文