Building Boost for Android from Windows

Posted on Sat 12 January 2019 in Review

Here are some up to date instructions on building static boost libraries for Android from Windows.

Step 1

Run bootstrap.bat and add the following to project-config.jam. Note androidNDKRoot points to where I've installed the NDK Bundle.

androidNDKRhoot = /Craftwork/Android/sdk/ndk-bundle ;

using clang : arm64 
:
    $(androidNDKRoot)/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe  
:
    <compileflags>--sysroot=$(androidNDKRoot)/sysroot 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++/include 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++abi/include 
    <compileflags>-I$(androidNDKRoot)/sources/android/support/include 
    <compileflags>-I$(androidNDKRoot)/sysroot/usr/include/aarch64-linux-android 
    <compileflags>-g 
    <compileflags>-O3
    <cxxflags>-std=c++14
    <compileflags>--target=aarch64-arm-linux-androideabi 
;

That following example just is for Arm64, see the end for Arm, x68 and x86_64.

Step 2

Add

C:\Craftwork\Android\sdk\ndk-bundle\toolchains\aarch64-linux-android-4.9\prebuilt\windows-x86_64\aarch64-linux-android\bin

to PATH. This is to workaround [Boost-build] How to make clang toolset use custom archiver and ranlib from user-config.jam? - "ar and ranlib cannot be customized for clang toolset".

Step 3

Run b2 target-os=android toolset=clang-arm64 link=static --with-system

Note --with-system is just for quicker testing, leave it out to build all libraries. Personally I use the following build options.

b2 target-os=android toolset=clang-arm64 link=static runtime-link=shared threading=multi -j6 stage --stagedir=libs-android-ndk

A problem at the moment

This process works fine for 1.68 and many earlier versions. But with 1.69 I get the following error before compilation starts.

C:\Craftwork\boost_1_69_0>b2 target-os=android toolset=clang-arm64 link=static --with-system
Performing configuration checks

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

Building the Boost C++ Libraries.


C:/Craftwork/boost_1_69_0/tools/build/src/tools\common.jam:979: in toolset-tag
*** argument error
* rule numbers.less ( n1 n2 )
* called with: ( 3 )
* missing argument n2
C:/Craftwork/boost_1_69_0/tools/build/src/util\numbers.jam:66:see definition of rule 'numbers.less' being called
C:/Craftwork/boost_1_69_0/tools/build/src/tools\common.jam:850: in common.format-name
C:/Craftwork/boost_1_69_0\boostcpp.jam:188: in boostcpp.tag
Jamroot:191: in Jamfile<C:\Craftwork\boost_1_69_0>.tag

[more lines omitted]

Thankfully there is a temporary working around. Open the mentioned file boost_1_69_0/tools/build/src/tools\common.jam and starting at line 979, comment out the 4 line if block like this

    # Ditto, from Clang 4
#    if $(tag) in clang clangw && [ numbers.less 3 $(version[1]) ]
#    {
#        version = $(version[1]) ;
#    }

Then things should compile. Hopefully this won't be necessary with the nest release.

Other architectures

Arm

using clang : arm 
:
    $(standaloneToolchains)/bin/clang++.exe 
:
    <compileflags>--sysroot=$(androidNDKRoot)/sysroot 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++/include 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++abi/include 
    <compileflags>-I$(androidNDKRoot)/sources/android/support/include 
    <compileflags>-I$(androidNDKRoot)/sysroot/usr/include/arm-linux-androideabi
    <compileflags>-g 
    <compileflags>-Os 
    <compileflags>-O3
    <cxxflags>-std=c++14
    <compileflags>--target=armv7a-none-linux-androideabi 
;

x86

using clang : x86 
:
    $(androidNDKRoot)/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe  
:
    <compileflags>--sysroot=$(androidNDKRoot)/sysroot 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++/include 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++abi/include 
    <compileflags>-I$(androidNDKRoot)/sources/android/support/include 
    <compileflags>-I$(androidNDKRoot)/sysroot/usr/include/i686-linux-android 
    <compileflags>-g 
    <compileflags>-O3
    <cxxflags>-std=c++14
    <compileflags>--target=i686-none-linux-androideabi 
;

x86_64

using clang : x64 
:
    $(androidNDKRoot)/toolchains/llvm/prebuilt/windows-x86_64/bin/clang++.exe  
:
    <compileflags>--sysroot=$(androidNDKRoot)/sysroot 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++/include 
    <compileflags>-I$(androidNDKRoot)/sources/cxx-stl/llvm-libc++abi/include 
    <compileflags>-I$(androidNDKRoot)/sources/android/support/include 
    <compileflags>-I$(androidNDKRoot)/sysroot/usr/include/i686-linux-android 
    <compileflags>-g 
    <compileflags>-O3
    <compileflags>-fPIC
    <cxxflags>-std=c++14
    <compileflags>--target=x86_64-none-linux-androideabi 
;