Windows下編譯使用NDK編譯Boost庫

背景

安卓開發中需要使用線程庫

準備工作

  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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章