undefined reference to B::B & B::~B
<p>I keep getting complaint from the g++ compiler that the following code has problems.
After careful examination, I still cannot figure out why it cannot find the constructor and destructor of class B from embedMain.cpp.</p>

<p>Can someone give me a little hint?</p>

<p>Thank you</p>

<pre><code>// embedMain.cpp
#include "embed.h"

int main(void)
{
  B b("hello world");
  return 0;
}
</code></pre>

<p>,</p>

<pre><code>// embed.h
#ifndef EMBED_H
#define EMBED_H
#include &lt;string&gt;

class B
{
public:
  B(const std::string&amp; _name);
  ~B();
private:
  std::string name;
};
#endif
</code></pre>

<p>,</p>

<pre><code>// embed.cpp

#include &lt;iostream&gt;
#include &lt;string&gt;
#include "embed.h"

B::B(const std::string&amp; _name) : name(_name) {}

B::~B() {
  std::cout &lt;&lt; "This is B::~B()" &lt;&lt; std::endl;
}
</code></pre>

<p>,</p>

<pre><code>~/Documents/C++ $ g++ --version
g++ (Ubuntu/Linaro 4.5.2-8ubuntu4) 4.5.2

~/Documents/C++ $ g++ -o embedMain embedMain.cpp 
/tmp/ccdqT9tn.o: In function `main':
embedMain.cpp:(.text+0x42): undefined reference to `B::B(std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt; const&amp;)'
embedMain.cpp:(.text+0x6b): undefined reference to `B::~B()'
embedMain.cpp:(.text+0x93): undefined reference to `B::~B()'
collect2: ld returned 1 exit status
</code></pre>

<p>// Updated //</p>

<p>Based on comments from experts here, I have found the right way to link the embedMain.cpp with the embed library.</p>

<p>Here is the detail step:</p>

<pre><code>user@ubuntu:~/Documents/C++$ tree
.
├── embed.cpp
├── embed.h
├── embedMain.cpp

user@ubuntu:~/Documents/C++$ g++ -Wall -c embed.cpp
user@ubuntu:~/Documents/C++$ ar -cvq libembed.a embed.o
user@ubuntu:~/Documents/C++$ g++ -o embedMain embedMain.cpp -L/home/user/Documents/C++ -lembed
user@ubuntu:~/Documents/C++$ tree
.
├── embed.cpp
├── embed.h
├── embedMain
├── embedMain.cpp
├── embed.o
├── libembed.a
</code></pre>
