Compiler libstdc ++ version or system version

I am trying to understand how g ++ chooses which version of libstdC ++ it links to, and what it means when the "system" version of the library is different.

I am using gcc / g ++ 4.1.2, which according to the ABI manual contains libstdC ++. so.6.0.8, and fairly confidently:

-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 advanced ABI 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 one with an earlier version, because he will have an older version of ABI.

On the same computer, an older version of libstdc ++ is installed in / usr / lib:

 -rwxr-xr-x 1 root root 804288 Jul 22 2005 /usr/lib/libstdc++.so.6.0.3 

If I compile the code using g ++ 4.1.2 on this computer, then ldd, I see the version of libstdC ++ in / usr / lib that is referenced, which is 6.0.3:

 # ldd test . libstdc++.so.6 => /usr/lib/libstdc++.so.6 (0x005b6000) . 

It is expected that / usr / lib is checked first. And the application is working fine.

My question is: what happened here?

Is g ++ 4.1.2 related to libstdC ++ version, so is it part of this version (6.0.8)? If so, how is it that the executable can use the older version in / usr / lib at runtime when it has an older ABI? Luck?

Or did g ++ 4.1.2 choose the version of libstdC ++ (6.0.3) / usr / lib at link time and use it because it resolves library paths in the same way as executables at runtime? Can g ++ do this even if libstdc ++ is not its "own" version? What is the purpose of libstdc ++ version in g ++ 4.1.2 (6.0.8)? Is it used at all in this process?

Any ideas appreciated.

+7
source share
2 answers

GCC selects all libraries according to the catalog search list. You can see it as follows:

 gcc -print-search-dirs 

The list usually prefers libraries specific to the compiler version, if any.

However, the timing of the link may not be the same as the timing of the run.

If the linker command includes the -rpath (some tool providers may include non-standard), then the dynamic linker will use this to find the libraries at runtime. Otherwise, the system will use its default library.

If two libraries do not fit well, then bad things can happen. The C library (usually glibc) has always tried to maintain compatibility. The C ++ library did not always have this luxury. This has been safer in recent years, but many people still do not recommend mixing and matching.

+9
source

By default, gcc uses libraries in the / usr / lib path.
<1.> gcc / g ++ 4.1.2 is not tied to the latest version of libstdC ++. so.6.0.8.
2. g ++ 4.1.2 chose the version of libstdC ++ (6.0.3) / usr / lib during the connection.

It still uses the system default libstdc ++. so.6.0.3 if you have not explicitly set the library path.

For gcc / g ++ 4.1.2, to use the latest version of libstdc ++. so.6.0.8, you will need 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 ++ will be used. so.6.0.8.

+1
source

All Articles