简体   繁体   中英

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 (eg, Ubuntu 12.10) then the .so produced has “version 1 (GNU/Linux)” while on older os (eg, 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?

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.

Linking statically to libc/libstdc++ should not be done, even if it works! It is quite dangerous, because a lot of security aspects requires updates to libc. If statically linked in no update can fill the hole.

I can't believe that a 'generic' libc exists which works on all linux platforms. The libc is the interface to the installed system which is big pool of differences. How should one lib fits all?

While linking you could try

-static-libstdc++ -static-libgcc

as options to ld. Maybe this helps. But I would never do that!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM