1

I have a non-system install of g++ and am trying to compile a program using

/MYINSTALLDIR/g++ -L/MYINSTALLLIBDIR -Wl,-rpath,MYINSTALLLIBDIR main.cpp -o tester

but when I then run

ldd ./tester

I get that the libstdc++ and other linked gcc/g++ libraries are the system installs and not the those in the specified rpath location.

I've read through linking g++ 4.8 to libstdc++, and the only other option I see to try is changing the g++ spec file. Since it is owned by root this would be problematic for me.

Any suggestions?

(Note, I've checked that LD_LIBRARY_PATH="" too. I would prefer not to change LD_LIBRARY_PATH.)

EDIT: I tried looking at the NEEDED and RPATH fields in the binary and they are what I would expect. For example, NEEDED includes "libstdc++" and RPATH is simply the value of MYINSTALLLIBDIR.

I then set LD_DEBUG="libs" and re-ran ldd on tester. It looks like ldd is seeing the correct library path before the system version, but not choosing it. The output related to libstdc++ is below:

    13337:  find library=libstdc++.so.6 [0]; searching
    13337:   search path=/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/tls/x86_64:/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/tls:/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/x86_64:/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2 (RPATH from file ./tester)
    13337:    trying file=/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/tls/x86_64/libstdc++.so.6
    13337:    trying file=/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/tls/libstdc++.so.6
    13337:    trying file=/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/x86_64/libstdc++.so.6
    13337:    trying file=/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/libstdc++.so.6
    13336:       13337: find library=libc.so.6 search cache= [/etc/ld.so.cache0]; searching
    13336:   search cache=/etc/ld.so.cache
    13337:    trying file=/usr/lib64/libstdc++.so.6
    13336:       13337:   trying file=

I believe the correct libstdc++ should be "libstdc++.so" in the /opt/rh/.../4.8.2 folder. There is no "libstdc++.so.6" there.

EDIT: Marc Glisse is correct. It turns out the new gcc libstdc++.so just selects the previously installed system version. So this appears to be the correct behavior.

Community
  • 1
  • 1
Sam
  • 21
  • 4
  • 1
    It should work, most likely you mistyped something somewhere. You can use `objdump` to check the NEEDED and RPATH/RUNPATH of tester. – Marc Glisse May 23 '14 at 08:20
  • Thanks, I tried what you suggested. Everything was type correct. I've updated the original post -- looks like the binary is correct but ld is just not choosing the correct library. – Sam May 23 '14 at 16:10
  • NEEDED says `libstdc++.so.6` and that's good, so you need to point your rpath to a place where it can find that. The name that ends in `.so` is only for compile-time. The `.so` is likely a symlink to the `.so.6` so you can look at it to find the right location, maybe `.../usr/lib64`. (PS: you should probably get rid of the `-L` flag, it should be useless) – Marc Glisse May 23 '14 at 16:56
  • Yeah, I figured -L wasn't needed, but wasn't 100% sure. It appears you are right, the libstdc++.so in the new gcc directory does indeed just use "INPUT( /usr/lib64/libstdc++.so.6...". So I guess this package doesn't actually upgrade libstdc++. Thank you for all the help! – Sam May 23 '14 at 17:09
  • Are you sure you don't have a `/opt/rh/devtoolset-2/root/usr/lib64/libstdc++.so.6`? If so, you can add that directory to rpath and edit the .so file to give it the "right" location. – Marc Glisse May 23 '14 at 17:28
  • All the files containing "libstdc++" in their name are under `/opt/rh/devtoolset-2/root/usr/lib/gcc/x86_64-redhat-linux/4.8.2/`, and I only have `libstdc++.a`, `libstdc++.so` (which just loads the system libstdc++), and `libstdc++_nonshared.a`. The only other place with any files matching `libstdc++*` in the name is in the `/usr/share/gdb/` subdirectory of the devtools-2 install. – Sam May 24 '14 at 18:14

1 Answers1

0

You're using GCC from the Red Hat Developer Toolset which uses a special linkage model to ensure that the binaries it compiles do not depend on a newer libstdc++.so, precisely so that you don't have to mess about setting RPATH or LD_LIBRARY_PATH or anything like that. It Just Works™. So seeing your binaries depend on the system libstdc++.so.6 is exactly what is supposed to happen.

I've closed this answer as a duplicate of another similar one where I already gave more details on how linking to libstdc++ works using devtoolset.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521