tech-userlevel archive

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

Re: style(5) proposal: forbid extern in .c



>>> extern struct netif_stats	le_stats[];
>>> ...
>>> struct netif_stats le_stats[NLE_IFS];

>> [riastradh@ and I say to s/extern/static/ and add static to the
>> later declaration, if le_stats can be file-local]

> Thanks, using 'static' as a forward declaration just works.

I may be misreading here, but this sounds as though you're converting
the `extern' to `static' but _not_ adding `static' to the later
declaration.

If so, well, this is not valid C, at least not as of C99.  If it "just
works", that is because the compiler in use is willing to wave off the
violation, not because it is correct.  (Or, potentially, it is adhering
to a different standard, though I'd be surprised if any other standard
differs in this respect.)

C99 6.2.2, "Linkages of identifiers"

       [#3]  If  the  declaration of a file scope identifier for an
       object or a function contains  the  storage-class  specifier
       static, the identifier has internal linkage.22)

(the 22 is a reference to a footnote that's about functions and thus
not relevant here).  So, the first declaration gives it internal
linkage.

       [#5] [...text about functions...
                      ...]  If the declaration of an identifier for
       an object has file scope and no storage-class specifier, its
       linkage is external.

And the second declaration gives it external linkage.

       [#7]  If,  within  a  translation  unit, the same identifier
       appears  with  both  internal  and  external  linkage,   the
       behavior is undefined.

Hence, undefined.

/~\ The ASCII				  Mouse
\ / Ribbon Campaign
 X  Against HTML		mouse%rodents-montreal.org@localhost
/ \ Email!	     7D C8 61 52 5D E7 2D 39  4E F1 31 3E E8 B3 27 4B


Home | Main Index | Thread Index | Old Index