22

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.

As this tutorial suggested i have kept my Boost lib in ****(Android NDK)\sources\boost_1_44_0**** compiled it successfully.

Then i made an Android.mk file inside sources/boost_1_44_0 and made the entry of each library which i want to use. In this case lib. file is libboost_date_time-gcc-mt-s-1_44.a available in boost_1_44_0/android/lib/
Here is the content of Android.mk file.

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_date
LOCAL_SRC_FILES:= boost_1_44_0/android/lib/libboost_date_time-gcc-mt-s-1_44.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY) 

Now the next step is to make an Android.mk file in my project directory, inside jni folder.(this is to create a shared library.). Here are its contents.

LOCAL_PATH := $(call my-dir)
 include $(call all-subdir-makefiles)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_LDLIBS := -llog -ldl
LOCAL_MODULE    := ndkfoo
LOCAL_SRC_FILES := ndkfoo.cpp
LOCAL_STATIC_LIBRARIES := boost_date
include $(BUILD_SHARED_LIBRARY)
$(call import-module,boost_1_44_0) 

Here is the Application.mk file placed on the same location, inside jni folder. Contents of Application.mk file are as follows:

APP_STL      = gnustl_static #(or APP_STL = stlport_static as required)
APP_CPPFLAGS = -fexceptions  

And finally here is my ndkfoo.cpp file

#include <string.h>
#include <jni.h>
#include <stdio.h>
#include <boost/date_time.hpp>

using namespace boost::gregorian;

void Java_com_ndkfoo_NdkfooActivity_invokeNativeFunction(JNIEnv* env, jobject javaThis) {
    date weekstart(2002,Feb,1);

}

this program might be incorrect but the problem is that it does not recognize any boost headers or function. and i always get compilation error.

Is there something i am missing or doing incorrectly? Any help would be really appreciated.

EDIT: This question contains everything you would need to include Boost library in android. For more tips look at my answer below. Hopefully this would also work for you.

Thanks.

Girish Nair
  • 5,148
  • 5
  • 40
  • 61
N-JOY
  • 10,344
  • 7
  • 51
  • 69
  • 1
    For one thing: your path is slightly different: in the tutorial the author has extracted to $ANDROID_NDK/sources/boost. Not sure if that makes a difference. The author's path to the lib begins with *android/..*, yours with *boost_1_44_0/android/..*. I would first try follow the tutorial exactly and see if that resolves your issues. – Ralf Oct 25 '11 at 06:48
  • ya true, but i think it might not make any difference. – N-JOY Oct 25 '11 at 06:53
  • knock knock anybody's there? please help me..... – N-JOY Oct 25 '11 at 09:21
  • 1
    Have you tried to use "ndk-build V=1" to see if the includes are right? That log might gives us more clues. – javier-sanz Oct 31 '11 at 15:10
  • hi! @paul did you try to run bjam or b2. it is a tool to compile boost. look at this link. http://www.codexperiments.com/android/2011/05/tips-tricks-building-boost-with-ndk-r5/ i have already mentioned this link in my post. – N-JOY Nov 25 '11 at 08:54
  • @N-JOY Yeah, I managed to compile bjam first. But when I tried to compile boost library. it's giving me unknown in module path. – Tae-Sung Shin Nov 25 '11 at 09:04
  • have you set the NDK path in user_config.jam – N-JOY Nov 25 '11 at 09:08
  • Yes, I followed instruction of the article. – Tae-Sung Shin Nov 25 '11 at 09:17
  • okk then try installing mingW compiler(gcc compiler) and then set path in enviromental variables or copy gcc.exe to cygwin's bin folder. – N-JOY Nov 25 '11 at 09:46
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/5339/discussion-between-n-joy-and-paul) – N-JOY Nov 25 '11 at 09:56
  • Thanks for your help with my problem and also appreciate your description of the problem with include file. Now I can use boost in my app. – Tae-Sung Shin Nov 26 '11 at 04:33
  • welcome and N_JOY BOOST. – N-JOY Nov 28 '11 at 04:53
  • I am trying to resolve the same issues as the question. It seems how they resolved it was lost in the "continue this discussion in chat" link. Can some one throw some light on how i could use boost with Android please. I am having an issues with include. Please help – display name Feb 05 '16 at 16:47
  • Also try and look at the suggestions mentioned in answer – N-JOY Feb 08 '16 at 06:49
  • I'm getting error in compile time when I include boost header: 'struct in6_addr' has no member named 's6_addr' . Please help – shantanu Dec 22 '16 at 20:46

1 Answers1

5

My question contained almost complete steps for including BOOST library in Android. But still there are some important points you should remember while working with this.

  • Delete auto generated obj and libs folder Each time before you compile your native code.

  • If you are going to write your native code in C++, add LOCAL_CPP_EXTENSION := .cpp to your Android.mk(jni/Android.mk) file.

  • if you are going to code in C++, put your all cpp code inside extern "C" {}.

    extern C { /*cpp code*/ }

halfer
  • 19,824
  • 17
  • 99
  • 186
N-JOY
  • 10,344
  • 7
  • 51
  • 69