Re: Help wanted on some source codes



slebetman@xxxxxxxxx said:

> But I didn't think all globals are external
> linkage by default. I thought you need to use the 'extern' keyword for
> that.

No. When you do this:

extern int foo;

you are saying "dear compiler, I promise to you that there is an object
called foo, which is an int, but its storage is already reserved elsewhere
so you don't need to reserve any; just trust me on this". Each translation
unit needing to read or write foo (EXCEPT ONE) will need to have such a
line. Typically, such lines are put into a header.

In exactly one place in your program (at file scope), you must keep your
promise:

int foo;

The translation unit that has this line need not have an extern int foo; as
well (although it will do no harm if it does).

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • Re: #define question
    ... Pascal/Delphi programmer writing a software program ... #define EXTERN extern ... foo.c owns, say, int foo. ... The drawback is that one develops a bad habit of defining a macro differently in different translation units; that's a demonstrated recipe for disaster. ...
    (comp.lang.c)
  • Re: shareable image, psect and data
    ... :extern int foo; ... Take a look at the declaration scope for whatever C instantiation and ... As a start on shareable images, see the Shareable Image Cookbook in ...
    (comp.os.vms)
  • Re: Help wanted on some source codes
    ... I thought you need to use the 'extern' keyword for ... > extern int foo; ... > unit needing to read or write foo will need to have such a ... > The translation unit that has this line need not have an extern int foo; ...
    (comp.lang.c)
  • Re: extern
    ... For objects it is a "tentative declaration" NOT an extern, although it has external linkage. ... If nothing else is seen by the end of the translation unit then it is as if there was a declaration with a 0 initialiser at the end of the translation unit. ...
    (comp.lang.c)
  • Re: extern
    ... >> without extern. ... > I'd *highly* suggest you use the extern keyword when declaring external ... 6.9.2/2 "A declaration of an identifier for an object that has file ... If a translation unit contains one or more tentative ...
    (comp.lang.c)