Intelligent Assistant
Chat with our virtual assistant to get answers promptly.
With the continuous advancement of technologies, the requirements for mobile app development are escalating. In conventional mobile app development, relatively complex configuration and environment setup are required, which inflates development costs. To solve this problem, the lycium tool can be used to help you simplify the development process and reduce the development time.
lycium is a compilation framework tool that helps you quickly cross-compile C/C++ third-party libraries using the shell language and quickly verify the compilation on HarmonyOS.
You only need to set the compilation mode and parameters for the corresponding C/C++ third-party library, and then use lycium to quickly compile binary files that can run on HarmonyOS.
This section uses OpenSSL as an example to describe how to use the lycium tool to quickly compile a third-party library.
This section describes how to use the lycium tool to quickly compile the OpenSSL third-party library source code using the OpenHarmony SDK in the Linux environment.
- owner@ubuntu:/mnt/e$ export OHOS_SDK=/xxx/ohos-sdk/linux # Set the SDK path to your own SDK decompression directory.
To simplify command configuration during development, several compilation commands are integrated for the Arm and AArch64 architectures and stored in the lycium/Buildtools directory. Before using the lycium tool, you need to copy the compilation commands to the corresponding directory of the SDK. The procedure is as follows:
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master$ cd lycium/Buildtools #: Go to the tool package directory.
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium/Buildtools$ sha512sum -c SHA512SUM # Check whether the tool package is normal. If "toolchain.tar.gz: OK" is displayed, the tool package is normal. Otherwise, the tool package is abnormal and needs to be downloaded again.
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium/Buildtools$ tar -zxvf toolchain.tar.gz # Decompress and copy the build tools.
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium/Buildtools$ cp toolchain/* ${OHOS_SDK}/native/llvm/bin # Copy the command to the native/llvm/bin directory of the toolchain.
The lycium framework provides the HPKBUILD file for you to build and configure the corresponding C/C++ third-party library.
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/thirdparty$ mkdir openssl
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/thirdparty$ cp /xxx/tpc_c_cplusplus-master/lycium/template/HPKBUILD openssl
- pkgname=NAME # Database name (Required)
- pkgver=VERSION # Library version (Required)
- source="https://downloads.sourceforge.net/$pkgname/$pkgname-$pkgver.tar.gz" # Source code download link of the library (required)
- buildtools= # (Required) Build tools. Currently, the cmake, configure, and make tools are supported. Use a tool based on the compilation and building method of the third-party library.
- builddir= # (Required) Directory name after the source package is decompressed.
- # Omit some configuration options.
- # Prepare for the build, such as setting environment variables and creating build directories.
- prepare() {
- }
-
- # Run the build command.
- build() {
- }
-
- # Installation and packaging
- package() {
- }
-
- # Check
- check() {
- }
-
- # Clear the environment.
- cleanbuild() {
- }
Each build script must define the corresponding variables and five functions based on the preceding rules. The variables are required and must be correctly set based on the library information. Otherwise, the build fails.
The following is an example:
The OpenSSL compilation and build mode is configure. For configure cross compilation, the host type and corresponding environment variables need to be configured. The framework integrates the environment variable setting API, which is encapsulated in envset.sh. Therefore, in addition to basic information, you also need to define a host variable and import the envset.sh file. The basic variable configuration is as follows:
- pkgname=openssl # Library name
- pkgver=OpenSSL_1_1_1u # Version number of the library
- source="https://github.com/openssl/$pkgname/archive/refs/tags/$pkgver.zip" # Path of the source package of the library
- archs=("armeabi-v7a" "arm64-v8a") # Architecture information
- buildtools="configure" # The build mode is configure.
- builddir=$pkgname-${pkgver} # Name of the folder generated after the OpenSSL source package is decompressed.
- packagename=$builddir.zip # Package name
- source envset.sh # Execute envset.sh, the environment setup script, which (located at tpc_c_cplusplus/lycium/script) typically defines variables and functions required for the build process.
- host= # Define the host variable.
Create a build directory in the prepare() function and configure the environment variables of the corresponding architecture.
- prepare() {
- mkdir -p $builddir/$ARCH-build
- if [ $ARCH == ${archs[0]} ] # Check if the environment variable $ARCH (representing the target CPU architecture) matches the first element of the archs array.
- then
- setarm32ENV
- host=linux-generic32
- elif [ $ARCH == ${archs[1]} ]
- then
- setarm64ENV
- host=linux-aarch64
- else
- echo "${ARCH} not support"
- return -1
- fi
- }
The build() function uses the configure command to generate Makefile and execute the make command.
- build() {
- cd $builddir/$ARCH-build
- ../Configure $* $host > `pwd`/build.log 2>&1
- make -j4 >> `pwd`/build.log 2>&1
- ret=$?
- cd $OLDPWD
- return $ret
- }
During the OpenSSL test, test cases need to be generated by compiling the target depend. Therefore, the corresponding check() function needs to be modified. Run the make depend command in the check() function, clear the corresponding environment variables after the execution, and use comments to describe the test method of the library on the device.
- check() {
- cd $builddir/$ARCH-build
- make depend >> `pwd`/build.log 2>&1
- cd $OLDPWD
- if [ $ARCH == ${archs[0]} ]
- then
- unsetarm32ENV
- fi
- if [ $ARCH == ${archs[1]} ]
- then
- unsetarm64ENV
- fi
- unset host
- echo "Test must be on HarmonyOS device!"
- # real test CMD
- # Add the compilation directory to the LD_LIBRARY_PATH environment variable.
- # make test
- }
For the package() and cleanbuild() functions, use the default settings in the template.
After the compilation mode parameters of the third-party library are configured, run the ./build.sh openssl command (openssl is the name of the created directory) in the lycium directory to automatically compile the third-party library, package the library, and install it in the usr/pkgname/ARCH directory of the current directory (pkgname indicates the third-party library name, and ARCH indicates the architecture name).
- owner@ubuntu:/mnt/e/tpc_c_cplusplus-master/lycium$ ./build.sh openssl # Compile the libraries in the thirdparty directory by default.
- Build OS linux
- OHOS_SDK=/mnt/e/ohos-sdk/linux
- CLANG_VERSION=15.0.4
- Build openssl OpenSSL_1_1_1u start!
- % Total % Received % Xferd Average Speed Time Time Time Current
- Dload Upload Total Spent Left Speed
- 100 222 0 222 0 0 457 0 --:--:-- --:--:-- --:--:-- 456
- 100 11.3M 0 11.3M 0 0 958k 0 --:--:-- 0:00:12 --:--:-- 1802k
- Compile HarmonyOS armeabi-v7a openssl OpenSSL_1_1_1u libs...
- # Omit some compilation information.
- ALL JOBS DONE!!!
If no error is reported and the log prints "ALL JOBS DONE!!!", the third-party library is compiled successfully.
After the compilation is successful, go to the lycium/usr directory to view the generated file.
To better manage the third-party libraries integrated into your app, create a thirdparty directory in the cpp directory of your app project and copy the generated binary files and header files to this directory.
As shown in the following figure, xxx indicates the name of the third-party library. The xxx folder contains binary files generated in the AArch64 and Arm architectures. Each architecture directory contains the header file directory include and binary file directory lib of the library.

If the binary file of the third-party library is a .so file, copy the .so file to the entry/libs/${OHOS_ARCH}/ directory in the project directory, as shown in the following figure.

Precautions for referencing dynamic libraries:

Copy method: Copy the .so file to the Windows OS without compression, or compress the .so file into a .zip file and copy it to the Windows OS. After the .so file is copied correctly, the size of the .so file should be the same as that of the entity file in the original library.
If the .so file is copied to the Windows OS in .tar, .gz, .7z, or .bzip2 format and then decompressed, the file is a soft link of the entity library. The size of the file is different from that of the entity library. As a result, the file cannot be used.
To configure links, you only need to add the corresponding target_link_libraries to the CMakeLists.txt file in the cpp directory. (You only need to enter one of the dynamic library link and static library link.)
- target_link_libraries(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/xxx/${OHOS_ARCH}/lib/libxxx.a)
- target_link_libraries(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/xxx/${OHOS_ARCH}/lib/libxxx.so)

Add target_include_directories to the CMakeLists.txt file in the cpp directory.
- target_include_directories(entry PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/thirdparty/xxx/${OHOS_ARCH}/include)
