Port Boost to Android

2019-03-08 21:01发布

问题:

Has somebody ported and used Boost on Android?

I've found the tool which builds boost for android (https://github.com/MysticTreeGames/Boost-for-Android), the build is successful, and i've got static boost libs. But when i'm tring to use it in simple android app:

#include <jni.h>
#include "boost/thread.hpp"

void f()
{
};

i've got a lot of compilation errors: redefinitions, undeclared etc. Seems it concerns NDK std headers. My Android.mk looks like:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

TARGET_PLATFORM := android-8

LOCAL_MODULE := Boost

LOCAL_CFLAGS := -DMYSTIC -I$(LOCAL_PATH)/boost/include/ 
LOCAL_LDLIBS := -L$(LOCAL_PATH)/external/boost/lib/

LOCAL_CPPFLAGS  := -fexceptions
LOCAL_CPPFLAGS  += -frtti
LOCAL_CPPFLAGS  += -DBOOST_THREAD_LINUX
LOCAL_CPPFLAGS  += -DBOOST_HAS_PTHREADS
LOCAL_CPPFLAGS  += -D__arm__
LOCAL_CPPFLAGS  += -D_REENTRANT
LOCAL_CPPFLAGS  += -D_GLIBCXX__PTHREADS
LOCAL_CPPFLAGS  += -DBOOST_HAS_GETTIMEOFDAY

LOCAL_SRC_FILES := main.cpp

include $(BUILD_SHARED_LIBRARY)

Also I tried to build with Crystax_NDK_r4 and Android_NDK_r5b but it hasn't resolved the problem.

Any ideas?

回答1:

I've solved the problem. I specified the incorrect path to NDK. Script patches CrystaX NDK too. So now it works!



回答2:

I just found a easy way to build boost under android NDK, which don't need patching the boost.

I don't use Android.mk to build boost, instead, I use the standalone-toolchain to build, just link CodeSourcery's toolchain.

  1. Prepare the NDK toolchain first:

    Install the NDK toolchain as a standalone toolchain. See $NDK/docs/STANDALONE-TOOLCHAIN.html

    Add the bin path of cross-toolchain to your PATH

  2. Build boost.Build tool, in Boost prj:

    ./bootstrap.sh

  3. echo "using gcc : android : arm-linux-androideabi-g++ ;" > $HOME/user-config.jam

  4. Build example

    ./b2 --prefix=$HOME/mybuild --with-thread --with-system toolset=gcc-android threading=multi link=static install

I hope these can help you.



回答3:

You can download a collection of scripts that will download and build some popular c/c++ open source libraries for android and ios including boost at this location.

https://github.com/mevansam/cmoss