一、前期准备
- 从 openHarmony SDK 官方发布渠道下载 SDK。
我下载的版本是 version-Master_Version-OpenHarmony_4.1.6.5_dev-20240127_020213-ohos-sdk-full.tar.gz
解压 SDK,得到 ohos_sdk 目录。可以看到 Linux 和 windows 两个平台的 SDK 目录,我们进入 Linux 目录。
ohos@ubuntu20:~/tools/OH_SDK$ cd ohos-sdk
ohos@ubuntu20:~/tools/OH_SDK/ohos-sdk$ ls
linux windows
ohos@ubuntu20:~/tools/OH_SDK/ohos-sdk$ cd linux/
ohos@ubuntu20:~/tools/OH_SDK/ohos-sdk/linux$ ls
ets-linux-x64-4.0.1.2-Canary1.zip native-linux-x64-4.0.1.2-Canary1.zip toolchains-linux-x64-4.0.1.2-Canary1.zip
js-linux-x64-4.0.1.2-Canary1.zip previewer-linux-x64-4.0.1.2-Canary1.zip
- 解压 Linux 环境下的 OHOS SDK 工具包,得到对应工具目录。由于我们是 c/c++ 库的编译,我们只需要关注 native 目录,将其解压即可。
二、使用 ohos-sdk 编译 OpenSSL 库
- 下载 openssl 官方源码,我使用的版本是3.0.12。
设置需要的编译工具链环境变量。这里设置的是 ohos64bit 库的编译工具链,如果需要编译 ohos32bit 库请设置为 32bit 编译工具链。
ohos@ubuntu20:~/openHarmony/openssl$ export OHOS_SDK=/home/ohos/tools/OH_SDK/ohos-sdk/linux #这里根据自己的SDK解压目录设置
ohos@ubuntu20:~/openHarmony/openssl$ export AS=${OHOS_SDK}/native/llvm/bin/llvm-as
ohos@ubuntu20:~/openHarmony/openssl$ export CC="${OHOS_SDK}/native/llvm/bin/clang --target=aarch64-linux-ohos"
ohos@ubuntu20:~/openHarmony/openssl$ export CXX="${OHOS_SDK}/native/llvm/bin/clang++ --target=aarch64-linux-ohos"
ohos@ubuntu20:~/openHarmony/openssl$ export LD=${OHOS_SDK}/native/llvm/bin/ld.lld
ohos@ubuntu20:~/openHarmony/openssl$ export STRIP=${OHOS_SDK}/native/llvm/bin/llvm-strip
ohos@ubuntu20:~/openHarmony/openssl$ export RANLIB=${OHOS_SDK}/native/llvm/bin/llvm-ranlib
ohos@ubuntu20:~/openHarmony/openssl$ export OBJDUMP=${OHOS_SDK}/native/llvm/bin/llvm-objdump
ohos@ubuntu20:~/openHarmony/openssl$ export OBJCOPY=${OHOS_SDK}/native/llvm/bin/llvm-objcopy
ohos@ubuntu20:~/openHarmony/openssl$ export NM=${OHOS_SDK}/native/llvm/bin/llvm-nm
ohos@ubuntu20:~/openHarmony/openssl$ export AR=${OHOS_SDK}/native/llvm/bin/llvm-ar
ohos@ubuntu20:~/openHarmony/openssl$ export CFLAGS="-fPIC -D__MUSL__=1"
ohos@ubuntu20:~/openHarmony/openssl$ export CXXFLAGS="-fPIC -D__MUSL__=1"
- 执行 Configure 构建脚本。
ohos@ubuntu20:~/openHarmony/openssl$ ./Configure linux-aarch64 --prefix=/home/ohos/openHarmony/openssl_install
# 在需要选择系统的时候由于configure不识别ohos,因此我们需要使用Linux aarch64等价系统和cpu架构 为了方便打包,需要指定安装目录
Configuring OpenSSL version 1.1.1t (0x1010114fL) for linux-aarch64
Using os-specific seed configuration
Creating configdata.pm
Creating Makefile
**********************************************************************
*** ***
*** OpenSSL has been successfully configured ***
*** ***
*** If you encounter a problem while building, please open an ***
*** issue on GitHub <https://github.com/openssl/openssl/issues> ***
*** and include the output from the following command: ***
*** ***
*** perl configdata.pm --dump ***
*** ***
*** (If you are new to OpenSSL, you might want to consult the ***
*** 'Troubleshooting' section in the INSTALL file first) ***
*** ***
**********************************************************************
ohos@ubuntu20:~/openHarmony/openssl$ make
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" include/crypto/bn_conf.h.in > include/crypto/bn_conf.h
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" include/crypto/dso_conf.h.in > include/crypto/dso_conf.h
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" include/openssl/opensslconf.h.in > include/openssl/opensslconf.h
make depend && make _all
make[1]: Entering directory '/home/ohos/openHarmony/openssl'
make[1]: Leaving directory '/home/ohos/openHarmony/openssl'
make[1]: Entering directory '/home/ohos/openHarmony/openssl'
/home/ohos/tools/OH_SDK/ohos-sdk/linux/native/llvm/bin/clang -I. -Iinclude -fPIC -pthread -Wa,--noexecstack -Qunused-arguments -fPIC --target=aarch64-linux-ohos -D__MUSL__=1 -DOPENSSL_USE_NODELETE -DOPENSSL_PIC -DOPENSSL_CPUID_OBJ -DOPENSSL_BN_ASM_MONT -DSHA1_ASM -DSHA256_ASM -DSHA512_ASM -DKECCAK1600_ASM -DVPAES_ASM -DECP_NISTZ256_ASM -DPOLY1305_ASM -DOPENSSLDIR="\"/home/ohos/openHarmony/openssl_install/ssl\"" -DENGINESDIR="\"/home/ohos/openHarmony/openssl_install/lib/engines-1.1\"" -DNDEBUG -MMD -MF apps/app_rand.d.tmp -MT apps/app_rand.o -c -o apps/app_rand.o apps/app_rand.c
# 删除大量 make 日志
${LDCMD:-/home/ohos/tools/OH_SDK/ohos-sdk/linux/native/llvm/bin/clang} -pthread -Wa,--noexecstack -Qunused-arguments -fPIC --target=aarch64-linux-ohos -D__MUSL__=1 -L. \
-o test/x509aux test/x509aux.o \
test/libtestutil.a -lcrypto -ldl -pthread
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" apps/CA.pl.in > "apps/CA.pl"
chmod a+x apps/CA.pl
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" apps/tsget.in > "apps/tsget.pl"
chmod a+x apps/tsget.pl
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" tools/c_rehash.in > "tools/c_rehash"
chmod a+x tools/c_rehash
/usr/bin/perl "-I." -Mconfigdata "util/dofile.pl" \
"-oMakefile" util/shlib_wrap.sh.in > "util/shlib_wrap.sh"
chmod a+x util/shlib_wrap.sh
make[1]: Leaving directory '/home/ohos/openHarmony/openssl'
ohos@ubuntu20:~/openHarmony/openssl$ make install
make depend && make _build_libs
make[1]: Entering directory '/home/ohos/openHarmony/openssl'
make[1]: Leaving directory '/home/ohos/openHarmony/openssl'
make[1]: Entering directory '/home/ohos/openHarmony/openssl'
make[1]: Nothing to be done for '_build_libs'.
make[1]: Leaving directory '/home/ohos/openHarmony/openssl'
created directory `/home/ohos/openHarmony/openssl_install/lib'
*** Installing runtime libraries
install libcrypto.so.1.1 -> /home/ohos/openHarmony/openssl_install/lib/libcrypto.so.1.1
install libssl.so.1.1 -> /home/ohos/openHarmony/openssl_install/lib/libssl.so.1.1
*** Installing development files
created directory `/home/ohos/openHarmony/openssl_install/include'
created directory `/home/ohos/openHarmony/openssl_install/include/openssl'
install ./include/openssl/aes.h -> /home/ohos/openHarmony/openssl_install/include/openssl/aes.h
install ./include/openssl/asn1.h -> /home/ohos/openHarmony/openssl_install/include/openssl/asn1.h
install ./include/openssl/asn1_mac.h -> /home/ohos/openHarmony/openssl_install/include/openssl/asn1_mac.h
# 删除大量 install 日志
/home/ohos/openHarmony/openssl_install/share/doc/openssl/html/man7/X25519.html
/home/ohos/openHarmony/openssl_install/share/doc/openssl/html/man7/X448.html -> /home/ohos/openHarmony/openssl_install/share/doc/openssl/html/man7/X25519.html
/home/ohos/openHarmony/openssl_install/share/doc/openssl/html/man7/x509.html
ohos@ubuntu20:~/openHarmony/openssl$
三、使用 ohos-sdk 编译 Poco 库
- SDK 采用 cmake 作为默认的编译脚本工具和 DevEco 开发工具是一致的。
下载 Poco 官方源码,建议使用版本是 1.12.6。如果使用1.12.4版本,会导致编译出的 libPocoCrypto.so 无法正常使用上述版本的 OpenSSL 库。 - 进入 poco-poco-1.12.6 目录,创建 ohos64build 目录(存放构建文件)。执行 cmake,生成 makefile。
root@kw:~/d# cd poco-poco-1.12.6
root@kw:~/d/poco-poco-1.12.6# mkdir ohos64build&&cd ohos64build
root@kw:~/d/poco-poco-1.12.6/ohos64build# /root/d/ohos-sdk/linux/native/build-tools/cmake/bin/cmake -DCMAKE_TOOLCHAIN_FILE=/root/d/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake -DOPENSSL_ROOT_DIR=/root/d/ohos-sdk/linux/native/sysroot/usr/openssl .. -L
-- The C compiler identification is Clang 15.0.4
-- The CXX compiler identification is Clang 15.0.4
-- Check for working C compiler: /root/d/ohos-sdk/linux/native/llvm/bin/clang
-- Check for working C compiler: /root/d/ohos-sdk/linux/native/llvm/bin/clang -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /root/d/ohos-sdk/linux/native/llvm/bin/clang++
-- Check for working CXX compiler: /root/d/ohos-sdk/linux/native/llvm/bin/clang++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Checking for C++14 compiler
-- Checking for C++14 compiler - available
-- Found OpenSSL: /root/d/ohos-sdk/linux/native/sysroot/usr/openssl/lib/libcrypto.so
-- Could NOT find APR (missing: APR_INCLUDE_DIR APR_LIBRARY)
-- Could NOT find APRUTIL (missing: APRUTIL_INCLUDE_DIR APRUTIL_LIBRARY)
-- Could NOT find Apache2 (missing: APACHE2_INCLUDE_DIR)
-- Could NOT find MySQL (missing: MYSQL_INCLUDE_DIR MYSQL_LIBRARY)
-- Could NOT find PostgreSQL (missing: PostgreSQL_LIBRARY PostgreSQL_INCLUDE_DIR)
-- Could NOT find ODBC (missing: ODBC_LIBRARY ODBC_INCLUDE_DIR)
-- Building without tests & samples
-- Using internal sqlite, zlib, pcre2, expat, ...
-- SQLite Support Enabled
-- MySQL Support Disabled
-- PostgreSQL Support Disabled
-- ODBC Support Disabled
-- CMake 3.16.5 successfully configured Poco using Unix Makefiles generator
-- Poco package version: 1.12.6
-- Building dynamic libraries
-- [cmake] Installation target path: /usr/local
-- [cmake] Use toolchain file: /root/d/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake
-- [cmake] Build for OS type: OHOS
-- [cmake] Build for OS version: 1
-- [cmake] Build for CPU type: aarch64
-- [cmake] Build type: RelWithDebInfo
-- [cmake] Build with cxx flags: -O2 -g -DNDEBUG -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -D__MUSL__
-- [cmake] Build with c flags: -O2 -g -DNDEBUG -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -fno-addrsig -Wa,--noexecstack -Wformat -Werror=format-security -D__MUSL__
-- Building: Encodings
-- Building: XML
-- Building: JSON
-- Building: Util
-- Building: Net
-- Building: MongoDB
-- Building: Redis
-- Building: Prometheus
-- Building: JWT
-- Building: NetSSL_OpenSSL
-- Building: Crypto
-- Building: Data
-- Building: Data/SQLite
-- Building: ActiveRecord
-- Building: ActiveRecordCompiler
-- Building: Zip
-- Building: PageCompiler
-- Building: File2Page
-- Configuring done
-- Generating done
-- Build files have been written to: /root/d/poco-poco-1.12.6/ohos64build
-- Cache values
BUILD_SHARED_LIBS:BOOL=ON
CMAKE_ASM_FLAGS:STRING=
CMAKE_ASM_FLAGS_DEBUG:STRING=
CMAKE_ASM_FLAGS_RELEASE:STRING=
CMAKE_BUILD_TYPE:STRING=
CMAKE_DEBUG_POSTFIX:STRING=d
CMAKE_INSTALL_PREFIX:PATH=/usr/local
CMAKE_MINSIZEREL_POSTFIX:STRING=
CMAKE_RELEASE_POSTFIX:STRING=
CMAKE_RELWITHDEBINFO_POSTFIX:STRING=
CMAKE_TOOLCHAIN_FILE:FILEPATH=/root/d/ohos-sdk/linux/native/build/cmake/ohos.toolchain.cmake
ENABLE_ACTIVERECORD:BOOL=ON
ENABLE_ACTIVERECORD_COMPILER:BOOL=ON
ENABLE_APACHECONNECTOR:BOOL=OFF
ENABLE_CPPPARSER:BOOL=OFF
ENABLE_CRYPTO:BOOL=ON
ENABLE_DATA:BOOL=ON
ENABLE_DATA_MYSQL:BOOL=OFF
ENABLE_DATA_ODBC:BOOL=OFF
ENABLE_DATA_POSTGRESQL:BOOL=OFF
ENABLE_DATA_SQLITE:BOOL=ON
ENABLE_ENCODINGS:BOOL=ON
ENABLE_ENCODINGS_COMPILER:BOOL=OFF
ENABLE_FOUNDATION:BOOL=ON
ENABLE_JSON:BOOL=ON
ENABLE_JWT:BOOL=ON
ENABLE_MONGODB:BOOL=ON
ENABLE_NET:BOOL=ON
ENABLE_NETSSL:BOOL=ON
ENABLE_NETSSL_WIN:BOOL=OFF
ENABLE_PAGECOMPILER:BOOL=ON
ENABLE_PAGECOMPILER_FILE2PAGE:BOOL=ON
ENABLE_PDF:BOOL=OFF
ENABLE_POCODOC:BOOL=OFF
ENABLE_PROMETHEUS:BOOL=ON
ENABLE_REDIS:BOOL=ON
ENABLE_SEVENZIP:BOOL=OFF
ENABLE_TESTS:BOOL=OFF
ENABLE_UTIL:BOOL=ON
ENABLE_XML:BOOL=ON
ENABLE_ZIP:BOOL=ON
FORCE_OPENSSL:BOOL=OFF
POCO_NO_FORK_EXEC:BOOL=OFF
POCO_UNBUNDLED:BOOL=OFF
STATIC_POSTFIX:STRING=
注意:
首先,这里执行的 cmake 是 SDK 内的 cmake,不是你自己系统上原有的 cmake。包括 toolchain file 也是 SDK 内的。所以在编译 OpenSSL 库时,要将 --prefix 设置为 /ohos-sdk/linux/native/sysroot/usr
其次,cmake执行的过程中,一定要确保出现:
– Found OpenSSL: /root/d/ohos-sdk/linux/native/sysroot/usr/openssl/lib/libcrypto.so
– Building: NetSSL_OpenSSL
– Building: Crypto
ENABLE_CRYPTO:BOOL=ON
ENABLE_NETSSL:BOOL=ON文章来源:https://www.toymoban.com/news/detail-846748.html
- 编译出 Poco 库。
root@kw:~/d/poco-poco-1.12.6/ohos64build# make -j2
- 使用 file 命令,查看编译出的 elf 文件的属性。可以看到动态库和可执行程序都是 ARM aarch64 架构的。ohos cmake toolchainfile 在不设置 cpu 架构时默认为 aarch64。如果你需要编译 arm32,需要在 cmake 时加上参数 -DOHOS_ARCH=armeabi-v7a
root@kw:~/d/poco-poco-1.12.6/ohos64build/lib# ls
libPocoActiveRecord.so libPocoDataSQLite.so.96 libPocoJWT.so libPocoNetSSL.so.96 libPocoXML.so
libPocoActiveRecord.so.96 libPocoEncodings.so libPocoJWT.so.96 libPocoPrometheus.so libPocoXML.so.96
libPocoCrypto.so libPocoEncodings.so.96 libPocoMongoDB.so libPocoPrometheus.so.96 libPocoZip.so
libPocoCrypto.so.96 libPocoFoundation.so libPocoMongoDB.so.96 libPocoRedis.so libPocoZip.so.96
libPocoData.so libPocoFoundation.so.96 libPocoNet.so libPocoRedis.so.96
libPocoData.so.96 libPocoJSON.so libPocoNet.so.96 libPocoUtil.so
libPocoDataSQLite.so libPocoJSON.so.96 libPocoNetSSL.so libPocoUtil.so.96
root@kw:~/d/poco-poco-1.12.6/ohos64build/lib# file libPocoCrypto.so libPocoCrypto.so.96
libPocoCrypto.so: symbolic link to libPocoCrypto.so.96
libPocoCrypto.so.96: ELF 64-bit LSB shared object, ARM aarch64, version 1 (SYSV), dynamically linked, BuildID[sha1]=e2b34d4ba732ff18d5ed2445804f706ab063b50e, with debug_info, not stripped
四、napi 调用 Poco 库
请参考简易Native C++ 示例(ArkTS)文章来源地址https://www.toymoban.com/news/detail-846748.html
到了这里,关于鸿蒙引入 OpenSSL 库和 Poco 库(编译、调用)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!