0

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?
user149408
  • 5,385
  • 4
  • 33
  • 69
  • Your NDK version seems a little bit old (I have 25.2.x installed). Does your NDK contain `crtbegin_dynamic.o`files? In my two NDK versions installed they are present for each Android API version. – Robert May 28 '23 at 14:36
  • @Robert what path would I find these files in? – user149408 May 28 '23 at 15:13
  • Just make a full search on the NDK and it's subfolders. – Robert May 28 '23 at 17:40

1 Answers1

0

This question provided some pointers, which eventually led me to a successful build:

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/android-arm build/android-arm64 build/android-x86 build/android-x86_64
CHOST="arm-linux-androideabi" CHOST2="armv7a-linux-androideabi17" CC="${CHOST2}-clang" CXX="${CHOST2}-clang++" RANLIB="${CHOST}-ranlib" LD="${CHOST}-ld" AR="${CHOST}-ar" ARFLAGS="cr" CHOST="${CHOST}" ./configure --prefix=build/android-arm
make
make install
make clean  # only if we’re going to build for multiple targets

CHOST specifies the target architecture:

  • arm-linux-androideabi (ARM)
  • aarch64-linux-android (64-bit ARM)
  • i686-linux-android (x86)
  • x86_64-linux-android (x86_64)

CHOST2 is similar, but with extra processor and platform API details, which are needed to select the correct clang compiler (API 17 for 32-bit platforms, 21 for 64-bit ones):

  • armv7a-linux-androideabi17
  • aarch64-linux-android21
  • i686-linux-android17
  • x86_64-linux-android21
user149408
  • 5,385
  • 4
  • 33
  • 69