[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: |
Mon, 1 Apr 2013 22:41:44 +0100 |
User-agent: |
Mutt/1.5.21 (2010-09-15) |
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.:
w.c:
int i; // these are all tentative declarations
int i; // which become a single definiton upon compilation
int i;
int i;
Outputs i with the value zero.
x.c:
int i; // tenative declaration
int i = 2; // definition
int i; // another tenative declaration
int i = 3; // conflicting definition - error
Ignoring the last line, this outputs i with the value 2.
y.c:
extern int i; // extern declaration
int i; // tenative declaration
int i = 2; // definition
Outputs i with the value 2.
z.c:
extern int i;
/* Not a tentative declaration:
* "An external object declaration that does not have an initializer,
* and does not contain the extern specifier, is a tentative
* definition"
*/
Outputs nothing.
Anyway, this is more to do with a single translation unit, your issue is
with the same definition in multiple object files (I say definition from
the multiple declarations being collapsed into one definition).
Easy enough to change in tcc but I'm interested what the C Standard says
on this.
Rob
- [Tinycc-devel] global variables in tcc, Lluís Batlle i Rossell, 2013/04/01
- Re: [Tinycc-devel] global variables in tcc, Rob, 2013/04/01
- Re: [Tinycc-devel] global variables in tcc, Lluís Batlle i Rossell, 2013/04/01
- Re: [Tinycc-devel] global variables in tcc, Daniel Glöckner, 2013/04/01
- Re: [Tinycc-devel] global variables in tcc, Lluís Batlle i Rossell, 2013/04/01
- Re: [Tinycc-devel] global variables in tcc,
Rob <=
- Re: [Tinycc-devel] global variables in tcc, Lluís Batlle i Rossell, 2013/04/02
- Re: [Tinycc-devel] global variables in tcc, Rob, 2013/04/02
- Re: [Tinycc-devel] global variables in tcc, grischka, 2013/04/02
- Re: [Tinycc-devel] global variables in tcc, Lluís Batlle i Rossell, 2013/04/02
- Re: [Tinycc-devel] global variables in tcc, Rob, 2013/04/04