ndk编译boost库

1. 简介
  最近,项目的需求——将原本运行在Linux的上C/C++移植到Android上运行。折腾了3天,终于可以编译出使用了boost库的可执行程序。这边主要是记录下,并分享出来,供需要的人员参考,希望对大家有所帮助。如果有什么问题可以在下面评论或发私信。
2. android-ndk在Linux上的安装
  博主的ndk版本是从底层的驱动人员那边获取的。这边使用的ndk版本是:android-ndk32-r10b-linux-x86。如果你使用的版本不一致也没什么关系,一般差异不会太大,只需要稍作修改就行。
  首先,将手头上的android-ndk32-r10b-linux-x86.tar进行解压得到目录android-ndk-r10b。


  然后,需要修改/etc/profile文件,在终端上运行命令行:sudo vim /etc/profile。添加这么一行,当然添加的行中的路径就是你之前解压的路径,需要对应自己的路径。博主的路径如下所示。

  最后,在终端下面运行命令:source /etc/profile。到这里就搞定了ndk了,很简单吧。
  下面我们验证下是否成功,在任意的路径下面键入:ndk-build -v。

  验证是通过的。
3. Android-ndk编译boost库
  博主的boost版本使用的是:boost_1_64_0。如果使用的是比较旧的boost版本可能会有所不同。同样的将我们使用的boost库解压,进入到解压后的目录后运行命令:./bootstrap.sh --prefix=/fordel/boostlib/boost_android/boost_1_64_0/。这个指定的路径就是之后编译完成之后,boost的头文件和静态库的存放路径。根据自己的需要指定。运行成功之后会生成b2,bjam以及一个project-config.jam的文件。
  首先,我们需要做的就是修改project-config.jam文件。
# Boost.Build Configuration
# Automatically generated by bootstrap.sh
 
import option ;
#import feature ;
import os ;    
 
# Compiler configuration. This definition will be used unless
# you already have defined some toolsets in your user-config.jam
# file.
     
if [ os.name ] = CYGWIN || [ os.name ] = NT {    
androidPlatform = windows-x86_64 ;    
}    
else if [ os.name ] = LINUX {
#如果使用64位的ndk    
#androidPlatform = linux-x86_64 ;
#使用32位的ndk
androidPlatform = linux-x86 ;  
}    
else if [ os.name ] = MACOSX {    
androidPlatform = darwin-x86 ;    
}    
     
modules.poke : NO_BZIP2 : 1 ;    
ANDROID_NDK = /home/myDisk/android-ndk/android-ndk-r10b ;    
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  
;
 
#原先需要保存的内容,下面这部分与之前的路径有关,
#不需要和这边一致
# Python configuration
import python ;
if ! [ python.configured ]
{
    using python : 2.7 : /usr ;
}
 
# List of --with-<library> and --without-<library>
# options. If left empty, all libraries will be built.
# Options specified on the command line completely
# override this variable.
libraries =  ;
 
# These settings are equivivalent to corresponding command-line
# options.
option.set prefix : /fordel/boostlib/boost_android/boost_1_64_0/ ;
option.set exec-prefix : /fordel/boostlib/boost_android/boost_1_64_0/ ;
option.set libdir : /fordel/boostlib/boost_android/boost_1_64_0//lib ;
option.set includedir : /fordel/boostlib/boost_android/boost_1_64_0//include ;
 
# Stop on first error
option.set keep-going : false ;
  这边需要着重说明的是:androidPlatform平台需要根据你使用的是64位的还是32位的ndk开发包。并且在编译器的路径中需要注意:

  上面圈出的就是可能需要修改的点,具体圈出的是否存在,还是有其他的版本就需要自己去路径中找下,然后改成存在的就可以了。
  其次,我们运行./b2。进行编译,等待编译完成即可,时间可能有点久。
  最后,我们运行./b2 install,就完成了boost库的编译。

  boost库的头文件和静态路就在我们指定的目录下面了。
4. 测试用例
  为了方便我们直接使用ndk目录下面的sample例子进行修改来验证测试用例。这里我使用的是hello-jni。
#include <unistd.h>
#include <iostream>
#include <boost/scoped_ptr.hpp>
 
using namespace std;
 
class CMyClass  
{  
public:  
    explicit CMyClass()   
    {  
        cout << "CMyClass Construction function !" << endl;  
    }  
  
    ~CMyClass()   
    {  
        cout << "CMyClass Destruct function" << endl;  
    }  
  
    void MyFunc(void)  
    {  
        cout << "Do Something...." << endl;  
    }  
};  
  
void Function1(void)  
{  
    //boost::scoped_ptr是一个简单的智能指针,它  
    //能保证离开作用域后对象被自动释放,能避免由于  
    //忘记释放而导致的内存泄漏。
    //验证运行的正确性是:打印:Now, Quit Function ...
    //之后,打印:CMyClass Destruct function
    boost::scoped_ptr<CMyClass> spMyClass(new CMyClass());  
    spMyClass->MyFunc();  
    cout << "Now, Quit Function ..." << endl;
}
                      
int main(void)  

    cout << "boost demo test start ..." << endl; 
    Function1();  
    cout << "boost demo test end ..." << endl;
 
    while (1)
    {
        sleep(1);
    }         
              
    return 0;  

# Copyright (C) 2009 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#Android.mk文件
LOCAL_PATH := $(call my-dir)
 
BOOST_INCLUDE_PATH    := /fordel/boostlib/boost_android/boost_1_64_0/include
BOOST_LIB_PATH        := /fordel/boostlib/boost_android/boost_1_64_0/lib
 
include $(CLEAR_VARS)
 
LOCAL_MODULE        := hello-jni
LOCAL_C_INCLUDES    := $(BOOST_INCLUDE_PATH)
 
LOCAL_LDFLAGS += -pie -fPIE 
 
LOCAL_SRC_FILES := hello-jni.cpp
 
include $(BUILD_EXECUTABLE)
#Application.mk文件
APP_ABI := all
APP_STL := gnustl_static

NDK交叉编译Boost是将Boost编译成适用于Android平台的文件,以便在Android应用中使用Boost的功能。下面是一般的NDK交叉编译Boost的步骤: 1. 下载NDK:首先,你需要下载并安装Android NDK,可以从官方网站(https://developer.android.com/ndk/downloads)上获取最新版本的NDK。 2. 下载Boost:接下来,你需要下载Boost的源代码。你可以从Boost官方网站(https://www.boost.org/users/download/)上下载最新版本的Boost。 3. 配置Boost:解压下载的Boost源代码,并进入解压后的目录。在终端中执行以下命令来配置Boost: ``` ./bootstrap.sh --with-libraries=<library_names> --with-toolset=<toolset_name> --prefix=<install_path> ``` 其中,`<library_names>`是你需要编译Boost的名称,可以根据你的需求进行选择;`<toolset_name>`是你要使用的编译工具链,例如`clang`或`gcc`;`<install_path>`是你希望安装Boost的路径。 4. 编辑user-config.jam文件:在Boost源代码目录下,创建一个名为`user-config.jam`的文件,并添加以下内容: ``` using clang : <ndk_version> : <path_to_ndk>/toolchains/llvm/prebuilt/<host_os>/bin/clang++ ; ``` 其中,`<ndk_version>`是你下载的NDK的版本号,`<path_to_ndk>`是你安装NDK的路径,`<host_os>`是你的操作系统类型(例如`darwin-x86_64`或`linux-x86_64`)。 5. 开始编译Boost:在终端中执行以下命令来开始编译Boost: ``` ./b2 toolset=clang-<ndk_version> target-os=android link=static threading=multi variant=release install ``` 这将使用指定的编译工具链和选项来编译Boost,并将编译结果安装到之前配置的安装路径中。 6. 完成编译:等待编译过程完成,然后你将在之前配置的安装路径中找到编译好的Boost文件。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值