Search code examples
androidandroid-ndkcross-compilinggmp

android GMP cross compilation


I'm on debian/amd64 and I want to cross-compile GMP for android 2.2 using NDK-7b. I took the source code from [gmplib](hg clone http://gmplib.org:8000/gmp-5.0 gmp). I configured it with:

./configure --enable-shared --host=arm-linux-androideabi --prefix=/home/fabien/android/spica/ndk-standalone-8 CFLAGS="-v -march=armv5te -mtune=xscale -msoft-float -Wl,-rpath,lib/ -DANDROID -ffunction-sections -funwind-tables -fstack-protector -funswitch-loops -finline-limit=300 -Wall -O3 -nodefaultlibs -fPIC -shared -Wl,--no-allow-shlib-undefined" PKG_CONFIG_PATH="/home/fabien/android/spica/ndk-standalone-8/lib/pkgconfig" LDFLAGS="-Wl,-rpath-link -Wl,/home/fabien/android/spica/ndk-standalone-8/lib -L/home/fabien/android/spica/ndk-standalone-8/lib"

I modified the file config.h by setting:

/* Define to 1 if you have the `obstack_vprintf' function. */
#ifndef ANDROID
#define HAVE_OBSTACK_VPRINTF 1
#endif
/* Define to 1 if you have the `localeconv' function. */
#ifndef ANDROID
#define HAVE_LOCALECONV 1
#endif
/* Define to 1 if you have the `vsnprintf' function and it works properly. */
#ifndef ANDROID
#define HAVE_VSNPRINTF 1
#endif

I updated the SUBDIRS parameter as follows in Makefile:

 SUBDIRS = tests mpn mpz mpq mpf printf scanf cxx mpbsd demos tune

It seems to compile when I run make:

libtool: link: (cd ".libs" && rm -f "libgmp.so" && ln -s "libgmp.so.10.0.5" "libgmp.so")
libtool: link: ( cd ".libs" && rm -f "libgmp.la" && ln -s "../libgmp.la" "libgmp.la" )

But when I run "make check", the linker seems lost:

/../../../../arm-linux-androideabi/bin/ld: warning: ld-linux.so.3, needed by /home/fabien/android/spica/sources/gmp/.libs/libgmp.so, not found (try using -rpath or -rpath-link)
t-bswap.o:(.ARM.exidx.text.main+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'
./.libs/libtests.a(misc.o):(.ARM.exidx.text.align_pointer+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'
.
.
.
refmpn.c:(.text.refmpn_get_str+0xb8): undefined reference to `__aeabi_uidiv'
refmpn.c:(.text.refmpn_get_str+0x238): undefined reference to `__aeabi_ui2d'
refmpn.c:(.text.refmpn_get_str+0x250): undefined reference to `__aeabi_dmul'
refmpn.c:(.text.refmpn_get_str+0x254): undefined reference to `__aeabi_d2uiz'
./.libs/libtests.a(refmpn.o):(.ARM.exidx.text.refmpn_get_str+0x0): undefined reference to `__aeabi_unwind_cpp_pr0'
/home/fabien/android/spica/sources/gmp/.libs/libgmp.so: undefined reference to `abort@GLIBC_2.4'
/home/fabien/android/spica/sources/gmp/.libs/libgmp.so: undefined reference to `puts@GLIBC_2.4'

Any hints ?


Solution

  • This error is due to the fact that the file that contains these helper functions (these are GCC helper functions) is not being included when the linker creates the target. To fix this, please add the libgcc.a (which contains the GCC helper function definitions) into the linker flags.

    As for the location of libgcc.a, assuming gcc version arm-linux-androideabi-4.4.3, it will be $NDK_ROOT/toolchains/arm-linux-androideabi-4.4.3/prebuilt/linux-x86/lib/gcc/arm-linux-androideabi/4.4.3/libgcc.a

    This will NOT fix the following errors(which seem to be related to the GNU libc being missing):

    .libs/libgmp.so: undefined reference to `abort@GLIBC_2.4'
    .libs/libgmp.so: undefined reference to `puts@GLIBC_2.4'
    

    The above 2 errors will always show up as android uses the Bionic libc, and not the GNU libc

    Note: This method will fix similar issues on all systems, not just Android.