赞
踩
笔者最近将python解释器cpython3.8移植到了OpenHarmony标准系统上。并且开源相关编译产物。
本文介绍如何在OpenHarmony标准系统上部署Python3.8编程环境,并且使用Python调用C/C++的SO库。
移植思路分享:
1.使用 OpenHarmony NDK
在X86的linux主机上 交叉编译 出OpenHarmony arm平台可以运行的Cpython解释器。
2.OpenHarmony系统采用llvm项目中的libc++作为C++运行时库,在系统侧使用libc ++ .so库来承载,应用侧使用libc++_shared.so来承载,两边一套代码,不同的C++命名空间。 https://gitee.com/openharmony/docs/blob/master/zh-cn/application-dev/napi/ohos-abi.md#c-abi
3.可执行文件格式:
目前缺失的模块:
1.解压cpython-3.8_oh_install.tar.xz
文件为cpython-3.8_oh_install.tar
文件
2.将解压好的cpython-3.8_oh_install.tar
文件发送到开发板data目录,将cpython-3.8_oh_install.tar
解压。
hdc file send cpython-3.8_oh_install.tar /data
hdc shell #进入开发板终端
cd data
tar -xvf cpython-3.8_oh_install.tar
3.设置python命令(cpython编译出来的可执行文件)所在路径
export PYTHONHOME=/data/cpython-3.8_oh_instal/bin
4.设置python库文件所在路径,包含两个路径
# /data/cpython-3.8_oh_install/lib/python3.8里面有基础库的.py文件
# /data/cpython-3.8_oh_install/lib/python3.8/lib-dynload里面有zlib、libffi等三方库的so 以及 import math需要的math.cpython-38-aarch64-linux-gnu.so等诸如此类的so
export PYTHONPATH=/data/cpython-3.8_oh_install/lib/python3.8:/data/cpython-3.8_oh_install/lib/python3.8/lib-dynload
# 设置third_party的so库的查找位置
# 因为把third_party的,例如libffi的库文件放在/data/cpython-3.8_oh_install/lib/python3.8/lib-dynload
export LD_LIBRARY_PATH=/data/cpython-3.8_oh_install/lib/python3.8/lib-dynload:$LD_LIBRARY_PATH
5.运行python3
cd cpython-3.8_oh_install
./bin/python3
6.将开发板连接网络
,安装pip
# 安装pip
./bin/python3 -m ensurepip --upgrade
# 卸载pip
./bin/python3 -m pip uninstall pip
# pip管理的三方列表
./bin/bin/pip3 list
# 安装requests库,使用国内清华源
./bin/bin/pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple requests
# 卸载requests库
./bin/bin/pip3 uninstall request
7.将64位python3和pip添加到系统环境变量中
export PATH=/data/cpython-3.8_oh_install/bin:$PATH
export PATH=/data/cpython-3.8_oh_install/bin/bin:$PATH
#上一步中pip3 and pip3.8被安装在/data/cpython-3.8_oh_instal
export PATH="/data/cpython-3.8_oh_instal/bin/bin:$PATH"
下面以一个简单的例子来实现在OpenHarmony标准系统中用Python的ctypes模块调用C/C++的SO库(类似于NAPI),按照以下步骤进行:
1.下载example工程,工程目录结构为
.
├── CMakeLists.txt
└── example.c
// 一个简单的C函数,将两个整数相加并返回结果
int add(int a, int b) {
return a + b;
}
- CMakeLists.txt
add_library(example SHARED example.c)
2.使用OpenHarmony NDK编译[example](https://gitee.com/from-north-to-north/OpenHarmony_Python/tree/master/1.Python%E8%B0%83%E7%94%A8C%E6%88%96C++%E4%BB%A3%E7%A0%81(Python%E8%B0%83%E7%94%A8c%E6%88%96c++%E7%9A%84so,%E7%B1%BB%E4%BC%BC%E4%BA%8ENAPI)/example)工程,下载 [OpenHarmony SDK ](https://gitee.com/openharmony-sig/oh-inner-release-management/blob/master/Release-Testing-Version.md) 到linux环境,解压好其中linux的`native-linux-x64-x.x.x.x-Release.zip`包
3.设置OpenHarmony NDK 64位编译工具链环境变量
export OHOS_SDK=/home/jiajiahao/OpenHarmony/ndk/ohos-sdk/linux # 此处是我的ohos_sdk解压目录,请替换为你自己的解压目录
export AS=${OHOS_SDK}/native/llvm/bin/llvm-as
export CC="${OHOS_SDK}/native/llvm/bin/clang --target=aarch64-linux-ohos"
export CXX="${OHOS_SDK}/native/llvm/bin/clang++ --target=aarch64-linux-ohos"
export LD=${OHOS_SDK}/native/llvm/bin/ld.lld
export STRIP=${OHOS_SDK}/native/llvm/bin/llvm-strip
export RANLIB=${OHOS_SDK}/native/llvm/bin/llvm-ranlib
export OBJDUMP=${OHOS_SDK}/native/llvm/bin/llvm-objdump
export OBJCOPY=${OHOS_SDK}/native/llvm/bin/llvm-objcopy
export NM=${OHOS_SDK}/native/llvm/bin/llvm-nm
export AR=${OHOS_SDK}/native/llvm/bin/llvm-ar
export CFLAGS="-fPIC -D__MUSL__=1"
export CXXFLAGS="-fPIC -D__MUSL__=1"
4.设置好OpenHarmony NDK编译环境后,编译[example](https://gitee.com/from-north-to-north/OpenHarmony_Python/tree/master/1.Python%E8%B0%83%E7%94%A8C%E6%88%96C++%E4%BB%A3%E7%A0%81(Python%E8%B0%83%E7%94%A8c%E6%88%96c++%E7%9A%84so,%E7%B1%BB%E4%BC%BC%E4%BA%8ENAPI)/example)工程
cd example
mkdir build
cd build
make clean
/home/jiajiahao/OpenHarmony/ndk/ohos-sdk/linux/native/build-tools/cmake/bin/cmake -DCMAKE_TOOLCHAIN_FILE=/home/jiajiahao/OpenHarmony/ndk/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake .. -L
make
![image.png](https://dl-harmonyos.51cto.com/images/202312/1509f5f9088533b3c26534ee5756e505e8fe57.png?x-oss-process=image/resize,w_820,h_1209)
5.将编译好的`libexample.so`发送到`开发板`上,使用Python调用so库
执行以下代码:
创建一个名为example.py的Python文件,编写如下代码:
import ctypes
libexample = ctypes.CDLL('./libexample.so')
result = libexample.add(2, 3)
print("Result:", result)
6.调用so库,最后得到两个数字相加的结果。
![image.png](https://dl-harmonyos.51cto.com/images/202312/369d10e583cfb61f74e514fd739d1a2111c683.png?x-oss-process=image/resize,w_820,h_505)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。