7

This is from the <iostream>:

namespace std 
{
  extern istream cin;       ///< Linked to standard input
  extern ostream cout;  
...

It seems by using extern the data types defined in other namespaces will just be available?

symfony
  • 915
  • 2
  • 13
  • 20

5 Answers5

9

extern means "these variables are defined in some other compilation unit (.cpp or .lib file)"

In this case, you #include <iostream> into your .cpp file, and because cin and cout are declared as extern, the compiler will let you use them without complaining. Then, when the linker runs, it looks up all of the extern variables and sorts it all out.

Dean Harding
  • 71,468
  • 13
  • 145
  • 180
  • Does it mean `istream` or `cin` is defined in some other unit in the case of `extern istream cin;`? – symfony Mar 25 '10 at 04:51
  • 2
    It means `cin` is. This is not the same thing as the *class definition*, which in this case, is in `iostream`. The *class definition* must always be available in a compilation unit to use objects of that class' type (this is why class definitions are put into *header* files). –  Mar 25 '10 at 04:55
  • 1
    `cin` and `cout` are global variables. In C++, a global variable must be declare in one and only one .cpp file (with some exceptions). So if you just said `istream cin` then the linker would complain when you go to link two files which both declare the variable because it wouldn't know which one to use. So by declaring all but one of them as `extern`, you tell the linker to use the non-`extern` as the "real" one, and the `extern` ones just say "this variable is declared in some other .cpp file". – Dean Harding Mar 25 '10 at 05:02
  • the distinction between *declare* and *define* is critical to understanding this. Please get it right! They are not synonymous. –  Mar 25 '10 at 05:04
7

extern is used to refer to a variable defined in a different compilation unit (for now, you can think of a compilation unit as a .cpp file). The statements in your example declare rather than define cin and cout. It is telling the compiler that the definition of these objects is found in another compilation unit (where they are not declared as extern).

  • But I don't find anywhere that define `cin`,only `istream` is defined in `` – symfony Mar 25 '10 at 04:54
  • `cin` is an object of type `istream`. `istream` is the class (in this case, template class) definition. `cin` is an instance of the `istream` class. The "definition" I refer to in my answer is the definition of `cin`, not `istream`. You need to look up the meaning of *definition* vs. *declaraion*. The statements in your examples are *declarations*. –  Mar 25 '10 at 04:59
  • The *definition* of the `cin` variable is most likely in a run-time library. You won't find it in the standard library header files. –  Mar 25 '10 at 05:00
  • @STingRaySC ,is there a trick to find where it's actually defined? – symfony Mar 25 '10 at 05:04
  • @symfony: No. It's defined in code that's already been compiled into a library that you will link your code with. In general, a variable referred to by an `extern` declaration can be defined in **any** compilation unit that is made available to the linker (in the form of a compiled object file). –  Mar 25 '10 at 05:13
2

No, this is an explicit way to say cin and cout are declared without actually defining them.

Eli Bendersky
  • 263,248
  • 89
  • 350
  • 412
0

The extern keyword tells the compiler that a variable is declared in another source(i.e outside the current scope). The linker then finds this actual declaration and sets up the extern variable to point to the correct location.

variables declared by extern statements will not have any space allocated for them, as they should be properly defined elsewhere. If a variable is declared extern, and the linker finds no actual declaration of it, it will show error.

Eg. extern int i;

//this declares that there is a variable named i of type int, defined somewhere in the program.

Akash sharma
  • 489
  • 4
  • 5
0

Some answers say here that extern means that variable is defined in other compilation unit. Then, following should not compile as no other compilation unit or file is provided to compiler.

extern int a;
int main(){
    std::cout<<a<<std::endl;    //Prints 3
}
int a=3;

So, I think extern is explicitly used to separate the declaration and definition in case of variable as stated in one of answer. I think it is useless in case of functions.