linux下编译鸿蒙版boost库

发布于:2024-10-17 ⋅ 阅读:(13) ⋅ 点赞:(0)

我在上一篇文章中介绍了curl和openssl的编译方式(linux下编译鸿蒙版curl、openssl-CSDN博客),这篇再介绍一下boost库的编译。

一.环境准备

1.鸿蒙NDK

下载安装方式可以参考上篇文章,完毕后NDK的路径为:/home/ubuntu/develop/ohos-sdk-5.0/linux

2.下载boost库

我们可以在 Boost Version History查找自己需要的版本下载,我这边下载的是2020年的版本1.75.0。

下载完毕后解压到开发目录:/home/ubuntu/thirdparty/boost_1_75_0

二.编译流程

1.生成boost编译工具

在boost目录下执行 bootstrap.sh,编译完成后生成编译工具 b2,以及要用到的 project-config.jam

2.修改工程编译配置

vim project-config.jam

将配置文件内容清空,替换成如下配置:

modules.poke : NO_BZIP2 : 1 ;
# 设置ndk的路径
OHOS_NDK = /home/ubuntu/develop/ohos-sdk-5.0/linux ;
# 设置编译参数
using clang : ohos5.0 :
    $(OHOS_NDK)/native/llvm/bin/clang++ :
    <archiver>$(OHOS_NDK)/native/llvm/bin/llvm-ar
    <ranlib>$(OHOS_NDK)/native/llvm/bin/llvm-ranlib
    <compileflags>--sysroot=$(OHOS_NDK)/native/sysroot
    <compileflags>--target=aarch64-linux-ohos
    <compileflags>-fdata-sections
    <compileflags>-ffunction-sections
    <compileflags>-funwind-tables
    <compileflags>-fstack-protector-strong
    <compileflags>-no-canonical-prefixes
    <compileflags>-fno-addrsig
    <compileflags>-Wa,--noexecstack
    <compileflags>-D__MUSL__ ;

project : default-build <toolset>clang-ohos5.0 ;

# List of --with-<library> and --without-<library> options.
# If left empty, all libraries will be built.
# Options specified on the command line completely override this variable.
# 这边可以配置需要编译哪些库,不编译哪些库,我这边注释掉之后,就会编译所有库
#libraries = --with-filesystem --with-thread --with-system --with-regex --with-program_options --with-timer --with-chrono --with-iostreams --with-exception ;

这块配置是编译成功与否的关键,大家也可以自行增删需要的编译选项

3.修改 context 库编译配置

vim libs/context/build/Jamfile.v2

添加新的配置内容:

alias asm_sources
   : asm/make_arm64_aapcs_elf_gas.S
     asm/jump_arm64_aapcs_elf_gas.S
     asm/ontop_arm64_aapcs_elf_gas.S
   : <abi>sysv
     <address-model>64
     <architecture>arm
     <binary-format>elf
     <toolset>clang-ohos
   ;

位置如图所示:

这边需要注意下,如果不修改该配置的话,在编译时会报 error: No best alternative for libs/context/build/asm_sources 的错误。

另外还有一种方式可以解决这个错误,在不修改配置的情况下,我们可以在 b2 的编译命令行中加入编译参数 abi=aapcs

./b2 abi=aapcs ...

4.编译

./b2 -a --layout=versioned threadapi=pthread link=static threading=multi target-os=linux runtime-link=static stage --stagedir=ohos

我这边编译的是多线程静态库,大家可以根据需要自行调整编译参数,参数说明可以参考 ./b2 --help

三.编译结果

编译结束后,结果生成在 ohos/lib 下。其中有些库可能会编译失败,如 boost_python,关于这些特殊库的编译大家可以自行查找资料,我这边就不介绍了。