This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: custom-built gcc 4.6.0 on ubuntu 11.04 links wrong libstdc++


On 19 April 2011 19:44, Razno Razno wrote:
>
> hi and thank you for your answer.
>
> if i have a hello.C file with
>
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
> ??? cout << "Hello" << endl;
> }
>
> and compile it with
>
> g++ hello.C
>
> ldd shows that a.out is linked to system libstdc++.

That's not strictly correct.

Your program is linked with "libstdc++.so.6" i.e with any library that
matches that soname, because that's the soname of the library that gcc
found during linking.

When you run your program (or check its library dependencies with ldd)
the dynamic linker tries to resolve the dependency on "libstdc++.so.6"

In your case, the first library it finds on your system with that
soname is the system libstdc++, so that's what it shows.

The distinction is important.  The executable is not linked to the
system libstdc++, that's just the first one found at runtime.

> if i compile with
>
> g++ -Wl,-rpath -Wl,$HOME/gcc-4.6.0 hello.C
>
> in ldd i get correct libstdc++.

That causes the dynamic linker to look in $HOME/gcc-4.6.0 before it
looks in the system directories, so the first library it finds that
matches "libstdc++.so.6" is the one in that directory.

Again, your executable is linked to any library called
"libstdc++.so.6" and will use the first one it finds with that soname.

I'm not just pointing this out to be pedantic:  If you use certain
features that are only provided by recent versions of GCC then the
system libstdc++ might not provide them, so although the dynamic
linker will find the system libstdc++, it won't be able to use it.

> can i somehow configure (preferably while building gcc itself) that my g++ automatically gets these options?

It's possible to use a spec file so that the -Wl,-rpath option is
always used, but the simplest solution is just to use a wrapper script
to call g++.  You can create a script called g++ in your path which
runs $HOME/gcc-4.6.0/bin/g++ and automatically passes the -Wl,-rpath
option to it.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]