Windows 和 android 平臺Boost編譯方法

1.Windows 平臺編譯

環境

Windows 10
Visual Studio 2015 
boost 1.64

步驟

直接執行bootstrap.bat

之後直接運行 bjam.exe,或者指定編譯命令
指定msvc版本14.0對應的是vs2015--stagedir是指定編譯後存放的目錄
./bjam.exe stage --toolset=msvc-14.0 --without-graph --without-graph_parallel --stagedir="E:\boost_1_60_0\bin\vc14" link=static runtime-link=shared runtime-link=static threading=multi debug release  


--without-graph --without-graph_parallel 指定不編譯的庫
等待庫編譯....

編譯好後在VS中設置包含目錄和庫目錄即可


2.Android 平臺編譯

1.下載安裝Cygwin

下載Cygwin (http://www.cygwin.com/

安裝時,默認設置,只選擇Devel,將後面的Default改爲Install,然後就是等了。


安裝完後,進入Cygwin的終端,分別 gcc -v 和 make -v 查看版本

配置環境變量,添加環境變量 NDK_ROOT= E:/Android/android-ndk-r11c

2.NDK編譯boost生成靜態庫
解壓 boost_1_64_0.zip 到 E:\Android\android-ndk-r11c\sources 文件夾裏
進入boost_1_64_0目錄運行 bootstrap.sh 生成 b2

進入 boost_1_64_0/project-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 = E:/Android/android-ndk-r11c ;  
using gcc : android4.9 : $(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ :  
<archiver>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ar  
<ranlib>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.9/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ranlib  
<compileflags>--sysroot=$(ANDROID_NDK)/platforms/android-21/arch-arm  
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.9/include  
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.9/libs/armeabi/include  
#<compileflags>-DBOOST_NO_STD_WSTRING 
<compileflags>-DBOOST_FILESYSTEM_VERSION=3
<compileflags>-lgnustl_shared  
<compileflags>-mthumb
<compileflags>-Os  
<compileflags>-fno-strict-aliasing
<compileflags>-O2
<compileflags>-DNDEBUG
<compileflags>-g  
<compileflags>-lstdc++
<compileflags>-std=gnu++11
<compileflags>-D__GLIBC__
<compileflags>-D_GLIBCXX__PTHREADS
<compileflags>-D__arm__
<compileflags>-D_REENTRANT
;
注意調整 ANDROID_NDK 的路徑,這裏使用android-ndk-r11c ,所以用的是android4.9

編譯 boost,在 boost_1_64_0/android/lib 下生成靜態庫
 ./b2.exe --with-system --with-serialization toolset=gcc-android4.9 link=static runtime-link=static target-os=linux --stagedir=android

--with-system --with-serialization 爲指定要編譯的庫,不指定則默認編譯所有庫
在boost_1_64_0 文件夾下新建 Android.mk 寫入 
LOCAL_PATH:= $(call my-dir)  
#LOCAL_CPPFLAGS += –fexceptions
include $(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_serialization
LOCAL_SRC_FILES:= android/lib/libboost_serialization.a  
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)  
include $(PREBUILT_STATIC_LIBRARY)  
   
include $(CLEAR_VARS)  
LOCAL_MODULE:= boost_wserialization
LOCAL_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_64_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 ANDROID
int 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章