编译OpenSSL for Android遇两类问题:链接错误与多架构编译失败
Hey there, I’ve run into similar headaches when compiling OpenSSL 1.1.1c with NDK r20 for Android, so let’s break down your two problems and fix them step by step:
These undefined references usually come from mismatched API levels, missing system library links, or a wonky cross-compiler setup. Here’s how to sort it out:
Specify the correct Android API level during OpenSSL configuration
NDK r20 needs explicit API level declaration to ensure compatibility. When configuring OpenSSL, add the--android-apiflag (use your target minimum API level, e.g., 21):./Configure android-arm --prefix=/your/desired/install/path --android-api 21Fix your example app’s linking command
Your link step is missing references to essential Android system libraries. Make sure to link againstlibcandlibdlin addition to OpenSSL’slibcryptoandlibssl, and point to the NDK’s sysroot:# Use the NDK r20's arm cross-compiler arm-linux-androideabi-gcc -o your_example your_example.c \ -L/path/to/your/compiled/openssl/lib -lcrypto -lssl \ -isystem $ANDROID_NDK_ROOT/sysroot/usr/include \ -L$ANDROID_NDK_ROOT/sysroot/usr/lib/arm-linux-androideabi \ -lc -ldlVerify your cross-compiler path
Ensure you’re using the compiler from NDK r20 instead of a system-wide one. Check with:which arm-linux-androideabi-gccThe output should point to
$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/[your-host-arch]/bin/arm-linux-androideabi-gcc.
Changing only the architecture variable in file-launcher-sh isn’t enough—you need to configure several key variables and adjust OpenSSL’s build parameters properly:
Complete the file-launcher-sh variable setup
Add these critical variables before running the build:# Point to your NDK r20 root directory export ANDROID_NDK_ROOT=/opt/android-ndk-r20 # Set the correct toolchain prefix for arm64 export TOOLCHAIN_PREFIX=aarch64-linux-android- # Add NDK toolchain to PATH export PATH=$ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/linux-x86_64/bin:$PATHConfigure OpenSSL for arm64 with proper flags
Explicitly set the architecture and crypto extensions in your configure command:./Configure android-arm64 --prefix=/your/install/path --android-api 21 \ CFLAGS="-march=armv8-a+crypto"Ensure you’re using the NDK’s assembler
System assemblers often don’t support ARMv8 instructions. Verify you’re using the NDK’s version:aarch64-linux-android-as --versionThe output should show it’s part of the NDK toolchain.
Clean old build artifacts
Always clean previous builds when switching architectures to avoid cached configuration conflicts:make clean rm -rf config.cache
内容的提问来源于stack exchange,提问作者MayurK




