1

So I am trying to understand it and what it is used for, I get the difference between a declared and defined variable.

I'll try to explain what I am confused at using the examples in the tutorial.

Example 1:

extern int var;
int main(void)
{
var = 10;
return 0;
}

so here I get that you cannot assign var to 10 because no memory was allocated to it.

Example 2:

#include "somefile.h"
extern int var;
int main(void)
{
var = 10;
return 0;
}

I'm lost here. somefile.h would have something like int var = 0;to declare it correct? ut then why would this program declare it again?

tenkii
  • 449
  • 2
  • 10
  • 23
  • 1
    @Soren He's not using `extern "C"`, he's just using normal `extern`. – Barmar Aug 07 '14 at 23:49
  • @soren Um... no. `extern` and `extern C` are pretty much unrelated. One says to use c name mangling, the other says that a variable with the given name exists somewhere else in teh code that isn't visible at compilation time, so wait until link time to complain about it being undefined. – KitsuneYMG Aug 07 '14 at 23:51
  • @Barmar -- you right -- thanks for the correction, i'll delete my "duplicate" comment.... – Soren Aug 07 '14 at 23:55
  • @Soren You should retract your close vote as well. – Barmar Aug 07 '14 at 23:59
  • @Barmar -- I considered that -- but it is still a duplicate, and you have listed the right duplicate. – Soren Aug 08 '14 at 00:01
  • Yeah, makes sense, since if you retract your close vote you can't re-vote to select mine. – Barmar Aug 08 '14 at 00:05

3 Answers3

3

Obligatory point #1: this relates to global variables, so the first thing you really need/want to do is not learn how to use them, but how to avoid them. They're much more likely to lead to problems than a solution.

That said, at least in the usual case, you put an extern declaration in a header. You put the definition of the variable in one source file, and you include the header in any other file that needs access to that variable.

For example:

//file1.cpp:
int var;

Then the header that declares the variable:

//file1.h:
extern int var;

Then in one file that needs access to the variable:

#include "file1.h"

int main() {
    var = 10; // Yes, this *is* allowed
}

...and in another file that needs access to the variable:

#include "file1.h"

int f() { return var; }

I'll repeat though: this is almost never necessary or desirable. If you're actually using globals very often at all, you're probably doing something wrong (I make it a practice to use a global once a year so I don't forget how, but it's been at least a few years since I used any other than that).

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
0

Basically the extern keyword tells the compiler/linker that it should expect the variable to be instantiated and defined elsewhere in the program, for example a library you are linking to or whatever object file "somefile.h" compiles to. Extern lets the compielr and programmer "know" about a variable while letting another piece of code actually manage it.

Here is a good explanation of extern: https://stackoverflow.com/a/496476/1874323

Community
  • 1
  • 1
BlamKiwi
  • 2,093
  • 2
  • 19
  • 30
0

extern effectively means that somewhere, in all the linked obj files/libraries there exists a variable (in your case) called "var of type int, the compiler doesn't know where, but he'll let the linker find it for you". This keeps the compiler happy.

In your case, you're right, 'int var=1;' must be defined somewhere, actually, in a c\cpp file, not a header (if it was in a header, you wouldn't need to extern it, you could just #include it).

If you extern a variable, but don't define it somewhere, the linker will get unhappy - with an unresolved symbol error (i.e. its saying "you told me to look for a variable called 'var', but I can't find it).

So - theoretically, you should not be able to create an executable with an extern'd variable which isn't defined.

parade
  • 106
  • 3