title: C++ shared library undefined reference to `FooClass::SayHello()'
body: <p>I'm making a C++ Shared Library and when I compile a main exe that uses the library the compiler gives me:</p>
  
  <pre><code>main.cpp:(.text+0x21): undefined reference to `FooClass::SayHello()'
  collect2: ld returned 1 exit status
  </code></pre>
  
  <p>Library code:</p>
  
  <p>fooclass.h</p>
  
  <pre class="lang-c prettyprint-override"><code>#ifndef __FOOCLASS_H__
  #define __FOOCLASS_H__
  
  class FooClass 
  {
      public:
          char* SayHello();
  };
  
  #endif //__FOOCLASS_H__
  </code></pre>
  
  <p>fooclass.cpp</p>
  
  <pre class="lang-c prettyprint-override"><code>#include "fooclass.h"
  
  char* FooClass::SayHello() 
  {
      return "Hello Im a Linux Shared Library";
  }
  </code></pre>
  
  <p>Compiling with:</p>
  
  <pre><code>g++ -shared -fPIC fooclass.cpp -o libfoo.so
  </code></pre>
  
  <p>Main: 
  main.cpp</p>
  
  <pre class="lang-c prettyprint-override"><code>#include "fooclass.h"
  #include &lt;iostream&gt;
  
  using namespace std;
  
  int main(int argc, char const *argv[])
  {
      FooClass * fooClass = new FooClass();
  
      cout&lt;&lt; fooClass-&gt;SayHello() &lt;&lt; endl;
  
      return 0;
  }
  </code></pre>
  
  <p>Compiling with: </p>
  
  <pre><code>g++ -I. -L. -lfoo main.cpp -o main
  </code></pre>
  
  <p>The machine is an Ubuntu Linux 12</p>
  
  <p>Thanks!</p>