有個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
LOCAL_PATH:= $(call my-dir)
common_SRC_FILES := \
libs/thread/src/pthread/thread.cpp \
libs/thread/src/pthread/once.cpp \
common_SRC_FILES += \
libs/filesystem/v3/src/codecvt_error_category.cpp \
libs/filesystem/v3/src/operations.cpp \
libs/filesystem/v3/src/path.cpp \
libs/filesystem/v3/src/path_traits.cpp \
libs/filesystem/v3/src/portability.cpp \
libs/filesystem/v3/src/utf8_codecvt_facet.cpp \
libs/system/src/error_code.cpp \
libs/system/src/local_free_on_destruction \
include $(CLEAR_VARS)
LOCAL_MODULE:= libboost
LOCAL_SRC_FILES := $(common_SRC_FILES)
prebuilt_stdcxx_PATH := prebuilts/ndk/current/sources/cxx-stl/gnu-libstdc++
LOCAL_C_INCLUDES := \
$(LOCAL_PATH)/boost \
$(prebuilt_stdcxx_PATH)/include \
$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include/ \
LOCAL_C_INCLUDES := \
$(prebuilt_stdcxx_PATH)/include \
$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI)/include/ \
$(prebuilt_supccxx_PATH)/include
LOCAL_CFLAGS += -fvisibility=hidden -lpthread
LOCAL_CPPFLAGS += -fexceptions -frtti
LOCAL_SHARED_LIBRARIES := libc libstdc++ libstlport
LOCAL_LDFLAGS += -L$(prebuilt_stdcxx_PATH)/libs/$(TARGET_CPU_ABI) -lgnustl_static -lsupc++
LOCAL_MODULE_TAGS := optional
include $(BUILD_SHARED_LIBRARY)
比較重要的是上面 紅色 部分文字
最後編譯生成:
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 庫。
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11