英文:
Cross-compiling zlib for Android
问题
我正在尝试为Android交叉编译zlib(最终将用于提供OpenSSH二进制文件)。
我已经成功交叉编译了目标架构(armel)的库,运行在Linux上的步骤如下:
cd ~/src
git clone git@github.com:madler/zlib.git
cd zlib
# 如果需要,git checkout v1.2.13(或者当前版本)
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
告诉我该文件是针对ARM架构的二进制文件 - 到目前为止,一切正常。
过去我曾经能够在Android上运行Linux二进制文件 - 如果它们针对正确的平台,没有任何未满足的依赖关系,并且能够处理相当古老的内核,这似乎是有效的,但我不确定是否这是首选的方法。
因此,我尝试使用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
# 如果需要,git checkout v1.2.13(或者当前版本)
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
但是这会导致以下错误:
/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
问题:
- 我能够针对
arm-linux-gnueabi
并在Android上使用结果吗?或者这可能会导致错误? - 如果不行,正确的构建zlib供Android使用的方法是什么?
- 如果我尝试的构建zlib供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?
答案1
得分: 0
这个问题提供了一些指引,最终帮助我成功构建了项目:
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
# 如有需要,请执行 git checkout v1.2.13(或当前版本)
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 # 仅当要构建多个目标时才需要
CHOST
指定了目标架构:
arm-linux-androideabi
(ARM)aarch64-linux-android
(64位 ARM)i686-linux-android
(x86)x86_64-linux-android
(x86_64)
CHOST2
类似,但带有额外的处理器和平台API细节,这些细节需要用于选择正确的clang编译器(32位平台使用API 17,64位平台使用API 21):
armv7a-linux-androideabi17
aarch64-linux-android21
i686-linux-android17
x86_64-linux-android21
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论