Using the 'extern' keyword properly

Viewed 9870

There are sources (books, online materials) that explain the usage of extern as following:

extern int i;        // declaration - has 'extern'
int i = 1;           // definition  - specified by the absence of 'extern'

And there are sources that support the following syntax:

extern int i;        // declaration
extern int i = 1;    // definition  - specified by the equal sign
                     // Both marked with 'extern'

My question is - is this a C vs. C++ distinction, or is it a pre-ANSI vs. ANSI practice?

Now, the more practical question:

Using the second syntax, I want to create a global object (visible from every compilation unit). The constructor takes no parameters, so neither parentheses, nor the equal sign are necessary.

extern MyClass myobject;

Now how can the compiler make the distinction between a declaration and the definition?

EDIT: Back at school, I was used to the first syntax (Borland C). Later I used a compiler (probably some ancient version of GCC) that refused to compile a definition without an 'extern'. That is what made me confused.

6 Answers
Related