Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to link against debug versions of libc and libstdc++ in GCC?

Tags:

c++

c

gcc

debugging

I am aware of this question, but it does not appear to work for me.

For the setup, take a simple C++ program, hw.cpp, given by: int main() { }

Upon compiling with g++ -o hw hw.cpp -O0 -g on Linux, running ldd ./hw gives:

    linux-gate.so.1 =>  (0x003e5000)
    libstdc++.so.6 => /usr/local/lib/libstdc++.so.6 (0x007c5000)
    libm.so.6 => /lib/tls/i686/cmov/libm.so.6 (0x006a4000)
    libgcc_s.so.1 => /usr/local/lib/libgcc_s.so.1 (0x00a40000)
    libc.so.6 => /lib/tls/i686/cmov/libc.so.6 (0x00a93000)
    /lib/ld-linux.so.2 (0x00a0f000)

Now I also appear to have debug libraries in /usr/lib/debug/lib/tls/i686/cmov/, which I imagine are the corresponding debug builds of the system libraries.

Question: How do I compile my program so that it is linked against the debug builds of the standard C and/or C++ libraries, libc/libm/libstdc++, shared or static? For the shared build, I want the output of ldd ./hw to point to the debug directory.

(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 (see here). So I figured that if I can step through the _Start() invocation in the CRT I might be able to trace the culprit.)

Update/Correction: 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 source code for the libraries.

Update II: OK, belay the previous update. I have the library sources now, but while it is true that the standard library ships with debug symbols, I don't appear to have a separate debug build. Is such a build available, and how would I link against it?

like image 214
Kerrek SB Avatar asked Jan 09 '12 22:01

Kerrek SB


1 Answers

On many Linux installations the debug libraries do not contain real code; they only contain the debug info. The two are separated so that you can choose not to install them if you don't need them and you are short of disk space, but the debug libraries are no good on their own.

GDB is normally preconfigured to find the debug libraries when you need them.

Of course, your system maybe different. You don't say what it is.

like image 188
ams Avatar answered Oct 02 '22 02:10

ams