简体   繁体   中英

How to compile boost 1.68.0 for Android using NDK r18b using Windows

I had to build a version of boost for Android using NDK r18b, I had a hardtime achieving this so I post Question+Answer here as it may help other persons.

Firstly, I tried https://github.com/moritz-wundke/Boost-for-Android , but this was unsuccessful, see How to build boost 1.69.0 for Android with NDK r18b using moritz-wundke/Boost-for-Android? .

Secondly, I tried https://github.com/dec1/Boost-for-Android , but this was unsuccessful too, see How to build boost 1.69.0 for Android with NDK r18b using dec1/Boost-for-Android? .

What are the steps to follow to achieve compilation of boost?

Actually, those scripts were designed for Linux, and they hardly work under Windows. Then, I started from scratch and could finally find the good config under Windows to achieve this. I basically checked how other libraries are compiled (I use QtCreator to deploy on Android, so the compilation window reported me how clang++ has to be invoked, so I write a user-config.jam based on that.

Here are the steps to follow to compile boost 1.68.0 for Android armeabiv7 and x86 using NDK r18b:

Then set some environment variables:

set ANDROIDNDKROOT=C:\Android\android-ndk-r18b (change this accordingly)
set NDKVER=r18b
set CLANGPATH=%ANDROIDNDKROOT%\toolchains\llvm\prebuilt\windows-x86_64\bin

Copy user-config.jam to boost folder tools/build/src :

import os ;
local AndroidNDKRoot = [ os.environ ANDROIDNDKROOT ] ;
local AndroidBinariesPath = [ os.environ CLANGPATH ] ;

using clang : armeabiv7a
:
$(AndroidBinariesPath)/clang++
:
<compileflags>-fexceptions
<compileflags>-frtti
<compileflags>-mthumb
<compileflags>-ffunction-sections
<compileflags>-funwind-tables
<compileflags>-fstack-protector-strong
<compileflags>-Wno-invalid-command-line-argument
<compileflags>-Wno-unused-command-line-argument
<compileflags>-no-canonical-prefixes
<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>-DANDROID
<compileflags>-Wa,--noexecstack
<compileflags>-Wformat
<compileflags>-Werror=format-security
<compileflags>-DNDEBUG
<compileflags>-D_REENTRANT
<compileflags>-O2
<compileflags>-gcc-toolchain
<compileflags>$(AndroidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64
<compileflags>-target
<compileflags>armv7-linux-androideabi
<compileflags>-march=armv7-a
<compileflags>-mfloat-abi=softfp
<compileflags>-mfpu=vfp
<compileflags>-fno-builtin-memmove
<compileflags>-fpic
<compileflags>-DHAVE_CONFIG_H
<compileflags>-fno-integrated-as
<compileflags>--sysroot
<compileflags>$(AndroidNDKRoot)/sysroot
<compileflags>-isystem
<compileflags>$(AndroidNDKRoot)/sysroot/usr/include/arm-linux-androideabi
<compileflags>-D__ANDROID_API__=18
<archiver>$(AndroidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/arm-linux-androideabi/bin/ar
<ranlib>$(AndroidNDKRoot)/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/arm-linux-androideabi/bin/ranlib

;

# --------------------------------------------------------------------

using clang : x86
:
$(AndroidBinariesPath)/clang++
:
<compileflags>-fexceptions
<compileflags>-frtti
<compileflags>-mthumb
<compileflags>-ffunction-sections
<compileflags>-funwind-tables
<compileflags>-fstack-protector-strong
<compileflags>-Wno-invalid-command-line-argument
<compileflags>-Wno-unused-command-line-argument
<compileflags>-no-canonical-prefixes
<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>-DANDROID
<compileflags>-Wa,--noexecstack
<compileflags>-Wformat
<compileflags>-Werror=format-security
<compileflags>-DNDEBUG
<compileflags>-D_REENTRANT
<compileflags>-O2
<compileflags>-gcc-toolchain
<compileflags>$(AndroidNDKRoot)/toolchains/x86-4.9/prebuilt/windows-x86_64
<compileflags>-target
<compileflags>i686-linux-android
<compileflags>-march=i686
<compileflags>-mfloat-abi=softfp
<compileflags>-mfpu=vfp
<compileflags>-fno-builtin-memmove
<compileflags>-fPIC
<compileflags>-mstackrealign
<compileflags>--sysroot
<compileflags>$(AndroidNDKRoot)/sysroot
<compileflags>-isystem
<compileflags>$(AndroidNDKRoot)/sysroot/usr/include/i686-linux-android
<compileflags>-D__ANDROID_API__=18
<archiver>$(AndroidNDKRoot)/toolchains/x86-4.9/prebuilt/windows-x86_64/i686-linux-android/bin/ar
<ranlib>$(AndroidNDKRoot)/toolchains/x86-4.9/prebuilt/windows-x86_64/i686-linux-android/bin/ranlib
;
  • Then, for armeabiv7-a:

    • Run bjam -q --without-math --without-context --without-coroutine --without-fiber --without-python architecture=arm --ignore-site-config -j4 target-os=android toolset=clang-armeabiv7a link=static threading=multi --layout=tagged --build-dir=build/arm/%NDKVER% --stagedir=stage_arm_%NDKVER% stage
  • Then, for x86:

    • Run bjam -q --without-math --without-context --without-coroutine --without-fiber --without-python architecture=arm --ignore-site-config -j4 target-os=android toolset=clang-armeabiv7a link=static threading=multi --layout=tagged --build-dir=build/arm/%NDKVER% --stagedir=stage_arm_%NDKVER% stage

Hopefully, the same steps can be followed to compile 1.69.0 (not tested)

Thanks for the helpful instructions.

moritz-wundke/Boost-for-Android is a great project and the original Boost-for-Android. However, the way it works, it requires constant maintenance in order to support each new combination of the ndk and boost as new versions come out (you can only build for the explicit versions the project has been configured to support). After a long time (boost version 1.53 -> 1.63, and ndk 10->16) where its seemed like the project was no longer being maintained, and my efforts to contact the maintainer were in vain, I decided to fork it, which is why dec1/Boost-for-Android came into existence.

It has the advantage that it does not, in general, need to be updated to support new versions of google's ndk, or boost. They generally build out of the box, or need rare adjustments (but I still test regularly to make sure).

Using dec1/Boost-for-Android is arguably also simpler (certainly than following your instructions above). You can build for any/all architectures (arm64-v8a, armeabi-v7a, x86, x86_64), in one go in a single line of code. And if you dont need a custom build you can simply download prebuilt binaries (boost 1.69.0, with ndk 19 and 18b, both dynamic and static are now also available) from here . There's also an example app to help you test your build.

Im pretty sure that it could be made to work on windows or mac too (possibly with less effort than you spent above), but its a lot easier to just use a virtual machine (eg the free virtualbox) to do the build, and copy the resulting binaries to windows if you really want to develop there.

Nonetheles, moritz-wundke/Boost-for-Android seems to have gotten a new influx of contributions of late, which is good news for everybody. And if you find it more suitable for building boost on windows (or any other platform) then thats great.

Good luck with your projects and thanks again for the insights.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM