Linking using g++ fails searching for -lstdc++
Asked Answered
F

4

31

I'm trying to use someone else's Makefile to complile a very simple c++ library. The makefile is as follows:

JNIFLAGS=-O2 -pthread -I/usr/lib/jvm/java-6-sun/include -I/usr/lib/jvm/java-6-sun/include/linux

all:
    rm -f ../dist/libUtils.so
    g++ $(JNIFLAGS) -c -m32 -o com_markets_utils_dates_NativeTime.o com_markets_utils_dates_NativeTime.cpp
    g++ $(JNIFLAGS) -c -m32 -o DateUtil.o DateUtil.cpp
    g++ -pthread -m32 -shared -fPIC -o ../dist/libUtils.so DateUtil.cpp
    g++ -pthread -m32 -shared -fPIC -o ../dist/libNativeTime.so DateUtil.o com_markets_utils_dates_NativeTime.o

This compiles fine, but the linker complains:

...
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.a when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.so when searching for -lstdc++
/usr/bin/ld: skipping incompatible /usr/lib/gcc/x86_64-linux-gnu/4.4.1/libstdc++.a when searching for -lstdc++
/usr/bin/ld: cannot find -lstdc++
collect2: ld returned 1 exit status
make: *** [all] Error 1

FYI, I am on Ubuntu 9.10 64bit.

Frontpage answered 18/1, 2010 at 12:49 Comment(2)
Which version of g++ are you using - do 'g++ --version' to find out.Braque
$ g++ --version g++ (Ubuntu 4.4.1-4ubuntu8) 4.4.1Frontpage
S
90

Posting for future reference, a solution I found was to install g++-multilib. I had the same incompatible problem relating to -lstdc++ on g++ version 4.6.1

On further probing: g++-multilib is a dummy package which installed g++4.6-multilib which in turn installed the appropriate libstdc++.so under the /usr/lib/gcc/x86_64-linux-gnu/4.6/32 folder.

Southwesterly answered 10/12, 2011 at 14:3 Comment(6)
Honestly! This deserves more than a single vote-up. Solved my problem, for sure.Waybill
This approach is also recommended by bugs.launchpad.net/ubuntu/+source/gcc-4.6/+bug/973240Asquint
This resolved my issue in building a 32bit version of NodeJS on Ubuntu 12.04.1 LTSAurelio
For OpenSuse users: sudo zypper install gcc-32bit g++-32bitChildlike
@Raunaq: Thanks! Needed this to compile Riak. After your further probing, would you say there is a smaller dependency to install to fix the problem? No complaints with this though, it worked for me :-)Bollay
Very old post but still helpful. Package name for Fedora is dnf install gcc-c++.Everard
F
19

Answering my own question:

Ths solution seems to be a bit of a hack, you need to create a symlink for the 32 bit version of the library (after installing the packages mentioned in @nos's answer):

$ sudo ln -s /usr/lib32/libstdc++.so.6 /usr/lib32/libstdc++.so

Once you've done this, the linker will automagically find the correct library to use.

Frontpage answered 18/1, 2010 at 13:47 Comment(2)
Probably related to this bug: bugs.launchpad.net/ubuntu/+source/ia32-libs/+bug/360870 (new link)Borehole
what if you don't have sudo?Grouse
B
5

It seems you're compiling a 32 bit library on a 64 bit machine, however a 32 bit version of libstdc++ is not present.

Try apt-get install ia32-libs libc6-i386 libc6-dev-i386 lib32gcc1 lib32stdc++6

(btw. you're producing a .so , you should specify -fPIC when compiling your .cpp files as well)

Borehole answered 18/1, 2010 at 12:57 Comment(1)
I had thought of that, however they are all installed - btw thanks for the -fPIC reminder, I should have caught that...Frontpage
I
0

Installing multilib should help, but pay attention that you need to specify the version number corresponding to GCC version like:

sudo apt-get install gcc-9-multilib
Ils answered 9/11, 2023 at 17:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.