title: Namespaces with external linkage
body: <p>The problem I have is basically the same as 'greentype' mentions at
  <a href="http://www.cplusplus.com/forum/beginner/12458/" rel="noreferrer">http://www.cplusplus.com/forum/beginner/12458/</a></p>
  
  <p>I'm sharing variables through namespaces and a problem arises when I try to put my function definitions into a separate file.</p>
  
  <p>Consider the following example, where I want to pass variable 'i', defined
  in the main code, to the function a():</p>
  
  <hr>
  
  <p><strong>* nn.h: *</strong></p>
  
  <pre><code>#ifndef _NN_H_
  #define _NN_H_
  
  namespace nn {
  int i;
  }
  #endif
  </code></pre>
  
  <p><strong>* main.cpp *</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "nn.h"
  using namespace std;
  using namespace nn;
  
  void a();
  
  int main()
  {
  i=5;
  a();
  }
  
  void a()
  {
  using namespace std;
  using namespace nn;
  
  i++;
  cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; endl;
  }
  </code></pre>
  
  <hr>
  
  <p>But now if I put the definition of a() into a separate file ...</p>
  
  <hr>
  
  <p><strong>* a.cpp *</strong></p>
  
  <pre><code>#include &lt;iostream&gt;
  #include "nn.h"
  
  void a()
  {
  using namespace std;
  using namespace nn;
  
  i++;
  cout &lt;&lt; "i = " &lt;&lt; i &lt;&lt; endl;
  }
  </code></pre>
  
  <hr>
  
  <p>... then I get 'multiple definition' error when linking (g++ main.cpp
  a.cpp -o main). If I make 'i' declaration in the header file 'extern' (as
  suggested in other forums), I get 'undefined reference' error. I can compile when 'i' is declared as const in the header, but that's not what I want.</p>
  
  <p>Any suggestions greatly appreciated. </p>