title: Problem Linking "static" Methods in C++
body: <p>I want to call a few "static" methods of a CPP class defined in a different file but I'm having linking problems. I created a test-case that recreates my problem and the code for it is below.</p>
  
  <p>(I'm completely new to C++, I come from a Java background and I'm a little familiar with C.)</p>
  
  <pre><code>// CppClass.cpp
  #include &lt;iostream&gt;
  #include &lt;pthread.h&gt;
  
  static pthread_t thread;
  static pthread_mutex_t mutex;
  static pthread_cond_t cond;
  static int shutdown;
  
  using namespace std;
  
  class CppClass
  {
  public:
          static void Start()
          {
                  cout &lt;&lt; "Testing start function." &lt;&lt; endl;
                  shutdown = 0;
                  pthread_attr_t attr;
                  pthread_attr_init(&amp;attr);
                  pthread_attr_setdetachstate(&amp;attr, PTHREAD_CREATE_JOINABLE);
                  pthread_mutex_init(&amp;mutex, NULL);
                  pthread_cond_init(&amp;cond, NULL);
  
                  pthread_create(&amp;thread, &amp;attr, run_thread, NULL);
          }
  
          static void Stop()
          {
                  pthread_mutex_lock(&amp;mutex);
                  shutdown = 1;
                  pthread_cond_broadcast(&amp;cond);
                  pthread_mutex_unlock(&amp;mutex);
          }
  
          static void Join()
          {
                  pthread_join(thread, NULL);
          }
  private:
          static void *run_thread(void *pthread_args)
          {
                  CppClass *obj = new CppClass();
                  pthread_mutex_lock(&amp;mutex);
                  while (shutdown == 0)
                  {
                          struct timespec ts;
                          ts.tv_sec = time(NULL) + 3;
                          pthread_cond_timedwait(&amp;cond, &amp;mutex, &amp;ts);
                          if (shutdown)
                          {
                                  break;
                          }
                          obj-&gt;display();
                  }
                  pthread_mutex_unlock(&amp;mutex);
                  pthread_mutex_destroy(&amp;mutex);
                  pthread_cond_destroy(&amp;cond);
                  pthread_exit(NULL);
                  return NULL;
          }
  
          void display()
          {
                  cout &lt;&lt; " Inside display() " &lt;&lt; endl;
          }
  };
  
  // main.cpp
  #include &lt;iostream&gt;
  /* 
   * If I remove the comment below and delete the
   * the class declaration part, it works.
   */
  // #include "CppClass.cpp"
  using namespace std;
  
  class CppClass
  {
  public:
          static void Start();
          static void Stop();
          static void Join();
  };
  
  int main()
  {
          CppClass::Start();
          while (1)
          {
                  int quit;
                  cout &lt;&lt; "Do you want to end?: (0 = stay, 1 = quit) ";
                  cin &gt;&gt; quit;
                  cout &lt;&lt; "Input: " &lt;&lt; quit &lt;&lt; endl;
                  if (quit)
                  {
                          CppClass::Stop();
                          cout &lt;&lt; "Joining CppClass..." &lt;&lt; endl;
                          CppClass::Join();
                          break;
                  }
          }
  }
  </code></pre>
  
  <p>When I tried to compile, I get the following error:</p>
  
  <pre>
  $ g++ -o go main.cpp CppClass.cpp -l pthread
  /tmp/cclhBttM.o(.text+0x119): In function `main':
  : undefined reference to `CppClass::Start()'
  /tmp/cclhBttM.o(.text+0x182): In function `main':
  : undefined reference to `CppClass::Stop()'
  /tmp/cclhBttM.o(.text+0x1ad): In function `main':
  : undefined reference to `CppClass::Join()'
  collect2: ld returned 1 exit status
  </pre>
  
  <p>But if I remove the class declaration in main.cpp and replace it with #include "CppClass.cpp", it works fine. Basically, I want to put these declarations in a separate .h file and use it. Am I missing something?</p>
  
  <p>Thanks for the help.</p>