You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

编译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:

Problem 1: Android-ARM library compiles successfully, but example app throws undefined reference errors (sigdelset, sigfillset, etc.)

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-api flag (use your target minimum API level, e.g., 21):

    ./Configure android-arm --prefix=/your/desired/install/path --android-api 21
    
  • Fix your example app’s linking command
    Your link step is missing references to essential Android system libraries. Make sure to link against libc and libdl in addition to OpenSSL’s libcrypto and libssl, 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 -ldl
    
  • Verify 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-gcc
    

    The output should point to $ANDROID_NDK_ROOT/toolchains/llvm/prebuilt/[your-host-arch]/bin/arm-linux-androideabi-gcc.

Problem 2: Assembly errors when compiling for Android-ARM64 (unrecognized .arch armv8-a+crypto, invalid stp instruction)

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:$PATH
    
  • Configure 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 --version
    

    The 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

火山引擎 最新活动