title: How to link against debug versions of libc and libstdc++ in GCC?
body: <p>I am aware of <a href="https://stackoverflow.com/questions/3162513/how-do-i-tell-gcc-or-ld-to-link-against-debug-versions-of-the-standard-c-and-c">this question</a>, but it does not appear to work for me.</p>
  
  <p>For the setup, take a simple C++ program, <code>hw.cpp</code>, given by: <code>int main() { }</code></p>
  
  <p>Upon compiling with <code>g++ -o hw hw.cpp -O0 -g</code> on Linux, running <code>ldd ./hw</code> gives:</p>
  
  <pre><code>    linux-gate.so.1 =&gt;  (0x003e5000)
      libstdc++.so.6 =&gt; /usr/local/lib/libstdc++.so.6 (0x007c5000)
      libm.so.6 =&gt; /lib/tls/i686/cmov/libm.so.6 (0x006a4000)
      libgcc_s.so.1 =&gt; /usr/local/lib/libgcc_s.so.1 (0x00a40000)
      libc.so.6 =&gt; /lib/tls/i686/cmov/libc.so.6 (0x00a93000)
      /lib/ld-linux.so.2 (0x00a0f000)
  </code></pre>
  
  <p>Now I also appear to have debug libraries in <code>/usr/lib/debug/lib/tls/i686/cmov/</code>, which I imagine are the corresponding debug builds of the system libraries.</p>
  
  <p><strong>Question:</strong> How do I compile my program so that it is linked against the debug builds of the standard C and/or C++ libraries, <code>libc</code>/<code>libm</code>/<code>libstdc++</code>, shared or static? For the shared build, I want the output of <code>ldd ./hw</code> to point to the debug directory.</p>
  
  <p><sub>(Background: One of the shared libraries that's used by my project is reported as leaking ("still reachable") by Valgrind, but the origins are not in the shared library itself, but in dlopen-type code (<a href="http://pastebin.com/51mdUyk6" rel="nofollow noreferrer">see here</a>). So I figured that if I can step through the <code>_Start()</code> invocation in the CRT I might be able to trace the culprit.)</sub></p>
  
  <p><strong>Update/Correction:</strong> I think I was just very, very stupid - the debug libraries have probably always been linked as desired all along. I was confused by the debugger not showing anything while stepping, which is because I don't have the <em>source code</em> for the libraries.</p>
  
  <p><strong>Update II:</strong> OK, belay the previous update. I have the library sources now, but while it is true that the standard library ships with debug <em>symbols</em>, I don't appear to have a separate debug <em>build</em>. Is such a build available, and how would I link against it?</p>