title: Undefined Reference to namespaces in C++
body: <p>I am getting an undefined reference when trying to use variables from my namespace. I created a header file and an implementation file with the namespace in it and am trying to use the namespace in  another file...</p>
  
  <p><strong>EDITED:</strong></p>
  
  <pre><code>//first.h
  namespace first
  {
    extern int var;
    extern int multiplyVar(int);
  }
  
  //first.cpp
  namespace first
  {
    int var = 5;
    int multiplyVar(int mult)
    {
      return mult * var;
    }
  }
  
  //someOtherFile.h
  #include "first.h"
  
  //someOtherFile.cpp
  first::var = 3;
  int newVar = first::multiplyVar(3);
  
  //error
  undefined reference to  'first::multiplyVar(...)'
  undefined reference to 'first::var'
  </code></pre>
  
  <p><strong>EDIT</strong> Actual Code</p>
  
  <pre><code>//jFork.h
  #ifndef JFORK_H
  #define JFORK_H
  
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  namespace jFork
  {
    extern int sockfd, newsockfd;
    int j_fork(string);
  }
  
  #endif //JWDSFORK_H
  
  //jFork.cpp
  namespace jFork
  {
    int sockfd = 0, newsockfd = 0;
  
    int j_fork(string name)
    {
      cout&lt;&lt;"Preparing to fork: "&lt;&lt;name&lt;&lt;endl;
  
      int rv = fork();
  
      cout&lt;&lt;"Called fork(): "&lt;&lt;name&lt;&lt;endl;
  
      switch(rv)
      {
      case -1:
          cout&lt;&lt;"Exiting..."&lt;&lt;endl;
          exit(EXIT_FAILURE);
          break;
      case 0:
          if(sockfd)
          {
              cout&lt;&lt;"Closing sockfd: "&lt;&lt;name&lt;&lt;endl;
              close(sockfd);
              sockfd = 0;
          }
  
          if(newsockfd)
          {
              cout&lt;&lt;"Closing newsockfd: "&lt;&lt;name&lt;&lt;endl;
              close(newsockfd);
              newsockfd = 0;
          }
  
          break;
      default:
          cout&lt;&lt;"Preparing to sleep: "&lt;&lt;name&lt;&lt;endl;
          sleep(1);
          cout&lt;&lt;"Woke up from sleep"&lt;&lt;name&lt;&lt;endl;
          break;
      }
  
      return rv;
    }
  }
  
  //server.cpp
  int pid = jFork::j_fork(name);
  </code></pre>