打算将PC下C++代码移植到到Android平台下,由于代码全部是基于boost进行的开发,所以首先需要将boost使用Android编译器进行编译;
移植之前使用的boost版本为1.58,NDK使用的是r10d版本,之前已使用r10d成功编译ACE、webrtc,ffmpeg等开源代码库;
编译过程大致与window/Linux下相当,只是需要将编译器设置为arm 编译器;
第一步:运行bootstrap.bat,生成b2.exe;
第二步:修改根目录下的project-config.jam内容,第一步之后,其默认为:
import option ;
using msvc ;
option.set keep-going : false ;
修改为:
import os ;
androidNDKRoot = D:/android-ndk-r13b ; # put the relevant path
using gcc : android :
$(androidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows/bin/arm-linux-androideabi-g++ :
<compileflags>--sysroot=$(androidNDKRoot)/platforms/android-21/arch-arm
<compileflags>-mthumb
<compileflags>-Os
<compileflags>-fno-strict-aliasing
<compileflags>-O2
<compileflags>-DNDEBUG
<compileflags>-g
<compileflags>-lstdc++
<compileflags>-I$(androidNDKRoot)/sources/cxx-stl/gnu-libstdc++/4.9/include
<compileflags>-I$(androidNDKRoot)/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include
<compileflags>-D__GLIBC__
<compileflags>-D_GLIBCXX__PTHREADS
<compileflags>-D__arm__
<compileflags>-D_REENTRANT
<archiver>$(androidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows/bin/arm-linux-androideabi-ar
<ranlib>$(androidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows/bin/arm-linux-androideabi-ranlib
注意:网上很多人说修改<boost>/tools/build/v2/user-config.jam文件,但是起码boost-1.58是没有V2这个文件夹的,这里修改根目录下的第二步:修改根目录下的project-config.jam;
由于使用较旧版本的NDK工具链去编译较新版本的boost,会导致编译器崩溃,所以升级NDK版本为android-ndk-r13b,同时脚本中指定c++编译器版本为4.9,Android API版本为21;
第三步:就是调用b2进行编译了
b2 --without-python --without-context --without-coroutine --without-test threading=multi link=static runtime-link=static toolset=gcc-android target-os=linux threadapi=pthread --stagedir=android stage
注:参数 --without-context --without-coroutine解决编译过程中的错误: libs\context\src\unsupported.cpp:7:2: error: #error "platform not supported",其实也可忽略;
--stagedir=android 指定安装库文件目录为根目录下的Android文件夹;
stage 只安装已完成编译的库文件到android目录下下;
如果需要指定安装目录,以方便合作开发,可以使用--prefix=指定安装目录 install,如--prefix=dev install,根目录下dev目录,则b2会自动将头文件和库文件拷贝到此目录,
相对整个boost来说也算是轻量级的了,哈哈,不过拷贝文件会比较耗时;