2023-06-01

Cross-compiling zlib for Android

I am trying to cross-compile zlib for Android (the library will ultimately be used to provide an OpenSSH binary).

I got to the point where I was able to cross-compile the library for the target architecture (armel) on Linux:

cd ~/src
git clone git@github.com:madler/zlib.git
cd zlib
# if needed, git checkout v1.2.13 (or whatever the current version is)
mkdir -p build/armel
CC=arm-linux-gnueabi-gcc ./configure --prefix=build/armel
CC=arm-linux-gnueabi-gcc make 
CC=arm-linux-gnueabi-gcc make install

file build/armel/lib/libz.so.1.2.13 tells me that the file is a binary for the ARM architecture – so far, so good.

I have in the past been able to run Linux binaries on Android – this seems to work if they target the correct platform, don’t have any unsatisfied dependencies and can deal with rather ancient kernels, but I’m not sure if that is the preferred way to do it.

Therefore I tried to build the lib with Android NDK:

export ANDROID_NDK_ROOT=$ANDROID_HOME/ndk/18.1.5063045
export PATH=$PATH:$ANDROID_NDK_ROOT/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin
cd ~/src
git clone git@github.com:madler/zlib.git
cd zlib
# if needed, git checkout v1.2.13 (or whatever the current version is)
mkdir -p build/armel
CC=arm-linux-androideabi-gcc ./configure --prefix=build/android-arm
CC=arm-linux-androideabi-gcc make 
CC=arm-linux-androideabi-gcc make install

This fails with the following error:

/usr/lib/gcc-cross/arm-linux-gnueabi/11/../../../../arm-linux-gnueabi/bin/ld: cannot find crtbegin_dynamic.o: No such file or directory
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [Makefile:285: example] Error 1

Questions:

  • Can I target arm-linux-gnueabi and use the result on Android? Or is that likely to cause errors?
  • If not, what is the correct way to build zlib for Android?
  • If the correct way to build zlib for Android is the one I tried, what is causing the error and how can I fix it?


No comments:

Post a Comment