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:
I rebuild my shared library which do not depends on specified GLIBC version.
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?
I found a link flag: -static-libgcc
Does that help if I build my shared library with this option? Any drawback?
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.