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)
CodePudding user response:
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.