Why is it a linker error to #include this file more than once?
<p>This question isn't so much a 'how to solve' question as its a question about <em>why doesn't this work?</em></p>

<p>In C++ I can define a bunch of variables that I want to use across multiple files in a few ways.</p>

<p>I can do it like this:</p>

<pre><code>int superGlobal;
#include "filethatUsesSuperglobal1.h"

int main()
{
  // go.
}
</code></pre>

<p>That way ONLY works if "filethatUsesSuperglobal1.h" has its entire implementation there in the header and no attached .cpp file.</p>

<p>Another way (the "morer correcter" way) is to use extern:</p>

<h3>externvardef.h</h3>

<pre><code>#ifndef externvardef_h
#define externvardef_h
// Defines globals used across multiple files.
// The only way this works is if these are declared
// as "extern" variables
extern int superGlobal;

#endif
</code></pre>

<h3>externvardef.cpp</h3>

<pre><code>#include "externvardef.h"
int superGlobal;
</code></pre>

<h3>filethatUsesSuperglobal1.h</h3>

<pre><code>#include "externvardef.h"
#include &lt;stdio.h&gt;
void go();
</code></pre>

<h3>filethatUsesSuperglobal1.cpp</h3>

<pre><code>#include "filethatUsesSuperglobal1.h"
void go()
{
  printf("%d\n", superGlobal );
}
</code></pre>

<h3>main.cpp</h3>

<pre><code>#include &lt;stdio.h&gt;
#include "externvardef.h"

#include "filethatUsesSuperglobal1.h"

int main()
{
  printf( "%d\n", superGlobal ) ;
  go() ;
}
</code></pre>

<p>This is a small point and a somewhat nit picky question, but <b>Why do I need to declare it extern</b> - should not the #include guards on "externvardef.h" avoid redefinition?</p>

<p>Its a linker error, even though that header file has #include guards around it.  So its not a compiler error, its a linker error, but why.</p>
