foob
foob

Reputation: 1317

How can I change the libraries that gcc uses when linking with /bin/ld?

I'm trying to use a version of gcc relocated from another system to compile something. I know that there are issues with this but if I can get it to work it will make my life much easier. I've added the /opt/transfer/lib path to $LIBRARY_PATH and gcc appears to see this (from gcc -print-search-dirs). I've also added /opt/transfer/lib to /etc/ld.so.conf though I would greatly prefer a solution where this wasn't necessary. The problem comes when I execute:

g++ -g -m32 chopper.o -Llibs -lRTS -o Chopper /usr/bin/ld: skipping incompatible /usr/lib/libstdc++.so.6 when searching for /usr/lib/libstdc++.so.6 /usr/bin/ld: cannot find /usr/lib/libstdc++.so.6 /usr/bin/ld: skipping incompatible /lib/libc.so.6 when searching for /lib/libc.so.6 /usr/bin/ld: cannot find /lib/libc.so.6 /usr/bin/ld: skipping incompatible /usr/lib/libc_nonshared.a when searching for /usr/lib/libc_nonshared.a /usr/bin/ld: cannot find /usr/lib/libc_nonshared.a collect2: ld returned 1 exit status

ld appears to be searching in /usr/lib/ and then giving up. There is a /opt/transfer/lib/libstdc++.so.6 file that should be compatible but is not being linked to. If I run "gcc -print-file-name=libstdc++.so.6" it finds the version that I want it to be using. Is there any way to force ld to look in another dir instead of /usr/lib in this case?

Edit

I tried using the --sysroot option as nneonneo suggested but I'm not totally sure if it worked. This options was not available in my version of ld so I recompiled using --with-sysroot=/opt/transfer and --with-build-sysroot=/opt/transfer. Then when I run I get this as an example output:

/opt/transfer/bin/ld: skipping incompatible /usr/lib/libstdc++.so.6 when searching for /usr/lib/libstdc++.so.6 /opt/transfer/bin/ld: cannot find /usr/lib/libstdc++.so.6 ld returned 1 exit status

From this output it isn't 100% clear to me that it's searching the /opt/transfer/lib and /opt/transfer/usr/lib directories because I'm fairly certain that the correct libstdc++.so.6 file should be there. If I remove the libraries from /opt/transfer then it starts complaining about other libraries missing so it's clearly at least partially working, but all of these other libraries say only that they cannot be found while for libstdc++ it still says it's skipping an incompatible library. Is it possible that libstdc++ is being treated as a special case? Could there be another option that addresses this?

Edit 2

I just moved the system's /usr/libstdc++.so.6 to a backup location and after doing this ld would no longer say it was skipping an incompatible version. This definitely means that --sysroot isn't doing what I was expecting here. Any more information that anybody has would be very much appreciated.

Upvotes: 1

Views: 5343

Answers (1)

nneonneo
nneonneo

Reputation: 179392

Try using the --sysroot option (--sysroot /opt/transfer). --sysroot is the recommended way to make GCC (and related tools) operate from a non-standard directory, and so it's ideal for your relocated GCC.

You will have to reorganize your /opt/transfer directory to more closely match the layout of the source distribution (i.e. put the libraries under /opt/transfer/usr/lib). You can symlink whatever GCC needs from your regular directories into /opt/transfer as necessary.

Upvotes: 3

Related Questions