Windows下编译使用NDK编译Boost库

1 篇文章 0 订阅
1 篇文章 0 订阅

背景

安卓开发中需要使用线程库

准备工作

  1. 下载ndk10e:地址
  2. 下载boost库:boost_1_69_0.zip
  3. 将“android-ndk-r10e-windows-x86.zip”和“boost_1_69_0.zip”解压到G盘根目录,得:“G:\boost_1_69_0\”和"G:\android-ndk-r10e"

成功编译的步骤

  1. 运行"G:\boost_1_69_0\bootstrap.bat"
  2. 替换"G:\boost_1_69_0\project-config.jam"文件的内容,可复制下文:
# define platform name of ndk
import os ;
if [ os.name ] = CYGWIN || [ os.name ] = NT
{
    androidPlatform = windows ;
}
else if [ os.name ] = LINUX
{
    androidPlatform = linux-x86_64 ;
}
else if [ os.name ] = MACOSX
{
    androidPlatform = darwin-x86 ;
}

# replace with your own path
ANDROID_NDK = "G:/android-ndk-r10e" ;

# compile with gcc, you can change compiler to clang or others
using gcc : 4.9 : $(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ :
<archiver>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ar
<ranlib>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ranlib
<compileflags>-I$(ANDROID_NDK)/platforms/android-19/arch-arm/usr/include
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.9/include
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.9/include/backward
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi-v7a/include
<compileflags>-fexceptions
<compileflags>-frtti
<compileflags>-fpic
<compileflags>-ffunction-sections
<compileflags>-funwind-tables
<compileflags>-D__ARM_ARCH_5__
<compileflags>-D__ARM_ARCH_5T__
<compileflags>-D__ARM_ARCH_5E__
<compileflags>-D__ARM_ARCH_5TE__
<compileflags>-D__ARM_ARCH_7__
<compileflags>-D__ARM_ARCH_7A__
<compileflags>-Wno-psabi
<compileflags>-march=armv7-a
<compileflags>-mtune=xscale
<compileflags>-mfloat-abi=softfp
<compileflags>-marm
<compileflags>-mthumb
<compileflags>-Os
<compileflags>-std=gnu++11
<compileflags>-fomit-frame-pointer
<compileflags>-fno-strict-aliasing
<compileflags>-finline-limit=64
<compileflags>-Wa,--noexecstack
<compileflags>-DANDROID
<compileflags>-D__ANDROID__
<compileflags>-D__ARM_EABI__
<compileflags>-DNDEBUG
<compileflags>-O2
<compileflags>-g
;

# project default compiler
project : default-build <toolset>gcc-4.9 ;

# replace with libraries you wanna to build
#libraries = --with-container --with-coroutine --with-fiber --with-graph --with-graph_parallel --with-log --with-python --with-test --with-type_erasure --with-atomic --with-date_time --with-program_options --with-chrono --with-context --with-iostreams --with-locale --with-mpi --with-serialization  --with-timer --with-wave --with-math --with-random --with-exception --with-filesystem --with-thread --with-system --with-regex --with-program_options ;

libraries = --with-thread ;
  1. 打开cmd窗口,切换当前目录到"G:\boost_1_69_0",执行命令
.\b2.exe link=static runtime-link=static target-os=linux --stagedir=android

遇到的问题

找不到g++

报错的部分内容如下:

G:/boost_1_69_0/tools/build/src/tools\gcc.jam:164: in gcc.init from module gcc
error: toolset gcc initialization:
error: provided command 'G:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.
9/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++' not found
error: initialized from project-config.jam:56

核对文件的路径发现,其真实的路径应该为"G:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.
9/prebuilt/windows/bin/arm-linux-androideabi-g++",观察“project-config.jam”发现,路径中的只有“windows”没有“-x86_64”,所以需要删除。如下图所示~~~~

参数错误

报错的部分内容如下:

Performing configuration checks

    - default address-model    : 32-bit
    - default architecture     : arm

Building the Boost C++ Libraries.


    - lockfree boost::atomic_flag : no
G:/boost_1_69_0/tools/build/src/tools\common.jam:973: in toolset-tag
*** argument error
* rule numbers.less ( n1 n2 )
* called with: ( 4 )
* missing argument n2
G:/boost_1_69_0/tools/build/src/util\numbers.jam:66:see definition of rule 'numb
ers.less' being called~~~~

根据提示,查看文件“G:/boost_1_69_0/tools/build/src/tools\common.jam”的第973行,如下图所示:猜测与gcc的版本有关系,而从参考文档1中复制的配置文件中有关于gcc的内容只有如图所示的部分:,统合错误提示,猜测与前面的android有关,删除后再次尝试,编译成功

延伸思考

  1. 配置文件中已经指定了默认的toolset,所以不用在命令行中再次指定了,同理,需要编译的library也在配置文件中指定了,也不需要在命令行中再次指定,故,相比与参考文档,第3步的命令更短、更方便。猜测可以全部放到配置文件中,命令仅.\b2.exe,应该也行~~~~

参考文档

  1. Windows下NDK直接编译编译boost 1.55(X86版本)
  2. ubuntu16.04编译boost for Android
  3. boost_for_android
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
NDK交叉编译Boost是将Boost编译成适用于Android平台的文件,以便在Android应用中使用Boost的功能。下面是一般的NDK交叉编译Boost的步骤: 1. 下载NDK:首先,你需要下载并安装Android NDK,可以从官方网站(https://developer.android.com/ndk/downloads)上获取最新版本的NDK。 2. 下载Boost:接下来,你需要下载Boost的源代码。你可以从Boost官方网站(https://www.boost.org/users/download/)上下载最新版本的Boost。 3. 配置Boost:解压下载的Boost源代码,并进入解压后的目录。在终端中执行以下命令来配置Boost: ``` ./bootstrap.sh --with-libraries=<library_names> --with-toolset=<toolset_name> --prefix=<install_path> ``` 其中,`<library_names>`是你需要编译Boost的名称,可以根据你的需求进行选择;`<toolset_name>`是你要使用编译工具链,例如`clang`或`gcc`;`<install_path>`是你希望安装Boost的路径。 4. 编辑user-config.jam文件:在Boost源代码目录下,创建一个名为`user-config.jam`的文件,并添加以下内容: ``` using clang : <ndk_version> : <path_to_ndk>/toolchains/llvm/prebuilt/<host_os>/bin/clang++ ; ``` 其中,`<ndk_version>`是你下载的NDK的版本号,`<path_to_ndk>`是你安装NDK的路径,`<host_os>`是你的操作系统类型(例如`darwin-x86_64`或`linux-x86_64`)。 5. 开始编译Boost:在终端中执行以下命令来开始编译Boost: ``` ./b2 toolset=clang-<ndk_version> target-os=android link=static threading=multi variant=release install ``` 这将使用指定的编译工具链和选项来编译Boost,并将编译结果安装到之前配置的安装路径中。 6. 完成编译:等待编译过程完成,然后你将在之前配置的安装路径中找到编译好的Boost文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值