How can I build a shared library that will be used for different g++ version?

I want to compile a shared library to other developers, but without shipping source code. The .so file I built against g++ 4.6, but they used g++ 4.4. When they linked with my .so file, g++ reported: undefined reference to 'memcpy@GLIBC_2.14'.

I think there are two possible solutions:

  1. I rebuild my shared library which do not depends on specified GLIBC version.

  2. Tell them a simple way to link with my shared library with some compiler options to make it work.

I have no idea in both methods, how can I achieve these solutions or one of them?

Thanks.

Edit:

I found a link flag: -static-libgcc

Does that help if I build my shared library with this option? Any drawback?

Answers


That symbol is defined by glibc (the C library) not by GCC, so -static-libgcc won't help in the slightest because it only affects libgcc not libc.

You can force the compiler and linker to use the old memcpy symbol like this (I'm assuming you're compiling on x86_64, so the older glibc they're using probably only has memcpy@GLIBC_2.2.5, if you're on x86 then you might want memcpy@GLIBC_2.0 instead):

__asm__(".symver memcpy,memcpy@GLIBC_2.2.5");
memcpy(tgt, src, n);

The .symver directive causes any references to memcpy to bind to the symbol memcpy@GLIBC_2.2.5 instead of the default, which is memcpy@GLIBC_2.14 in your case.


Finally, I add these options to make it without depends on any shared libraries.

-static-libgcc -static-libstdc++ -nodefaultlibs -shared -fPIC

The ldd reports 'static linked', and no any required .so files.

This might be useful for anyone who want to ship portable shared library.


Need Your Help

Introspect function to determine which argument unpacking (positional or keyword) was used

python python-3.x introspection argument-passing kwargs

I was searching for a way to identify whether some argument is used for unpacking and I have found this:

What's the correct alternative to static method inheritance?

c# .net inheritance static

I understand that static method inheritance is not supported in C#. I have also read a number of discussions (including here) in which developers claim a need for this functionality, to which the t...