Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compiling one shared library on Linux to target all distributions

We want to create one shared library (.so) to target all distributions, including old ones. The code is written in C++ and uses C++11 features, so the compiler must be at least gcc 4.7. We noticed that if we compile our code on a Linux machine with gcc 4.7.2 installed (e.g., Ubuntu 12.10) then the .so produced has “version 1 (GNU/Linux)” while on older os (e.g., CentOS 5.6) the version is “version 1 (SYSV)” – and libraries with the GNU/Linux newer version cannot be used on older os.

So we tried the approach of installing gcc 4.7 on the CentOS 5.6 machine, compile our code with this compiler and statically link with libstdc++ (-static-libstdc++) – this produced an .so that was usable on every linux we found.

And this worked fine for 32-bit. However, when we followed the same approach on a 64-bit os (CentOS) this failed with the error that the existing libstdc++.a we tried to link to was compiled without –fPIC.

So we tried to compile the gcc 4.7.2 sources with the “–with-pic” option, but we couldn’t link to the new libstdc++.a – the error is:

/opt/centos/devtoolset-1.1/root/usr/libexec/gcc/x86_64-CentOS-linux/4.7.2/ld: /usr/local/lib/libFoo.so: version node not found for symbol _ZNSs7_M_copyEPcPKcm@GLIBCXX_3.4 /opt/centos/devtoolset-1.1/root/usr/libexec/gcc/x86_64-CentOS-linux/4.7.2/ld: failed to set dynamic section sizes: Bad value collect2: error: ld returned 1 exit status

We googled up that compiling libstdc++ with –fPIC may be problematic, but why does it work for 32-bit and not for 64-bit os? Is there another suggested way to create one .so for all Linux distros?

like image 299
user3659253 Avatar asked Nov 10 '22 07:11

user3659253


1 Answers

I answered this at https://gcc.gnu.org/ml/libstdc++/2014-05/msg00107.html

This looks like https://gcc.gnu.org/bugzilla/show_bug.cgi?id=54482 so might be fixed in GCC 4.7.3 (but I'm not sure)

...

PIC is implemented differently for x86 and x86_64, because 64-bit mode has built-in support for it.

like image 200
Jonathan Wakely Avatar answered Nov 15 '22 04:11

Jonathan Wakely