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