1

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?

Bear
  • 1,367
  • 2
  • 13
  • 19
  • You're thinking of **glibc**, not **glib** - see [this question](http://stackoverflow.com/questions/8823267/linking-against-older-symbol-version-in-a-so-file). – ptomato Jul 17 '12 at 14:39
  • It's not the g++ version, it's the libc version. I only have `memcpy@GLIBC_2.0` on Ubuntu 10.04.4 LTS, so I would get the undefined reference too. Link against the oldest glibc you can. – n. m. could be an AI Jul 17 '12 at 15:23

2 Answers2

4

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.

Jonathan Wakely
  • 166,810
  • 27
  • 341
  • 521
3

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.

Bear
  • 1,367
  • 2
  • 13
  • 19