How to link with libstdc++_pic.a with GCC?

2019-09-07 11:40发布

问题:

I have a libstdc++_pic.a on my system (Ubuntu 12.04, gcc 4.6.3), presumably this is the version of libstdc++ created with -fPIC. I also have a libstdc++.a.

I see the following error when I try to statically link libstdc++ with a shared library via the -static-libstdc++ link flag:

:-1: error: /usr/lib/gcc/x86_64-linux-gnu/4.6/libstdc++.a(functexcept.o): relocation R_X86_64_32 against `std::bad_exception::~bad_exception()' can not be used when making a shared object; recompile with -fPIC

So I'm assuming it is using the non pic version, as I would expect based on the default naming. How can I instruct the linker to use the libstdc++_pic.a file?

Update 1 The _pic version is in the 32 bit gcc subfolder at /usr/lib/gcc/x86_64-linux-gnu/4.6/32, and I don't see a _pic version in the parent folder where the 64 bit versions are located. It appears that my version of gcc was not compiled with a 64 bit PIC static libstdc++.a.

Update 2 I was able to install the 64 bit version of libstdc++_pic.a by checking my gcc version and then installing the package:

gcc --version gcc (Ubuntu/Linaro 4.6.3-1ubuntu5) 4.6.3 sudo apt-get install libstdc++6-4.6-pic

And now I see it in /usr/lib/gcc/x86_64-linux-gnu/4.6/. Still need to figure out how to tell linker to use it.

回答1:

I seem to have fixed this by passing the -nostdlib linker option, and then explicitly linking libstdc++_pic.

However, now I am getting the following error:

version node not found for symbol std::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >::_M_move(wchar_t*, wchar_t const*, unsigned long)@@GLIBCXX_3.4.5

After researching it appears there may have been a bug for static libstdc++ 64bit with pic, which is now addressed in newer versions of gcc (4.7.3). This other question has more information:

Compiling one shared library on Linux to target all distributions