libevent之android与鸿蒙编译过程
背景近来基于libevent开发了一个端侧的缓存代理库,先是基于macOS编译开发的,基本0问题,后来移植到鸿蒙与android时遇到一些编译链接问题。
libevent版本如下:
软件版本号libevent-2.1.8 android编译
编译环境
android studio版本ndk编译链版本api级别 Android Studio Flamingo | 2022.2.1 Patch 2
Build #AI-222.4459.24.2221.10121639, built on May 12, 2023
Runtime version: 17.0.6+0-17.0.6b802.4-9586694 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 14.3.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 10
Metal Rendering is ON
Registry:
external.system.auto.import.disabled=true
ide.text.editor.with.preview.show.floating.toolbar=false
ide.instant.shutdown=false
gradle.version.catalogs.dynamic.support=true
Non-Bundled Plugins:
OpenGL-Plugin (1.0.73)
Dart (222.4582)
com.localizely.flutter-intl (1.18.3-2022.2)
io.flutter (74.0.2)
~/Library/Android/sdk/ndk/22.1.717167028 问题征象
ld: error: undefined symbol: arc4random_addrandom
>>> referenced by evutil_rand.c
>>> evutil_rand.o:(evutil_secure_rng_add_bytes) in archive ./.libs/libevent.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** Error 1
make: *** Error 2
/Applications/Xcode.app/Contents/Developer/usr/bin/makeinstall-am
CCLD sample/dns-example
ld: error: undefined symbol: arc4random_addrandom
>>> referenced by evutil_rand.c
>>> evutil_rand.o:(evutil_secure_rng_add_bytes) in archive ./.libs/libevent.a
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** Error 1
make: *** Error 2 办理办法
找到evutil_rand.c文件,将以下代码注释掉即可:
// void
// evutil_secure_rng_add_bytes(const char *buf, size_t n)
// {
// arc4random_addrandom((unsigned char*)buf,
// n>(size_t)INT_MAX ? INT_MAX : (int)n);
// } 贴出android下编译libevent为arm64和armv7a架构的shell脚本:
#!/bin/bash
#根据实际安装位置修改
export ANDROID_NDK_HOME=/Users/mingo/Library/Android/sdk/ndk/21.1.6352462
export TOOLCHAIN_DIR=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64
export SYSROOT=$TOOLCHAIN_DIR/sysroot
export TARGET_ARCH=aarch64-linux-android
export TOOLCHAIN=$TOOLCHAIN_DIR
export API_LEVEL=21
TARGET=aarch64-linux-android${API_LEVEL}
export CLANG="$TOOLCHAIN/bin/aarch64-linux-android$API_LEVEL-clang"
export CLANGXX="$TOOLCHAIN/bin/aarch64-linux-android$API_LEVEL-clang++"
export AR="$TOOLCHAIN/bin/aarch64-linux-android-ar"
export LD="$TOOLCHAIN/bin/aarch64-linux-android-ld"
export RANLIB="$TOOLCHAIN/bin/aarch64-linux-android-ranlib"
export STRIP="$TOOLCHAIN/bin/aarch64-linux-android-strip"
#根据实际安装位置修改
OPENSSL_OUTPUT=/Users/mingo/Applications/workspace/opensrc/openssl-android/android
OPENSSL_DIR=${OPENSSL_OUTPUT}/aarch64
PKG_CONFIG_PATH="$OPENSSL_DIR/lib/pkgconfig"
CFLAGS="--sysroot=$SYSROOT -I$OPENSSL_DIR/include --target=$TARGET -fPIC -DEVUTIL_DISABLE_ARC4RANDOM"
LDFLAGS="--sysroot=$SYSROOT -L$OPENSSL_DIR/lib -lz -lc"
OUTPUT_DIR=$(pwd)/android/$TARGET_ARCH
mkdir -p ${OUTPUT_DIR}
function build() {
./configure --host=$TARGET_ARCH --prefix=$OUTPUT_DIR \
--with-sysroot=${SYSROOT} \
--with-openssl=$OPENSSL_DIR \
--disable-shared \
--disable-arc4random \
CC=$CLANG \
CXX=$CLANGXX \
AR=$AR \
RANLIB=$RANLIB \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS"
make clean
make j8
make install
}
build
TARGET_ARCH=arm-linux-androideabi
TARGET=arm-linux-android${API_LEVEL}
OUTPUT_DIR=$(pwd)/android/$TARGET_ARCH
mkdir -p ${OUTPUT_DIR}
OPENSSL_DIR=${OPENSSL_OUTPUT}/armv7a
PKG_CONFIG_PATH="$OPENSSL_DIR/lib/pkgconfig"
CFLAGS="--sysroot=$SYSROOT -I$OPENSSL_DIR/include --target=$TARGET -fPIC -DEVUTIL_DISABLE_ARC4RANDOM"
LDFLAGS="--sysroot=$SYSROOT -L$OPENSSL_DIR/lib -lz"
build openssl编译
#!/bin/bash
#根据实际安装位置修改
export ANDROID_NDK_HOME=/Users/mingo/Library/Android/sdk/ndk/21.1.6352462
export TOOLCHAIN_DIR=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64
export PATH=$TOOLCHAIN_DIR/bin:$PATH
export TARGET_ARCH=aarch64
export API_LEVEL=21# Android 5.0 (Lollipop)
export CC="$TOOLCHAIN_DIR/bin/aarch64-linux-android$API_LEVEL-clang"
export CXX="$TOOLCHAIN_DIR/bin/aarch64-linux-android$API_LEVEL-clang++"
export AR="$TOOLCHAIN_DIR/bin/aarch64-linux-android-ar"
export AS="$TOOLCHAIN_DIR/bin/aarch64-linux-android-as"
export LD="$TOOLCHAIN_DIR/bin/aarch64-linux-android-ld"
export RANLIB="$TOOLCHAIN_DIR/bin/aarch64-linux-android-ranlib"
export STRIP="$TOOLCHAIN_DIR/bin/aarch64-linux-android-strip"
function build() {
make clean
OUTPUT_DIR=$(pwd)/android/$TARGET_ARCH
mkdir -p ${OUTPUT_DIR}
#config for android arm64
./Configure ${TARGET} -D__ANDROID_API__=${API_LEVEL} no-asm zlib no-shared no-ssl2 no-ssl3 no-comp no-hw no-engine --prefix=${OUTPUT_DIR}
make -j8
make install
}
TARGET=android-arm64
build
export TARGET_ARCH=armv7a
export TARGET_HOST=armv7a-linux-androideabi
export CC="$TOOLCHAIN_DIR/bin/${TARGET_HOST}${API_LEVEL}-clang"
export CXX="$TOOLCHAIN_DIR/bin/${TARGET_HOST}${API_LEVEL}-clang++"
export AR="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-ar"
export AS="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-as"
export LD="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-ld"
export RANLIB="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-ranlib"
export STRIP="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-strip"
TARGET=android-arm
build 注意
1)在一个app项目里,ndk编译的时候要记得android api级别要一致,就好比本文app的android api级别要求位21,那么在编译libevent和openssl的时候,都要调整为21方可。
鸿蒙编译
编译环境
DevEco版本编译链版本api级别DevEco Studio NEXT Developer Beta3
Build #DS-233.14475.28.36.503600
Build Version: 5.0.3.600, built on August 7, 2024
Runtime version: 17.0.10+1-b1087.17 aarch64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
macOS 14.3.1
GC: G1 Young Generation, G1 Old Generation
Memory: 2048M
Cores: 10
Metal Rendering is ON
Registry:
idea.plugins.compatible.build=IC-233.14475.28
~/Library/OpenHarmony/Sdk/1212 先是到华为官网下载DevEco IDE开发工具,ID安装好后,再安装鸿蒙编译链,选择api 12的ndk版本进行安装:
下载中心 | 华为开发者联盟-HarmonyOS开发者官网,共建鸿蒙生态
https://i-blog.csdnimg.cn/direct/11ea530e5ab047a2b65f1e48cea438fb.png
编译链安装完毕,本文默认安装在以下目录:
mingo@localhost:~/Library/OpenHarmony/Sdk/12$pwd
/Users/mingo/Library/OpenHarmony/Sdk/12
mingo@localhost:~/Library/OpenHarmony/Sdk/12$tree -L 2
.
├── native
│ ├── NOTICE.txt
│ ├── build
│ ├── build-tools
│ ├── llvm
│ ├── nativeapi_syscap_config.json
│ ├── ndk_system_capability.json
│ ├── oh-uni-package.json
│ └── sysroot
└── toolchains
├── NOTICE.txt
├── ark_disasm
├── configcheck
├── diff
├── hdc
├── hnpcli
├── id_defined.json
├── idl
├── lib
├── libusb_shared.dylib
├── modulecheck
├── oh-uni-package.json
├── restool
├── syscap_tool
└── syscapcheck
11 directories, 15 files 编译脚本
编译链安装完毕,便可以利用该工具链进行编译了,贴出完整编译脚本:
#!/bin/bash
set -e
# 根据实际位置修改
HARMONY_SDK_PATH=/Users/mingo/Library/OpenHarmony/Sdk/12
CLANG=$HARMONY_SDK_PATH/native/llvm/bin/clang
CLANGXX=$HARMONY_SDK_PATH/native/llvm/bin/clang++
AR=$HARMONY_SDK_PATH/native/llvm/bin/llvm-ar
RANLIB=$HARMONY_SDK_PATH/native/llvm/bin/llvm-ranlib
# compile for aarch64
TARGET_ARCH=aarch64-linux-gnu
TARGET=aarch64-linux-ohos
SYSROOT=$HARMONY_SDK_PATH/native/sysroot
ZLIB_ROOT_PATH=${HARMONY_SDK_PATH}/native/sysroot/usr
ZLIB_INCLUDE_PATH=${ZLIB_ROOT_PATH}/include
ZLIB_LIB_PATH=${ZLIB_ROOT_PATH}/lib/aarch64-linux-ohos
# 根据实际位置修改
OPENSSL_DIR=/Users/mingo/Applications/workspace/opensrc/openssl/hmos/aarch64
PKG_CONFIG_PATH="$OPENSSL_DIR/lib/pkgconfig"
CPPFLAGS="-I$OPENSSL_DIR/include -I${ZLIB_INCLUDE_PATH}"
SYSLIB_INCLUDE_DIR=${SYSROOT}/usr/include/aarch64-linux-ohos
echo "syslib=$SYSLIB_INCLUDE_DIR"
CFLAGS="--sysroot=$SYSROOT -I$SYSLIB_INCLUDE_DIR -I$OPENSSL_DIR/include -I$ZLIB_INCLUDE_PATH --target=$TARGET -fPIC"
#CFLAGS="--sysroot=$SYSROOT -I$SYSLIB_INCLUDE_DIR -I$ZLIB_INCLUDE_PATH --target=$TARGET -fPIC"
LDFLAGS="--sysroot=$SYSROOT -L$OPENSSL_DIR/lib -L$ZLIB_LIB_PATH -lz"
export CC=$CLANG
export CXX=$CLANGXX
export AR=$AR
export RANLIB=$RANLIB
export CFLAGS=$CFLAGS
export LDFLAGS=$LDFLAGS
# compile for libevent-2.1.8-stable
LIBEVENT_SRC_PATH=`pwd`/libevent-2.1.8-stable
cd $LIBEVENT_SRC_PATH
PREFIX=${LIBEVENT_SRC_PATH}/ohos/aarch64
mkdir -p ${PREFIX}
function build_event() {
make clean
make distclean
./configure --host=$TARGET_ARCH --prefix=$PREFIX \
--with-sysroot=${SYSLIB_INCLUDE_DIR} \
--with-openssl=$OPENSSL_DIR \
--with-zlib=$ZLIB_ROOT_PATH \
--disable-shared \
CC=$CLANG \
CXX=$CLANGXX \
AR=$AR \
RANLIB=$RANLIB \
CFLAGS="$CFLAGS" \
LDFLAGS="$LDFLAGS"
make verbose=1
make install
cd ..
}
build_event 提示:鸿蒙编译链编译libevent的时候,先是下载的api=10的编译链,但在找系统库文件的时候找不到,于是切到api=12级别的编译链,可顺遂编译出所需静态库。
openssl编译
#!/bin/bash
export ANDROID_NDK_HOME=/Users/mingo/Library/Android/sdk/ndk/21.1.6352462
export TOOLCHAIN_DIR=$ANDROID_NDK_HOME/toolchains/llvm/prebuilt/darwin-x86_64
export PATH=$TOOLCHAIN_DIR/bin:$PATH
export TARGET_ARCH=aarch64
export API_LEVEL=21# Android 5.0 (Lollipop)
export CC="$TOOLCHAIN_DIR/bin/aarch64-linux-android$API_LEVEL-clang"
export CXX="$TOOLCHAIN_DIR/bin/aarch64-linux-android$API_LEVEL-clang++"
export AR="$TOOLCHAIN_DIR/bin/aarch64-linux-android-ar"
export AS="$TOOLCHAIN_DIR/bin/aarch64-linux-android-as"
export LD="$TOOLCHAIN_DIR/bin/aarch64-linux-android-ld"
export RANLIB="$TOOLCHAIN_DIR/bin/aarch64-linux-android-ranlib"
export STRIP="$TOOLCHAIN_DIR/bin/aarch64-linux-android-strip"
function build() {
make clean
OUTPUT_DIR=$(pwd)/android/$TARGET_ARCH
mkdir -p ${OUTPUT_DIR}
#config for android arm64
./Configure ${TARGET} -D__ANDROID_API__=${API_LEVEL} no-asm zlib no-shared no-ssl2 no-ssl3 no-comp no-hw no-engine --prefix=${OUTPUT_DIR}
make -j8
make install
}
TARGET=android-arm64
build
export TARGET_ARCH=armv7a
export TARGET_HOST=armv7a-linux-androideabi
export CC="$TOOLCHAIN_DIR/bin/${TARGET_HOST}${API_LEVEL}-clang"
export CXX="$TOOLCHAIN_DIR/bin/${TARGET_HOST}${API_LEVEL}-clang++"
export AR="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-ar"
export AS="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-as"
export LD="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-ld"
export RANLIB="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-ranlib"
export STRIP="$TOOLCHAIN_DIR/bin/${TARGET_HOST}-strip"
TARGET=android-arm
build
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页:
[1]