I'm trying to understand how g++ selects which version of libstdc++ it links against and what it means when the "system" version of the library is different.
I'm using gcc/g++ 4.1.2, which according to the ABI Guidelines doc, includes libstdc++.so.6.0.8, and sure enough:
-rwxr-xr-x 1 root root 4397810 May 18 2007 /opt/gcc4.1.2/lib/libstdc++.so.6.0.8
Based on my understanding of the ABI forward-compatibility, I can build with g++ 4.1.2 and expect the code to run on a system with a later version of libstdc++ than 6.0.8, but not on one with an earlier version, because that will have an older version of the ABI.
On the same machine there is an older version of libstdc++ in /usr/lib:
-rwxr-xr-x 1 root root 804288 Jul 22 2005 /usr/lib/libstdc++.so.6.0.3
If I compile code using g++ 4.1.2 on this machine, then ldd it, I see the version of libstdc++ in /usr/lib referenced, which is 6.0.3:
# ldd test
.
libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x005b6000)
.
This is expected, as /usr/lib is checked first. And the application runs fine.
My question is: what's happened here?
Has g++ 4.1.2 linked against the version of libstdc++.so that is part of that release (6.0.8)? If so, how come the executable can use the older version in /usr/lib at runtime, when that has an older ABI? Luck?
Or has g++ 4.1.2 picked up the /usr/lib version of libstdc++ (6.0.3) at link time and used that, because it resolves library paths in the same way as executables do at runtime? Can g++ do that, even if the libstdc++ is not its "own" version? What is the purpose of the libstdc++ version in g++4.1.2 (6.0.8)? Has it been used at all in this process?
Any insights appreciated.
GCC selects all libraries according to a directory search list. You can see it like this:
gcc -print-search-dirs
The list usually prefers libraries specific to the compiler version, if there is one.
However, the link-time choice may not be the same as the run-time choice.
If the linker command includes an -rpath
option (some toolchain vendors may include a non-standard one) then the dynamic linker will use that to find the right libraries at run-time. Otherwise the system will use its default library.
If the two libraries are not well matches then bad things may happen. The C library (usually glibc) has always been careful to maintain compatibility. The C++ library has not always had this luxury. It's been safer in recent years but many people still recommend not mixing and matching.
By default, gcc uses the libraries in the /usr/lib path.
1. gcc/g++ 4.1.2 is not linked to latest version of libstdc++.so.6.0.8.
2. g++ 4.1.2 picked up the /usr/lib version of libstdc++ (6.0.3) at link time.
It still uses the system default libstdc++.so.6.0.3 unless you explicitly set the library path.
For gcc/g++ 4.1.2, to use the latest version of libstdc++.so.6.0.8, you will have to export the library path before compiling.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/opt/gcc4.1.2/lib
Now when linking using gcc/g++ 4.1.2, libstdc++.so.6.0.8 will be used.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With