title: Why do I not need to include main.cpp?
body: <p>In the small example needsExtern.cpp needs the definition of <code>global::bar</code>. needsExtern.cpp would normally include the file with the definition (in this case main.cpp). However, since the file is main.cpp it is not needed.</p>
  
  <p>Why does needsExtern.cpp not need to include main.cpp?</p>
  
  <p>needsExtern.h</p>
  
  <pre><code>struct NeedsExtern
  {
      NeedsExtern();
  };
  </code></pre>
  
  <p>needsExtern.cpp</p>
  
  <pre><code>#include "needsExtern.h"
  #include &lt;iostream&gt;
  
  namespace global
  {
      extern const int bar;
  }
  
  NeedsExtern::NeedsExtern()
  {
      std::cout &lt;&lt; global::bar &lt;&lt; "\n";
  }
  </code></pre>
  
  <p>main.cpp</p>
  
  <pre><code>#include "needsExtern.h"
  
  namespace global
  {
      extern const int bar{26};
  }
  
  void main()
  {
      NeedsExtern ne;
  }
  </code></pre>