libtool links with 32-bit version of libstdc++.so on a 64-bit target platform when multiple version of gcc are installed

Nicolas picture Nicolas · Apr 3, 2013 · Viewed 7.3k times · Source

First, I'd like to let you know that it is my first post on StackOverflow, so I hope that I won't make a fool of myself by asking a very stupid question. I've been googling about this problem for a few days now, and I couldn't find any answer so far.

Here is the situation:

I'm working on a C++ project managed with autotools. The target platform is RHEL5 64bit with two versions of gcc installed:

  • gcc 4.1.2 installed in /usr
  • gcc 4.3.5 installed in /local/opt/gcc-4.3.5

When I'm building my project with the default gcc version (4.1.2), everything goes fine, but when I'm switching to gcc 4.3.5, I'm getting this error at link time:

/local/opt/gcc-4.3.5/lib/../lib/libstdc++.so: could not read symbols: File in wrong format
collect2: ld returned 1 exit status

It seems that libtool hardcoded the path to the 32-bit version of libstdc++.so in the command line, while it should have been the 64-bit version. More precisely, the libtool call that fails is:

/bin/sh ./libtool --tag=CXX --mode=link g++  -m64   -o libfoo.la -rpath /local/opt/foo/lib  src/foo/libfoo_la-bar1.lo src/foo/libfoo_la-bar2.lo

It is translated by libtool as:

g++ -shared -nostdlib
/usr/lib/../lib64/crti.o
/local/opt/gcc-4.3.5/lib/gcc/x86_64-unknown-linux-gnu/4.3.5/crtbeginS.o
src/foo/.libs/libfoo_la-bar1.o
src/foo/.libs/libfoo_la-bar2.o
-Wl,--rpath
-Wl,/local/opt/gcc-4.3.5/lib/../lib
-Wl,--rpath
-Wl,/local/opt/gcc-4.3.5/lib/../lib
-L/local/opt/gcc-4.3.5/lib/gcc/x86_64-unknown-linux-gnu/4.3.5
-L/local/opt/gcc-4.3.5/lib/gcc/x86_64-unknown-linux-gnu/4.3.5/../../../../lib64
-L/lib/../lib64
-L/usr/lib/../lib64
-L/local/opt/gcc-4.3.5/lib/gcc/x86_64-unknown-linux-gnu/4.3.5/../../..
/local/opt/gcc-4.3.5/lib/../lib/libstdc++.so
-lm -lc -lgcc_s
/local/opt/gcc-4.3.5/lib/gcc/x86_64-unknown-linux-gnu/4.3.5/crtendS.o
/usr/lib/../lib64/crtn.o
-m64 -Wl,-soname -Wl,libfoo.so.0 -o .libs/libfoo.so.0.0.0

I should precise that the method I use to switch from default gcc to gcc 4.3.5 is the following:

$ export PATH=/local/opt/gcc-4.3.5/bin:$PATH
$ export LD_LIBRARY_PATH=/local/opt/gcc-4.3.5/lib:/local/opt/gcc-4.3.5/lib64:$LD_LIBRARY_PATH
$ export GCC_HOME=/local/opt/gcc-4.3.5

I'm quite new to all these tools, so I suspect I'm doing something wrong. I would be very grateful if anyone could give me a clue as to how to fix this.

Cheers

Answer

Nicolas picture Nicolas · Dec 17, 2013

I've just found the answer to my own question: in addition to setting correctly LD_LIBRARY_PATH, I needed to pass the argument LDFLAGS=-L/local/opt/gcc-4.3.5/lib64 to the configure script. Thank you all for your inputs.