libevent之android与鸿蒙编译过程

2024-08-22 23:36

本文主要是介绍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[1]: *** [sample/dns-example] Error 1
make: *** [all] Error 2
/Applications/Xcode.app/Contents/Developer/usr/bin/make  install-amCCLD     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[1]: *** [sample/dns-example] Error 1
make: *** [install] 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/sysrootexport 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 cleanmake j8make install
}
buildTARGET_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:$PATHexport 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 cleanOUTPUT_DIR=$(pwd)/android/$TARGET_ARCHmkdir -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 -j8make install
}
TARGET=android-arm64
buildexport TARGET_ARCH=armv7aexport 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开发者官网,共建鸿蒙生态

    编译链安装完毕,本文默认安装在以下目录: 

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└── syscapcheck11 directories, 15 files

编译脚本 

    编译链安装完毕,便可以利用该工具链进行编译了,贴出完整编译脚本: 

#!/bin/bashset -e# 根据实际位置修改
HARMONY_SDK_PATH=/Users/mingo/Library/OpenHarmony/Sdk/12CLANG=$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/sysrootZLIB_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-stablecd $LIBEVENT_SRC_PATH
PREFIX=${LIBEVENT_SRC_PATH}/ohos/aarch64
mkdir -p ${PREFIX}function build_event() {make cleanmake 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=1make installcd ..
}build_event

提示:鸿蒙编译链编译libevent的时候,先是下载的api=10的编译链,但在找系统库文件的时候找不到,于是切到api=12级别的编译链,可顺利编译出所需静态库。 

openssl编译

#!/bin/bashexport 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:$PATHexport 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 cleanOUTPUT_DIR=$(pwd)/android/$TARGET_ARCHmkdir -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 -j8make install
}
TARGET=android-arm64
buildexport TARGET_ARCH=armv7aexport 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

这篇关于libevent之android与鸿蒙编译过程的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1097715

相关文章

最新版IDEA配置 Tomcat的详细过程

《最新版IDEA配置Tomcat的详细过程》本文介绍如何在IDEA中配置Tomcat服务器,并创建Web项目,首先检查Tomcat是否安装完成,然后在IDEA中创建Web项目并添加Web结构,接着,... 目录配置tomcat第一步,先给项目添加Web结构查看端口号配置tomcat    先检查自己的to

SpringBoot集成SOL链的详细过程

《SpringBoot集成SOL链的详细过程》Solanaj是一个用于与Solana区块链交互的Java库,它为Java开发者提供了一套功能丰富的API,使得在Java环境中可以轻松构建与Solana... 目录一、什么是solanaj?二、Pom依赖三、主要类3.1 RpcClient3.2 Public

Android数据库Room的实际使用过程总结

《Android数据库Room的实际使用过程总结》这篇文章主要给大家介绍了关于Android数据库Room的实际使用过程,详细介绍了如何创建实体类、数据访问对象(DAO)和数据库抽象类,需要的朋友可以... 目录前言一、Room的基本使用1.项目配置2.创建实体类(Entity)3.创建数据访问对象(DAO

SpringBoot整合kaptcha验证码过程(复制粘贴即可用)

《SpringBoot整合kaptcha验证码过程(复制粘贴即可用)》本文介绍了如何在SpringBoot项目中整合Kaptcha验证码实现,通过配置和编写相应的Controller、工具类以及前端页... 目录SpringBoot整合kaptcha验证码程序目录参考有两种方式在springboot中使用k

你的华为手机升级了吗? 鸿蒙NEXT多连推5.0.123版本变化颇多

《你的华为手机升级了吗?鸿蒙NEXT多连推5.0.123版本变化颇多》现在的手机系统更新可不仅仅是修修补补那么简单了,华为手机的鸿蒙系统最近可是动作频频,给用户们带来了不少惊喜... 为了让用户的使用体验变得很好,华为手机不仅发布了一系列给力的新机,还在操作系统方面进行了疯狂的发力。尤其是近期,不仅鸿蒙O

SpringBoot整合InfluxDB的详细过程

《SpringBoot整合InfluxDB的详细过程》InfluxDB是一个开源的时间序列数据库,由Go语言编写,适用于存储和查询按时间顺序产生的数据,它具有高效的数据存储和查询机制,支持高并发写入和... 目录一、简单介绍InfluxDB是什么?1、主要特点2、应用场景二、使用步骤1、集成原生的Influ

SpringBoot实现websocket服务端及客户端的详细过程

《SpringBoot实现websocket服务端及客户端的详细过程》文章介绍了WebSocket通信过程、服务端和客户端的实现,以及可能遇到的问题及解决方案,感兴趣的朋友一起看看吧... 目录一、WebSocket通信过程二、服务端实现1.pom文件添加依赖2.启用Springboot对WebSocket

Android WebView的加载超时处理方案

《AndroidWebView的加载超时处理方案》在Android开发中,WebView是一个常用的组件,用于在应用中嵌入网页,然而,当网络状况不佳或页面加载过慢时,用户可能会遇到加载超时的问题,本... 目录引言一、WebView加载超时的原因二、加载超时处理方案1. 使用Handler和Timer进行超

鸿蒙开发搭建flutter适配的开发环境

《鸿蒙开发搭建flutter适配的开发环境》文章详细介绍了在Windows系统上如何创建和运行鸿蒙Flutter项目,包括使用flutterdoctor检测环境、创建项目、编译HAP包以及在真机上运... 目录环境搭建创建运行项目打包项目总结环境搭建1.安装 DevEco Studio NEXT IDE

浅析Spring Security认证过程

类图 为了方便理解Spring Security认证流程,特意画了如下的类图,包含相关的核心认证类 概述 核心验证器 AuthenticationManager 该对象提供了认证方法的入口,接收一个Authentiaton对象作为参数; public interface AuthenticationManager {Authentication authenticate(Authenti