首页 > 代码库 > Android NDK r10c 编译boost 1.55 (使用Cygwin)

Android NDK r10c 编译boost 1.55 (使用Cygwin)

项目用到boost::asio,boost::serialization, Android打包环境配置 ... 做个记录,下次别又掉到这坑里浪费时间

 

1. 安装Cygwin :

下载Cygwin (http://www.cygwin.com/) 

安装时,默认设置,只选择Devel,将后面的Default改为Install,然后就是等了。

技术分享

 

安装完后,进入Cygwin的终端,分别 gcc -v 和 make -v 查看版本

技术分享

 

配置环境变量,打开vi ~/.bash_profile,添加环境变量export NDK=/cygdrive/d/Android/android-ndk-r10c

技术分享

输入$NDK看看是否生效

 

 

2. NDK编译boost生成静态库:

进入Cygwin终端,解压boost_1_55_0.tar.bz2 到 android-ndk-r10c/sources 文件夹里

# tar -jxvf boost_1_55_0.tar.bz2

 

进入boost_1_55_0/目录运行 bootstrap.sh 生成 b2

技术分享

 

进入 boost_1_55_0/tools/build/v2/user-config.jam,在后面添加:

import os ;     if [ os.name ] = CYGWIN || [ os.name ] = NT {  androidPlatform = windows-x86_64 ;  }  else if [ os.name ] = LINUX {  androidPlatform = linux-x86_64 ;  }  else if [ os.name ] = MACOSX {  androidPlatform = darwin-x86 ;  }     modules.poke : NO_BZIP2 : 1 ;  #ANDROID_NDK = ../.. ;  ANDROID_NDK = D:/Android/android-ndk-r10c ;  using gcc : android4.8 : $(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.8/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ :  <archiver>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.8/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ar  <ranlib>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.8/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ranlib  <compileflags>--sysroot=$(ANDROID_NDK)/platforms/android-9/arch-arm  <compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.8/include  <compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.8/libs/armeabi/include  #<compileflags>-DBOOST_NO_STD_WSTRING<compileflags>-DNDEBUG  <compileflags>-D__GLIBC__  <compileflags>-DBOOST_FILESYSTEM_VERSION=3  <compileflags>-lstdc++  <compileflags>-lgnustl_shared  <compileflags>-mthumb  <compileflags>-fno-strict-aliasing  <compileflags>-std=gnu++11  <compileflags>-O2;  

注意调整 ANDROID_NDK 的路径.

 

编译 boost::asio,boost::serialization,在 boost_1_55_0/android/lib 下生成静态库

# ./b2 --with-system --with-serialization toolset=gcc-android4.8 link=static runtime-link=static target-os=linux --stagedir=android

技术分享

 

在boost_1_55_0 文件夹下新建 Android.mk 写入

LOCAL_PATH:= $(call my-dir)  #LOCAL_CPPFLAGS += –fexceptionsinclude $(CLEAR_VARS)  LOCAL_MODULE:= boost_system  LOCAL_SRC_FILES:= android/lib/libboost_system.a  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)  include $(PREBUILT_STATIC_LIBRARY)     include $(CLEAR_VARS)  LOCAL_MODULE:= boost_serializationLOCAL_SRC_FILES:= android/lib/libboost_serialization.a  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)  include $(PREBUILT_STATIC_LIBRARY)     include $(CLEAR_VARS)  LOCAL_MODULE:= boost_wserializationLOCAL_SRC_FILES:= android/lib/libboost_wserialization.a  LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)  include $(PREBUILT_STATIC_LIBRARY)     

 

3. 项目打包

打开jni/Android.mk 添加

LOCAL_WHOLE_STATIC_LIBRARIES += boost_system
LOCAL_WHOLE_STATIC_LIBRARIES += boost_serialization

$(call import-module, boost_1_55_0)

 

编译报错:

技术分享

则要在 jni/Application.mk 文件中添加 ,

APP_CPPFLAGS += -fexceptions (添加异常支持 LOCAL_CPPFLAGS += –fexceptions)

 

编译到boost::serialization报错,

参考http://stackoverflow.com/questions/15479136/how-to-implement-mbtowc-for-android-or-ideally-how-not-to

技术分享

技术分享

这类没定义的函数是c库,NDK没实现类些函数。。。boost库官网说相信你自己能够实现,好厉害啊~

在cpp文件里加入代码,替代那些没定义的函数

#ifdef ANDROIDint wctomb(char *s, wchar_t wc) { return wcrtomb(s,wc,NULL); }int mbtowc(wchar_t *pwc, const char *s, size_t n) { return mbrtowc(pwc, s, n, NULL); }#endif

好了,就这样可以打出app包了

 

最后的 jni/Android.mk 文件内容是(红色是添加的部分)

LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := cocos2dcpp_sharedLOCAL_MODULE_FILENAME := libcocos2dcppLOCAL_SRC_FILES := hellocpp/main.cpp                    ../../Classes/AppDelegate.cpp                    ../../Classes/HelloWorldScene.cppLOCAL_C_INCLUDES := $(LOCAL_PATH)/../../ClassesLOCAL_WHOLE_STATIC_LIBRARIES := cocos2dx_staticLOCAL_WHOLE_STATIC_LIBRARIES += cocosdenshion_staticLOCAL_WHOLE_STATIC_LIBRARIES += boost_systemLOCAL_WHOLE_STATIC_LIBRARIES += boost_serialization#LOCAL_WHOLE_STATIC_LIBRARIES += boost_wserialization# LOCAL_WHOLE_STATIC_LIBRARIES += box2d_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocosbuilder_static# LOCAL_WHOLE_STATIC_LIBRARIES += spine_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocostudio_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_network_static# LOCAL_WHOLE_STATIC_LIBRARIES += cocos_extension_staticinclude $(BUILD_SHARED_LIBRARY)$(call import-module,.)$(call import-module,audio/android)$(call import-module, boost_1_55_0)# $(call import-module,Box2D)# $(call import-module,editor-support/cocosbuilder)# $(call import-module,editor-support/spine)# $(call import-module,editor-support/cocostudio)# $(call import-module,network)# $(call import-module,extensions)

 

最后的 jni/Application.mk 文件内容是(红色是添加的部分)

APP_STL := gnustl_staticAPP_CPPFLAGS := -frtti -DCC_ENABLE_CHIPMUNK_INTEGRATION=1 -DCOCOS2D_DEBUG=1 -std=c++11 -fsigned-charAPP_CPPFLAGS += -fexceptions

 

 

参考:

http://blog.csdn.net/ly131420/article/details/41390379http://stackoverflow.com/questions/15479136/how-to-implement-mbtowc-for-android-or-ideally-how-not-tohttp://blog.csdn.net/hcj116/article/details/19752307http://stackoverflow.com/questions/17667978/using-boost-in-android-ndk-with-windowshttp://www.codexperiments.com/android/2011/05/tips-tricks-building-boost-with-ndk-r5/http://blog.csdn.net/andyhuabing/article/details/8594679

Android NDK r10c 编译boost 1.55 (使用Cygwin)