Cross Build nghttp2 for Andriod

本文详细介绍如何为Android设备跨平台构建HTTP2库,包括获取nghttp2源代码、配置Android NDK环境变量及编写构建脚本等关键步骤,特别针对Arm64-v8a和Armeabi-v7a架构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Build Environment Prepare

If you wan to build nghttp2 for andriod you shoud do two things. Frist you should get nghttp2 source code, second you need to configure your build environment.

Get Nghttp2-v1.41 Source Code

  • The nghttp2 source web site at:
    https://github.com/nghttp2/nghttp2

  • Get libevent sorce code
    git clone https://github.com/nghttp2/nghttp2.git after you clone you need to switch to a stable tag like v1.4.1.

Get Andriod NDK and Configure Environment Variable.

  • NDK cross build tools web site at:
    https://developer.android.google.cn/ndk/downloads/index.html
  • Set NDK Environment Variable
    export NDK_HOME=/home/heyongxin/android-ndk-r16b
  • Openssl and libcrpto Note
    if you wan to use libevent_openssl.a, you need to set openssl include path in build scripts. My computer have make and install libevent in defaut path so i don’t need set it.

Writing the Build Scripts

Arm64-v8a and Armeabi-v7a chip arch hvave covered the most device in the market. so we only provide Arm64-v8a arch’s build script,the armeabi-v7a’s build scripts reference the v8a.

The Target Device for Arm64-v8a

#!/bin/sh
export ANDROID_NDK_ROOT=/home/heyongxin/android-ndk-r16b
NDK=/home/heyongxin/android-ndk-r16b
NGHTTP2_TMP_FOLDER=$PWD
NGHTTP2_TARGET_API=android-23
NGHTTP2_GCC_VERSION=4.9
NGHTTP2_OUTPUT_PATH=$PWD/nghttp2_build
rm -rf $NGHTTP2_OUTPUT_PATH
mkdir -p $NGHTTP2_OUTPUT_PATH
mkdir -p $NGHTTP2_TMP_FOLDER/android-toolchain-aarch64
NDK_MAKE_TOOLCHAIN=$NDK/build/tools/make-standalone-toolchain.sh
${NDK_MAKE_TOOLCHAIN} \
    --platform=${NGHTTP2_TARGET_API} \
    --arch=arm64 \
    --toolchain=aarch64-linux-android-${NGHTTP2_GCC_VERSION} \
    --install-dir="${NGHTTP2_TMP_FOLDER}/android-toolchain-aarch64" \
    --force
export TOOLCHAIN_PATH="${NGHTTP2_TMP_FOLDER}/android-toolchain-aarch64/bin"
export TOOL=aarch64-linux-android
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
export LINK=${CXX}
export LD=${NDK_TOOLCHAIN_BASENAME}-ld
export AR=${NDK_TOOLCHAIN_BASENAME}-ar
export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
export ARCH_FLAGS=
export ARCH_LINK=
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
cd ${NGHTTP2_TMP_FOLDER}
echo $CC
./configure --host=aarch64-linux-android  --disable-shared --enable-static 
make 

The Arch for Armeabi-v7a

#!/bin/sh
export ANDROID_NDK_ROOT=/home/heyongxin/android-ndk-r16b
NDK=/home/heyongxin/android-ndk-r16b
NGHTTP2_TMP_FOLDER=$PWD
NGHTTP2_TARGET_API=android-23
NGHTTP2_GCC_VERSION=4.9
NGHTTP2_OUTPUT_PATH=$PWD/nghttp2_build
rm -rf $NGHTTP2_OUTPUT_PATH
mkdir -p $NGHTTP2_OUTPUT_PATH
mkdir -p $NGHTTP2_TMP_FOLDER/android-toolchain-arm
NDK_MAKE_TOOLCHAIN=$NDK/build/tools/make-standalone-toolchain.sh
${NDK_MAKE_TOOLCHAIN} \
    --platform=${NGHTTP2_TARGET_API} \
    --arch=arm \
    --toolchain=arm-linux-androideabi-${NGHTTP2_GCC_VERSION} \
    --install-dir="${NGHTTP2_TMP_FOLDER}/android-toolchain-arm" \
    --force
export TOOLCHAIN_PATH="${NGHTTP2_TMP_FOLDER}/android-toolchain-arm/bin"
export TOOL=arm-linux-androideabi
export NDK_TOOLCHAIN_BASENAME=${TOOLCHAIN_PATH}/${TOOL}
export CC=${NDK_TOOLCHAIN_BASENAME}-gcc
export CXX=${NDK_TOOLCHAIN_BASENAME}-g++
export LINK=${CXX}
export LD=${NDK_TOOLCHAIN_BASENAME}-ld
export AR=${NDK_TOOLCHAIN_BASENAME}-ar
export RANLIB=${NDK_TOOLCHAIN_BASENAME}-ranlib
export STRIP=${NDK_TOOLCHAIN_BASENAME}-strip
export ARCH_FLAGS="-march=armv7-a -mfloat-abi=softfp -mfpu=vfpv3-d16"
export ARCH_LINK="-march=armv7-a -Wl,--fix-cortex-a8"
export CPPFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export CXXFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 -frtti -fexceptions "
export CFLAGS=" ${ARCH_FLAGS} -fpic -ffunction-sections -funwind-tables -fstack-protector -fno-strict-aliasing -finline-limit=64 "
export LDFLAGS=" ${ARCH_LINK} "
cd ${NGHTTP2_TMP_FOLDER}
echo $CC
./configure --host=arm-linux-androideabi  --disable-shared --enable-static
make

Build Output

If you run build scripts no error, it mean you have build success. Right now you can check the build output, in the source code root path. you will find the .libs folder. the folder context are as follows:

.libs
├── libnghttp2.a
├── libnghttp2.la -> ../libnghttp2.la
└── libnghttp2.lai

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值