Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #172997

Re: Clang vs GCC on block scope declaration of inline function

From Tim Rentsch <tr.17687@z991.linuxsc.com>
Newsgroups comp.lang.c
Subject Re: Clang vs GCC on block scope declaration of inline function
Date 2023-08-28 06:46 -0700
Organization A noiseless patient Spider
Message-ID <86msybxot4.fsf@linuxsc.com> (permalink)
References <0beab85a-6ce0-4f29-b6c8-9346581b11c0n@googlegroups.com>

Show all headers | View raw


arnab chatterjee <arnabchatterjeeofficial@gmail.com> writes:

> Hello all,
>
> I'd like to confirm if there's a bug in GCC's compilation of the
> following code:
>
> int main(void) { void f(void); f(); }
> inline void f(void) {}
>
> gcc successfully generates an executable, but clang gives the
> following error:  " undefined reference to `f' ".

My reading of the C standard is that what gcc does and what clang
does are both allowed by the standard.  The declarations of f()
give it external linkage.  There is a use of f().  Because of the
use, there must be an external definition of f(), but there isn't
one.  Because there isn't, the program has undefined behavior.
Hence either working or giving a link error is allowed.

> It seems to me that a linker error is expected here, because (all)
> file scope declarations of `f' specify inline without extern,so its
> given definition should be an inline definition, not an external one
> (also not prohibiting the latter).

It might be reasonable to expect a link error, but it is not
required, by virtue of the situation being undefined behavior.

> Consequently, adding a (non-inline) external definition of `f' in
> another file makes the linker complain about "multiple definition of
> `f'" when the first file is compiled with gcc, but an executable is
> generated when compiled with clang.

My reading of this result is that what gcc does does not conform
to what the C standard says.  AFAICT the program is conforming
and has no undefined behavior, so it should work.  In this case
clang is right, and gcc is wrong.

> Also, explicitly specifying inline in the block scope declaration
> makes gcc's outcome consistent with that of clang (ld reports
> "undefined reference to `f'").

That should make no difference.  The one-file case is still
undefined behavior, the two-file case is still well-defined
behavior.

Back to comp.lang.c | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Clang vs GCC on block scope declaration of inline function arnab chatterjee <arnabchatterjeeofficial@gmail.com> - 2023-08-28 01:30 -0700
  Re: Clang vs GCC on block scope declaration of inline function Tim Rentsch <tr.17687@z991.linuxsc.com> - 2023-08-28 06:46 -0700
    Re: Clang vs GCC on block scope declaration of inline function arnab chatterjee <arnabchatterjeeofficial@gmail.com> - 2023-08-28 21:30 -0700
  Re: Clang vs GCC on block scope declaration of inline function Kaz Kylheku <864-117-4973@kylheku.com> - 2023-08-28 16:20 +0000
    Re: Clang vs GCC on block scope declaration of inline function arnab chatterjee <arnabchatterjeeofficial@gmail.com> - 2023-08-28 21:30 -0700

csiph-web