title: How to link C++ object files with ld
body: <p>I'm trying to link the output of C++ using ld and not g++. I'm only doing this to learn how to do it, not for practical purposes, so please don't suggest just to do it with g++.</p>
  
  <p>Looking at <a href="https://stackoverflow.com/questions/6704780/using-only-g-works-but-not-g-c-and-ld">this question</a>, the person gets the same error when they run the ld command:</p>
  
  <pre><code>$ ld test.o -o test.out
  ld: warning: cannot find entry symbol _start; defaulting to 00000000004000e8
  test.o: In function `main':
  test.cpp:(.text+0x1c): undefined reference to `strcasecmp'
  test.cpp:(.text+0x23): undefined reference to `std::cout'
  test.cpp:(.text+0x28): undefined reference to `std::ostream::operator&lt;&lt;(int)'
  test.cpp:(.text+0x2d): undefined reference to `std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp; std::endl&lt;char, std::char_traits&lt;char&gt; &gt;(std::basic_ostream&lt;char, std::char_traits&lt;char&gt; &gt;&amp;)'
  test.cpp:(.text+0x35): undefined reference to `std::ostream::operator&lt;&lt;(std::ostream&amp; (*)(std::ostream&amp;))'
  test.o: In function `__static_initialization_and_destruction_0(int, int)':
  test.cpp:(.text+0x75): undefined reference to `std::ios_base::Init::Init()'
  test.cpp:(.text+0x7a): undefined reference to `__dso_handle'
  test.cpp:(.text+0x84): undefined reference to `std::ios_base::Init::~Init()'
  test.cpp:(.text+0x89): undefined reference to `__cxa_atexit'
  ld: test.out: hidden symbol `__dso_handle' isn't defined
  ld: final link failed: Bad value
  </code></pre>
  
  <p>The answers in the linked post suggest that adding the C++ library as a linker argument will fix the problem, so I tried</p>
  
  <pre><code>ld test.o -o test.out -llibstd++
  </code></pre>
  
  <p>which is what they suggested, and I also tried a lot of other library names like libstdc++ or stdc++. But I'll always get an error that looks like</p>
  
  <pre><code>ld: cannot find -llibstd++
  </code></pre>
  
  <p>What am I doing wrong and how can I link my object files using ld?</p>