1. 程式人生 > >Cygwin環境下使用Android NDK r8e編譯boost 1.53

Cygwin環境下使用Android NDK r8e編譯boost 1.53

這裡不能不說Google確實搜了1天都沒解決問題, baidu第一個就解決了...

cygwin參考這裡

簡單的說

1.下載cygwin的網路安裝包(第一次安裝只能這樣)

2. 選擇靠前一點的網站進行下載( 下好的包可以留著, 下次安裝直接從資料夾中選擇就可以安裝)

cygwin下使用NDK編譯boost參考這裡

細節注意:

1. boost官網下載boost_1_53_0.tar.bz2, 解壓放在android-ndk-r8e/sources下

2. 生成boost編譯工具: 在boost目錄下執行 ./bootstrap.sh 或者 bootstrap.bat 生成 b2

3. 修改 boost/tools/build/v2/user-config.jam 內容修改如下

import os ;
if [ os.name ] = CYGWIN || [ os.name ] = NT {
androidPlatform = windows ;
}
else if [ os.name ] = LINUX {
androidPlatform = linux-x86_64 ;
}
else if [ os.name ] = MACOSX {
androidPlatform = darwin-x86 ;
}
modules.poke : NO_BZIP2 : 1 ;
ANDROID_NDK = ../.. ;
using gcc : android4.6 : $(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.6/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ :
<archiver>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.6/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ar
<ranlib>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.6/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.6/include
<compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/4.6/libs/armeabi/include
<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
;

4. 為了安全起見, 我打了個BoostForAndriod上的補丁: 手動修改下就好了

diff -ruN boost_1_53_0-boot/boost/asio/detail/socket_types.hpp boost_1_53_0-patched/boost/asio/detail/socket_types.hpp
--- boost_1_53_0-boot/boost/asio/detail/socket_types.hpp     2012-01-15 14:46:25.000000000 +0100
+++ boost_1_53_0-patched/boost/asio/detail/socket_types.hpp     2012-06-27 19:19:01.279562338 +0200
@@ -123,7 +123,12 @@
typedef int socket_type;
const int invalid_socket = -1;
const int socket_error_retval = -1;
+// @Moss - Some platforms do not define it (Android)
+#if defined(INET_ADDRSTRLEN)
const int max_addr_v4_str_len = INET_ADDRSTRLEN;
+#else // defined(INET_ADDRSTRLEN)
+const int max_addr_v4_str_len = 16;
+#endif // defined(INET_ADDRSTRLEN)
#if defined(INET6_ADDRSTRLEN)
const int max_addr_v6_str_len = INET6_ADDRSTRLEN + 1 + IF_NAMESIZE;
#else // defined(INET6_ADDRSTRLEN)
diff -ruN boost_1_53_0-boot/boost/asio/ip/impl/address_v6.ipp boost_1_53_0-patched/boost/asio/ip/impl/address_v6.ipp
--- boost_1_53_0-boot/boost/asio/ip/impl/address_v6.ipp     2012-01-15 14:46:25.000000000 +0100
+++ boost_1_53_0-patched/boost/asio/ip/impl/address_v6.ipp     2012-06-27 19:19:11.029581297 +0200
@@ -11,6 +11,23 @@
#ifndef BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP
#define BOOST_ASIO_IP_IMPL_ADDRESS_V6_IPP
+// @Moss - Define IPv6 macros
+#if !defined(IN6_IS_ADDR_MULTICAST) 
+#define IN6_IS_ADDR_MULTICAST(a) (((__const uint8_t *) (a))[0] == 0xff)
+#endif
+
+#if !defined(IN6_IS_ADDR_MC_NODELOCAL)
+#define IN6_IS_ADDR_MC_NODELOCAL(a) \
+        (IN6_IS_ADDR_MULTICAST(a) \
+         && ((((__const uint8_t *) (a))[1] & 0xf) == 0x1))
+#endif
+
+#if !defined(IN6_IS_ADDR_MC_GLOBAL)
+#define IN6_IS_ADDR_MC_GLOBAL(a) \
+        (IN6_IS_ADDR_MULTICAST(a) \
+         && ((((__const uint8_t *) (a))[1] & 0xf) == 0xe))
+#endif
+
#if defined(_MSC_VER) && (_MSC_VER >= 1200)
# pragma once
#endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
diff -ruN boost_1_53_0-boot/boost/config/user.hpp boost_1_53_0-patched/boost/config/user.hpp
--- boost_1_53_0-boot/boost/config/user.hpp     2004-01-10 13:10:00.000000000 +0100
+++ boost_1_53_0-patched/boost/config/user.hpp     2012-06-27 19:18:46.129532736 +0200
@@ -13,6 +13,15 @@
//  configuration policy:
//
+// Android defines
+#define __arm__ 1
+#define _REENTRANT 1
+#define _GLIBCXX__PTHREADS 1
+// There is problem with std::atomic on android (and some other platforms).
+// See this link for more info:
+// https://code.google.com/p/android/issues/detail?id=42735#makechanges
+#define BOOST_ASIO_DISABLE_STD_ATOMIC 1
+
// define this to locate a compiler config file:
// #define BOOST_COMPILER_CONFIG <myheader>
diff -ruN boost_1_53_0-boot/boost/detail/endian.hpp boost_1_53_0-patched/boost/detail/endian.hpp
--- boost_1_53_0-boot/boost/detail/endian.hpp     2011-03-29 23:58:48.000000000 +0200
+++ boost_1_53_0-patched/boost/detail/endian.hpp     2012-06-27 19:18:39.359519453 +0200
@@ -31,7 +31,7 @@
// GNU libc offers the helpful header <endian.h> which defines
// __BYTE_ORDER
-#if defined (__GLIBC__)
+#if defined (__GLIBC__) || defined(ANDROID)
# include <endian.h>
# if (__BYTE_ORDER == __LITTLE_ENDIAN)
#  define BOOST_LITTLE_ENDIAN
diff -ruN boost_1_53_0-boot/boost/interprocess/detail/workaround.hpp boost_1_53_0-patched/boost/interprocess/detail/workaround.hpp
--- boost_1_53_0-boot/boost/interprocess/detail/workaround.hpp     2011-12-26 18:21:36.000000000 +0100
+++ boost_1_53_0-patched/boost/interprocess/detail/workaround.hpp     2012-06-27 19:18:52.909546004 +0200
@@ -54,7 +54,7 @@
    #endif
    //Check for XSI shared memory objects. They are available in nearly all UNIX platforms
-   #if !defined(__QNXNTO__)
+   #if !defined(__QNXNTO__) && !defined(ANDROID)
       #define BOOST_INTERPROCESS_XSI_SHARED_MEMORY_OBJECTS
    #endif
diff -ruN boost_1_53_0-boot/libs/filesystem/src/operations.cpp boost_1_53_0-patched/libs/filesystem/src/operations.cpp
--- boost_1_53_0-boot/libs/filesystem/src/operations.cpp     2012-01-28 15:40:16.000000000 +0100
+++ boost_1_53_0-patched/libs/filesystem/src/operations.cpp     2012-06-27 19:19:19.269597266 +0200
@@ -73,13 +73,15 @@
     const fs::path dot_dot_path("..");
#   include <sys/types.h>
#   include <sys/stat.h>
-#   if !defined(__APPLE__) && !defined(__OpenBSD__)
+#   if !defined(__APPLE__) && !defined(__OpenBSD__) && !defined(__ANDROID__) && !defined(ANDROID)
#     include <sys/statvfs.h>
#     define BOOST_STATVFS statvfs
#     define BOOST_STATVFS_F_FRSIZE vfs.f_frsize
#   else
#     ifdef __OpenBSD__
#     include <sys/param.h>
+#     elif defined(__ANDROID__) || defined(ANDROID) // @Moss - Android messes up a bit with some headers, this one is the correct one :D
+#     include <sys/vfs.h>
#     endif
#     include <sys/mount.h>
#     define BOOST_STATVFS statfs
@@ -206,7 +208,19 @@
          || ::mkdir(to.c_str(),from_stat.st_mode)!= 0))
#   define BOOST_COPY_FILE(F,T,FailIfExistsBool)copy_file_api(F, T, FailIfExistsBool)
#   define BOOST_MOVE_FILE(OLD,NEW)(::rename(OLD, NEW)== 0)
+#if defined(__ANDROID__) || defined(ANDROID)
+    int BOOST_RESIZE_FILE(const char *path, off_t size)
+    {
+      int result = -1;
+      int fd = open(path, O_WRONLY);
+      if (fd != -1)
+        result = ftruncate(fd, size);
+      close(fd);
+      return result;
+    }
+#else
#   define BOOST_RESIZE_FILE(P,SZ)(::truncate(P, SZ)== 0)
+#endif
#   define BOOST_ERROR_NOT_SUPPORTED ENOSYS
#   define BOOST_ERROR_ALREADY_EXISTS EEXIST

5. 命令列編譯:

./b2 --without-python --without-mpi --without-serialization toolset=gcc-android4.6 link=static runtime-link=static target-os=linux --stagedir=android

使用編譯完成的boost庫

1. 將編譯boost_1_53_0\android\lib\ 下的.a庫 及boost_1_53_0\boost\下的標頭檔案拷貝出來放在工程的boost4android目錄下

2. 在boost4android目錄下,建立Android.mk, 內容如下

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_atomic
LOCAL_SRC_FILES:= lib/libboost_atomic.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_chrono
LOCAL_SRC_FILES:= lib/libboost_chrono.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_context
LOCAL_SRC_FILES:= lib/libboost_context.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_date_time
LOCAL_SRC_FILES:= lib/libboost_date_time.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_exception
LOCAL_SRC_FILES:= lib/libboost_exception.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_filesystem
LOCAL_SRC_FILES:= lib/libboost_filesystem.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_graph
LOCAL_SRC_FILES:= lib/libboost_graph.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_iostreams
LOCAL_SRC_FILES:= lib/libboost_iostreams.a 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_math_c99
LOCAL_SRC_FILES:= lib/libboost_math_c99.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_math_c99f
LOCAL_SRC_FILES:= lib/libboost_math_c99f.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_math_c99l
LOCAL_SRC_FILES:= lib/libboost_math_c99l.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_math_tr1
LOCAL_SRC_FILES:= lib/libboost_math_tr1.a 
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_math_tr1f
LOCAL_SRC_FILES:= lib/libboost_math_tr1f.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_math_tr1l
LOCAL_SRC_FILES:= lib/libboost_math_tr1l.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_program_options
LOCAL_SRC_FILES:= lib/libboost_program_options.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_random
LOCAL_SRC_FILES:= lib/libboost_random.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_regex
LOCAL_SRC_FILES:= lib/libboost_regex.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_system
LOCAL_SRC_FILES:= lib/libboost_system.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_thread
LOCAL_SRC_FILES:= lib/libboost_thread.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE:= boost_timer
LOCAL_SRC_FILES:= lib/libboost_timer.a
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)
include $(PREBUILT_STATIC_LIBRARY)
可以根據自己的需要調整以上包含 3. 在應用程式的Android.mk中新增

LOCAL_WHOLE_STATIC_LIBRARIES += boost_system
LOCAL_WHOLE_STATIC_LIBRARIES += boost_filesystem
LOCAL_WHOLE_STATIC_LIBRARIES += boost_thread

用多少, 包多少

新增異常支援 LOCAL_CPPFLAGS += –fexceptions

新增模組引用

$(call import-module,boost4android)