有个android项目上需要使用到boost标准c++库,Boost库是一个可移植、提供源代码的C++库,作为标准库的后备,是C++标准化进程的发动机之一,所以想将其移植到android系统上进行编译。

直接从网上下载 boost1.52版本,然后写makefile。。。进行编译,,碰到无数的编译错误。求助于google网络,终于发现一个好东东:

一个通过NDK编译boost代码的git项目:
https://github.com/MysticTreeGames/Boost-for-Android

这里是使用NDK进行编译boost的但肯定对我有用吧。。经过一番折腾终于编译成功,,高兴,呵呵!

下面将主要的过程记录下来:

1、下载boost 源码:

由于https://github.com/MysticTreeGames/Boost-for-Android这里说明:

       Boost for Android

Boost for android is a set of tools to compile the main part of the Boost C++ Libraries for the Android platform.

Currently supported boost versions are 1.45.0, 1.48.0 and 1.49.0.

因此下载最新的 1.49 版本,下载完成后进行打补丁

2、下载补丁代码

https://github.com/MysticTreeGames/Boost-for-Android/tree/master/patches/boost-1_49_0

3、打补丁代码

      boost_1_49_0.tar.bz2 放在android的 external 下解压:

      tar -xjvf external/boost_1_49_0.tar.bz2


      将boost-1_49_0.patch放在external\boost_1_49_0下面根目录执行:

      patch -Np1 -d external/boost_1_49_0 < external/boost_1_49_0/boost-1_49_0.patch


4、编写boost的makefile文件,即Android.mk

      

[cpp]  view plain copy
  1. LOCAL_PATH:= $(call my-dir)  
  2.   
  3. common_SRC_FILES := \  
  4.         libs/thread/src/pthread/thread.cpp \  
  5.         libs/thread/src/pthread/once.cpp \  
  6.   
  7. common_SRC_FILES += \  
  8.     libs/filesystem/v3/src/codecvt_error_category.cpp \  
  9.     libs/filesystem/v3/src/operations.cpp \  
  10.     libs/filesystem/v3/src/path.cpp \  
  11.     libs/filesystem/v3/src/path_traits.cpp \  
  12.     libs/filesystem/v3/src/portability.cpp \  
  13.     libs/filesystem/v3/src/utf8_codecvt_facet.cpp \  
  14.     libs/system/src/error_code.cpp \  
  15.     libs/system/src/local_free_on_destruction \  
  16.   
  17.   
  18. include $(CLEAR_VARS)  
  19. LOCAL_MODULE:= libboost  
  20. LOCAL_SRC_FILES := $(common_SRC_FILES)  
[cpp]  view plain copy
  1. <span style="color:#ff0000;">prebuilt_stdcxx_PATH := prebuilts/ndk/current/sources/cxx-stl/gnu-libstdc++  
  2.   
  3. LOCAL_C_INCLUDES := \  
  4.     $(LOCAL_PATH)/boost \  
  5.     $(prebuilt_stdcxx_PATH)/include \  
  6.     $(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include/ \  
  7.   
  8. LOCAL_C_INCLUDES += \  
  9.     $(prebuilt_stdcxx_PATH)/include \  
  10.     $(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include/ \  
  11.     $(prebuilt_supccxx_PATH)/include  
  12.   
  13. LOCAL_CFLAGS += -fvisibility=hidden -lpthread  
  14. LOCAL_CPPFLAGS += -fexceptions -frtti  
  15.   
  16. LOCAL_SHARED_LIBRARIES := libc libstdc++ libstlport  
  17.   
  18. LOCAL_LDFLAGS += -L$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI) -lgnustl_static -lsupc++</span>  
  19.   
  20. LOCAL_MODULE_TAGS := optional  
  21. include $(BUILD_SHARED_LIBRARY)  
[cpp]  view plain copy
  1.   

比较重要的是上面 红色 部分文字

最后编译生成:

target thumb C++: libboost <= external/boost_1_49_0/libs/thread/src/pthread/thread.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/thread/src/pthread/once.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/filesystem/v3/src/codecvt_error_category.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/filesystem/v3/src/operations.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/filesystem/v3/src/path.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/filesystem/v3/src/path_traits.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/filesystem/v3/src/portability.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/filesystem/v3/src/utf8_codecvt_facet.cpp
target thumb C++: libboost <= external/boost_1_49_0/libs/system/src/error_code.cpp
target SharedLib: libboost (out/target/product/godbox/obj/SHARED_LIBRARIES/libboost_intermediates/LINKED/libboost.so)
target Symbolic: libboost (out/target/product/godbox/symbols/system/lib/libboost.so)
target Strip: libboost (out/target/product/godbox/obj/lib/libboost.so)
Install: out/target/product/godbox/system/lib/libboost.so      


成功生成 libboost.so  库