tinycc-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Tinycc-devel] global variables in tcc


From: Rob
Subject: Re: [Tinycc-devel] global variables in tcc
Date: Tue, 2 Apr 2013 13:54:56 +0100

On 2 April 2013 13:10, Lluís Batlle i Rossell <address@hidden> wrote:
> On Mon, Apr 01, 2013 at 10:41:44PM +0100, Rob wrote:
> > On Mon, Apr 01, 2013 at 09:27:22PM +0200, Lluís Batlle i Rossell wrote:
> > > On Mon, Apr 01, 2013 at 08:58:06PM +0200, Daniel Glöckner wrote:
> > > > On Mon, Apr 01, 2013 at 04:55:18PM +0200, Lluís Batlle i Rossell wrote:
> > > > > > GCC and Clang allow these to be merged together at link-time,
> > > > > > which means users who make mistakes such as missing `extern' in
> > > > > > header files still get the multiple definitions merged.
> > > > > >
> > > > > > tcc is stricter and perhaps more conformant in disallowing this.
> > > > >
> > > > > I think that the C standard requires the gcc and clang behaviour,
> > > > > 'extern' never being a required word.
> > > > >
> > > > > No?
> > > >
> > > > I just took a look inside the N1570 C11 draft.
> > > > Chapter 6.9 paragraph 5 says:
> > > >
> > > > "If an identifier declared with external linkage is used in an
> > > > expression ([...]), somewhere in the entire program there shall be
> > > > exactly one external definition for the identifier; otherwise, there
> > > > shall be no more than one."
> > >
> > > From K&R:
> > >
> > > "An external declaration for an object is a definition if it has an
> > > initializer.  An external object declaration that does not have an
> > > initializer, and does not contain the extern specifier, is a tentative
> > > definition. If a definition for an object appears in a translation
> > > unit, any tentative definitions are treated merely as redundant
> > > declarations. If no definition for the object appears in the
> > > translation unit, all its tentative definitions become a single
> > > definition with initializer 0."
> > >
> > > I don't know if this is a fight between K&R and the C11 draft. :)
> > >
> > > Regards,
> > > Lluís.
> >
> > I think that just means per translation unit, i.e.:
>
> As I can read in C99 (6.9.2), I agree with you.
>
> But K&R (Prentice Hall, 2nd edition) adds after the cited paragraph (A10.2):
>
> "Each object must have exactly one definition. For objects with internal 
> linkage,
> this rule applies separately to each translation unit, because 
> internally-linked
> objects are unique to a translation unit. For objects with external linkage, 
> it
> *applies to the entire program*."

In that case, I suppose it depends how you define "definition"
(ironically) and "declaration".
I guess "int i;" in each file could be seen as multiple declarations
which the linker collapses into one definition at link-time, in which
case, GCC and Clang are correct. Or "int i;" could be seen as becoming
a definition when a single translation unit is compiled, so then you
get multiple definitions at link time, in which case tcc is correct.

Anyways, perhaps there should be a -fcommon flag on tcc, just like
gcc/clang. If I have time tonight I'll have a hack around and see if I
can add one. I still think it's wiser to have the symbols conflict
(current tcc behaviour) since the general usage is declaring a
variable extern in a header then instantiating it in a single file.

Cheers,
Rob



reply via email to

[Prev in Thread] Current Thread [Next in Thread]