Windows下编译使用NDK编译Boost库"/>
Windows下编译使用NDK编译Boost库
背景
安卓开发中需要使用线程库
准备工作
- 下载ndk10e:地址
- 下载boost库:boost_1_69_0.zip
- 将“android-ndk-r10e-windows-x86.zip”和“boost_1_69_0.zip”解压到G盘根目录,得:“G:\boost_1_69_0\”和"G:\android-ndk-r10e"
成功编译的步骤
- 运行"G:\boost_1_69_0\bootstrap.bat"
- 替换"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 ;
- 打开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 : armBuilding 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有关,删除后再次尝试,编译成功
延伸思考
- 配置文件中已经指定了默认的toolset,所以不用在命令行中再次指定了,同理,需要编译的library也在配置文件中指定了,也不需要在命令行中再次指定,故,相比与参考文档,第3步的命令更短、更方便。猜测可以全部放到配置文件中,命令仅
.\b2.exe
,应该也行~~~~
参考文档
- Windows下NDK直接编译编译boost 1.55(X86版本)
- ubuntu16.04编译boost for Android
- boost_for_android
更多推荐
Windows下编译使用NDK编译Boost库
发布评论