corner stamp here.

Tips & Tricks: Building Boost with NDK R5

Boost is almost composed of template and header files. Thus, nothing needs to be built at all most of the time… just include the necessary header files. This is true for smart pointers for example. But a few features of Boost are available through compiled libraries, like the threading module. Let’s see how to do that with the NDK R5!

First, I would like to point out that this solution is based on a discussion from Google Group. To compile boost, do the following (I do it on Ubuntu but this should be similar on Windows with Cygwin):

  • The first thing is to download Boost. I am using Boost v1.46.1 in the present post.
  • Then untar the archive into your $ANDROID_NDK/sources directory. You should end up with the following
> ls -al $ANDROID_NDK/sources/boost
boost    doc    libs    more    stage    status    tools ...
  • When uncompressed, open the file “user-config.jam” located in “$ANDROID_NDK/sources/boost/tools/build/v2″. BJam is a custom build tool more or less like make or ant and which is used to build boost. The file user-config.jam is, like its name indicates it, a custom configuration file that can be set-up by Boost users before compilation. Update user-config.jam with the following content:
import os ;

if [ os.name ] = CYGWIN || [ os.name ] = NT
{
    androidPlatform = windows ;
}
else if [ os.name ] = LINUX
{
    androidPlatform = linux-x86 ;
}
else if [ os.name ] = MACOSX
{
    androidPlatform = darwin-x86 ;
}
modules.poke : NO_BZIP2 : 1 ;
ANDROID_NDK = ../.. ;
using gcc : android4.4.3 :
$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ :
<compileflags>--sysroot=$(ANDROID_NDK)/platforms/android-9/arch-arm
<compileflags>-mthumb
<compileflags>-Os
<compileflags>-fno-strict-aliasing
<compileflags>-O2
<compileflags>-DNDEBUG
<compileflags>-g
<compileflags>-lstdc++
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/include
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include
<compileflags>-D__GLIBC__
<compileflags>-DBOOST_NO_INTRINSIC_WCHAR_T
<compileflags>-DBOOST_FILESYSTEM_VERSION=2
<archiver>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ar
<ranlib>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ranlib
;
  • Once this is done, launch compilation using the following command line. We need to exclude a few modules which are not working with NDK like the “serialization” module. Indeed NDK does not support wide chars which are required for it.  We don’t compile python which requires additional python libs:
bjam --without-python --without-serialization toolset=gcc-android4.4.3 link=static runtime-link=static target-os=linux --stagedir=android
  • The biggest step has been performed. No error should appear on screen after compilation (look for “…failed updating X targets…” message). Now, how to include it in our own project? One of the best solution is to make use of the new import-module feature of NDK R5.
  • Create an Android.mk file in $ANDROID_NDK/sources/boost. It needs to contain one module declaration per library. Variable LOCAL_EXPORT_C_INCLUDES is important as it is the one which automatically append boost directory to include file directories. Here, we declared two static libs Boost thread and iostreams. All available libraries can be found in $ANDROID_NDK/sources/boost/android/lib:
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_thread
LOCAL_SRC_FILES:= android/lib/libboost_thread.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE:= boost_iostreams
LOCAL_SRC_FILES:= android/lib/libboost_iostreams.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
  • To include Boost in an application, we need to link with a STL which supports exception. Hopefully, we don’t need to build one manually anymore as NDK R5 now provides STLport and GNU STL. However, only GNU STL support exceptions right now. But on the opposite to STLport, only static linking is supported. So to make use of GNU STL, just open (or create) your Application.mk file and add (or complete) the two following lines:
...
APP_STL      = gnustl_static
APP_CPPFLAGS = -fexceptions
  • Finally, open your Application (not Boost one) Android.mk file, include the boost module and call import-module function:
...
LOCAL_STATIC_LIBRARIES := ... boost_thread ...
...
$(call import-module,boost)

That’s it! Just include the headers, write your code with boost and run it!

boost::thread bgThread(...);
...
bgThread.join();

That’s it! Now enjoy the (almost) full power of Boost…

  1. null

    thanks for good posting.
    but i have a problem below.
    what’s wrong with this?

    $ ndk-build
    Android NDK: Trying to define local module ‘boost_iostreams’ in /cygdrive/d/dev/
    android/android-ndk-r6/sources/boost/Android.mk.
    Android NDK: But this module was already defined by /cygdrive/d/dev/android/andr
    oid-ndk-r6/sources/boost/Android.mk.
    /cygdrive/d/dev/android/android-ndk-r6/build/core/build-module.mk:34: *** Androi
    d NDK: Aborting. . Stop.

  2. www-admin

    It’s hard to say without more information. I compiled it with NDK R5, so give it a try to make sure this problem isn’t due to an evolution in the NDK… Just make sure you include the module properly in your “application” Android.mk file and that it is correctly defined in “boost” Android.mk file.

  3. Mark S

    Thanks so much for this! Worked like a charm for the libraries I needed on android-r6b. I did make a few changes, however. For example, I compiled against the android-3 platform, for example, to have greater compatibility.

  4. jon

    I’m using Crystax r6 so I needed to add another include dir to user-config.jam, points to /sources/crystax/include otherwise build complains about the missing wchar.h header.

    Cheers.

  5. Include Boost C++ library in android | SeekPHP.com

    [...] 1 everybody, I have been trying to marry Boost and android on windows for long time and tried lot of approaches but still no luck. I want to make a sample program using Boost library in android. I am following this tutorial here. [...]

  6. Jacob Juul Kolding

    I have just follow these instruction and got BOOST working with ndk7 execpt for lib UUID which uses the wcslen.

    This is the error I get:

    jni/OME/src/../../../../android-ndk-r7b/sources/boost/boost/uuid/string_generator.hpp:52: error: ‘wcslen’ is not a member of ‘std’
    In file included from jni/OME/src/../../../../android-ndk-r7b/sources/boost/boost/uuid/uuid_generators.hpp:16,

    Do you know of any solution?

    Best Regards
    Jacob

  7. Alexandre

    Thank you for this tutorial !

  8. Antoine

    Hi,

    I’m trying to follow the similar process described in the Android NDK Beginner’s guide (which only differs with the use of b2 instead of bjam)

    I turn out to have errors I don’t understand :
    I am on windows, using either cmd or cygwin (provided through Tegra Android Development Pack) and when compiling boost I have these errors : http://pastebin.com/AHrReC4a

    Has anybody achieved compilation for android on windows ?

    Thanks

  9. www-admin

    Hi Antoine, sorry for the late reply.
    Beware bjam is older than b2 and *will not* work properly with latest boost releases. Prefer building and using b2. or you will run into trouble. I am planning to reinstall Windows 7 soon so maybe I’ll give it a try when I have time. It’s likely there are some breaking changes in Boost 1.49. Note that depending on what you need, you may be able to disable some boost modules or just stick to previous version used in the book.
    Hope that helps.

  10. ksyusha

    I am getting an error trying to build it on mac:
    boost/tools/build/v2/tools/gcc.jam:107: in gcc.init from module gcc
    error: toolset gcc initialization:
    error: version ‘android4.4.3′ requested but ‘g++-android4.4.3′ not found and version ’4.2.1′ of default ‘g++’ does not match

    I’m not sure why it tries to look up “g++-android4.4.3″ or this default of “4.2.1″ is set. I’ve check the path $(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ and it’s fine, the compiler is there. Can anyone advise?

  11. dd

    hi..

    is there a way to build the asio lib too?
    (trying to make this work with ndk-r8 and boos 1.49)

    it worked for some libs:
    …failed updating 8 targets…
    …skipped 3 targets…
    …updated 392 targets…

    thx in advance.

  12. ksyusha

    @dd “is there a way to build the asio lib too?”
    Boost Asio itself is also all headers but it has dependencies on other libs that have to be built, System at a minimum: http://www.boost.org/doc/libs/1_51_0/doc/html/boost_asio/using.html.

    You can build the libs you need with boost tools, eg: ./b2 -q -d+2 toolset=gcc-android4.6 link=static runtime-link=static target-os=linux –stagedir=_android –with-system –with-chrono –with-date_time –with-regex –with-signals –with-thread

    I myself had linking problems when using boost build. Probably some options but I just switched to building Boost with a custom Android.mk, there is not that many files to list. Asio works fine for me on Android. You may need to tweak compile flags in Android.mk

  13. dd

    @ksyusha:

    thx.. you’re right..

    looks like its almost working but it seems to have a problem with read_some oder buffer:
    unsigned int x;
    socket.read_some( boost::asio::buffer (&x, sizeof (x)));

    is that what youi meant with linking problems? how can i build it with a custom mk?

    thx

  14. Sanjeev

    Hi, I am getting following errors while building android_hello_boost-master :
    /android-ndk-r8/sources/android_hello_boost-master# ../../ndk-build
    Gdbserver : [arm-linux-androideabi-4.4.3] libs/armeabi/gdbserver
    Gdbsetup : libs/armeabi/gdb.setup
    Compile++ thumb : hello-boost <= first.cpp
    In file included from /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options/value_semantic.hpp:12,
    from /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options/options_description.hpp:13,
    from /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options.hpp:15,
    from jni/first.cpp:38:
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/any.hpp: In member function 'const std::type_info& boost::any::type() const':
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/any.hpp:90: error: cannot use typeid with -fno-rtti
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/any.hpp: In member function 'virtual const std::type_info& boost::any::holder::type() const’:
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/any.hpp:129: error: cannot use typeid with -fno-rtti
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/any.hpp: In function ‘ValueType* boost::any_cast(boost::any*)’:
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/any.hpp:180: error: cannot use typeid with -fno-rtti
    In file included from /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options/options_description.hpp:13,
    from /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options.hpp:15,
    from jni/first.cpp:38:
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options/value_semantic.hpp: In member function ‘const std::type_info& boost::program_options::typed_value::value_type() const’:
    /home/Software/android-ndk/android-ndk-r8/sources/boost/boost/program_options/value_semantic.hpp:340: error: cannot use typeid with -fno-rtti
    make: *** [obj/local/armeabi/objs-debug/hello-boost/first.o] Error 1

    so please guide me in this issue.

  15. www-admin

    By default, the Android NDK compiles with the “-fno-rtti” flag, i.e. no Run Time Type Information (the typeof keyword).
    So I think this should be solve by happening the “-frtti” to your compiler flags.

  16. Sanjeev

    Thanks for your quick response. I used this option “-frtti” and its working fine :).

    I have one more issue. As per my current requirement I need libboost_filesystem.a library, and for the same i used following options
    /home/Software/android-ndk/android-ndk-r8/sources/boost# ./bjam –with-filesystem toolset=gcc-android4.4.3 link=static runtime-link=static target-os=linux –stagedir=android

    and got following error:

    …failed gcc.compile.c++ bin.v2/libs/filesystem/build/gcc-android4.4.3/release/link-static/runtime-link-static/threading-multi/v3/src/unique_path.o…
    …skipped libboost_filesystem.a(clean) for lack of v2/src/v2_operations.o…
    …skipped libboost_filesystem.a for lack of v2/src/v2_operations.o…
    …skipped libboost_filesystem.a for lack of libboost_filesystem.a…

    and because of which libboost_filesystem.a not being created.

    So, please guide what option should I use ? so that I can create this particular library.

  17. anguila

    FYI! If you are running under Eclipse and you’re building the ndk side from ndk-build, it’s necessary to add this path “${NDKROOT}/../usr/include” into project properties -> C/C++ General -> Path and symbols ->Includes
    ator
    Although the ndk building its working OK, the eclipse can’t resolve the libraries so you have to add it to the project compiler path.

    I’m working on windows and I compiled boost in cygwin, and finally after hours of many headeaches, I can proudly say.. It works!! :)

    I hope this info could help someone..

  18. ja chan

    i encountered follows errors, i don’t know the reason:

    common.copy ../build/include/boost-1_49/boost/tr1/tr1/algorithm
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/array
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/bitset
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/cmath
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/complex
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/deque
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/exception
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/fstream
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/functional
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/iomanip
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/ios
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/iostream
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/istream
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/iterator
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/limits
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/list
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/locale
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/map
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/memory
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/new
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/numeric
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/ostream
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/queue
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/random
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/regex
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/set
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/sstream
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/stack
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/stdexcept
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/streambuf
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/string
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/strstream
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/tuple
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/typeinfo
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/type_traits
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/unordered_map
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/unordered_set
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/utility
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/valarray
    common.copy ../build/include/boost-1_49/boost/tr1/tr1/vector
    …failed updating 54 targets…
    …skipped 24 targets…
    …updated 9809 targets…
    Done!

  19. Manolis

    Trying to build boost 1_46 on cygwin, but when i run bjam i get the following error

    cc1plus.exe: error: unrecognized command line option “-mthreads”

  20. Leonid

    Did you try to build boost 1_50? Specifically, I have errors on filesystem lib build. Using of filesystem V2 deprecated since 1_48. but V3 needs wstring As I know gnustl doesn’t have wchar_t. Any idea?

  21. Include Boost C++ library in android | BlogoSfera

    [...] 1 everybody, I have been trying to marry Boost and android on windows for long time and tried lot of approaches but still no luck. I want to make a sample program using Boost library in android. I am following this tutorial here. [...]

  22. Dmitry

    I’m trying to build my app by using Bjam & Boost.Build for Android, the scripts works fine for Windows and Linux, when I’m building for Android all the modules are also built, but I can’t make linker to work properly. Linker tells that it can’t find all the libraries, I see many unresolved external symbols from stl and runtime libraries.

Leave a Reply