【博文翻译】Building Boost with NDK R5

  写在前面:

      最近一直都在搞关于NDK的东西,由于资料的缺少吧,这所学习的路程,真的是前路坎坷,却也收获良多。我真的很是感叹,关于NDK,Boost的相结合的资料真的是太少了,若想找到一点点合适的资料,就和大海捞针一样艰难。这短时间,我一边学习一边整理了很多相应的文档,也尝试着发到博客园两篇,我想应该会帮助到一点和我有同样目的的人。至于有没有误导别人,呵呵,我只能说小子才疏学浅,误导了你,那真抱歉了。

      虽然找资料很郁闷,但幸运的是,我最终找到了解决我的问题的方法,找到了网络上最靠谱的资料。

      在这里我向大家推荐一本书《Android.NDK.Beginner's.Guide》,作者:Sylvain Ratabouil。这本书我在之前的文章中提到过,作者号称这是世界上第一本关于NDK的书籍,我几乎都拜读了,确实讲解的很细,比这老小子在博客上面写得靠谱多了。

      下面的这篇文章就是他的博客上的一篇文章,关于Building Boost with NDK R5。

      翻译原文地址:http://www.codexperiments.com/android/2011/05/tips-tricks-building-boost-with-ndk-r5/

 

——————————————————————————切割线—————————————————————————

 

      

      Boost is almost composed of template and header files. Thus, nothing needs to be built at all most of the time… just include the necessary header files. This is true for smart pointers for example. But a few features of Boost are available through compiled libraries, like the threading module. Let’s see how to do that with the NDK R5!

      Boost库差不多都是由模版和头文件组成,因此,大多数时候并不需要去编译它,只需要包含特殊的头文件即可使用。这就像是智能指针。但是,Boost的一些特性是需要通过编译子库才能使用的,就像Thread。让我们一起来看一下使用NDK R5编译Boost。

 

 

      First, I would like to point out that this solution is based on a discussion from Google Group. To compile boost, do the following (I do it on Ubuntu but this should be similar on Windows with Cygwin):

      首先,我想要指明的是这个解决方案是根据Google Group的一个讨论而来的。要编译Boost,可以按照一下的步骤来(我的操作是在Ubuntu上面进行,但是这个流程和在windows上面使用Cygwin的应该是相似的):

 

      The first thing is to download Boost. I am using Boost v1.46.1 in the present post.

      Then untar the archive into your $ANDROID_NDK/sources directory. You should end up with the following.

      

      【博文翻译】Building Boost with NDK R5

      第一步是去下载Boost。我在这个文章中使用的是v1.46。

      然后,解压归档文件到$ANDROID_NDK/sources这个目录下。你最终得到的结果应该是以下内容。

      (图片略)

 

      When uncompressed, open the file “user-config.jam” located in “$ANDROID_NDK/sources/boost/tools/build/v2″. BJam is a custom build tool more or less like make or ant and which is used to build boost. The file user-config.jam is, like its name indicates it, a custom configuration file that can be set-up by Boost users before compilation. Update user-config.jam with the following content:

    import os ;

    if [ os.name ] = CYGWIN || [ os.name ] = NT

    {

        androidPlatform = windows ;

    }

    else if [ os.name ] = LINUX

    {

        androidPlatform = linux-x86 ;

    }

    else if [ os.name ] = MACOSX

    {

        androidPlatform = darwin-x86 ;

    }

    modules.poke : NO_BZIP2 : 1 ;

    ANDROID_NDK = ../.. ;

    using gcc : android4.4.3 :

    $(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-g++ :

    <compileflags>--sysroot=$(ANDROID_NDK)/platforms/android-9/arch-arm

    <compileflags>-mthumb

    <compileflags>-Os

    <compileflags>-fno-strict-aliasing

    <compileflags>-O2

    <compileflags>-DNDEBUG

    <compileflags>-g

    <compileflags>-lstdc++

    <compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/include

    <compileflags>-I$(ANDROID_NDK)/sources/cxx-stl/gnu-libstdc++/libs/armeabi/include

    <compileflags>-D__GLIBC__

    <compileflags>-DBOOST_NO_INTRINSIC_WCHAR_T

    <compileflags>-DBOOST_FILESYSTEM_VERSION=2

    <archiver>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ar

    <ranlib>$(ANDROID_NDK)/toolchains/arm-linux-androideabi-4.4.3/prebuilt/$(androidPlatform)/bin/arm-linux-androideabi-ranlib

    ;

      当解压完成后,打开位于$ANDROID_NDK/sources/boost/tools/build/v2下的文件user-config.jam. Bjam是一个自带的用来编译Boost的小工具,就像make 或 ant 一样。user-config.jam 文件就像它的名字表现的那样,是一个可以被Boost用户定制的一个用于编译Boost的配置文件。在user-config.jam中更新以下内容:

(代码略)

 

 

      Once this is done, launch compilation using the following command line. We need to exclude a few modules which are not working with NDK like the “serialization” module. Indeed NDK does not support wide chars which are required for it.  We don’t compile python which requires additional python libs:

      bjam --without-python --without-serialization toolset=gcc-android4.4.3 link=static runtime-link=static target-os=linux --stagedir=android

      

      一旦这一步做完了,使用以下命令行进行编译。我们需要排除一些像serialization一样不能与NDK一起工作的库。事实上NDK不支持宽字符而serialization需要宽字符。我们不能编译python因为编译它需要附加的库。

      (代码略)

 

        

      The biggest step has been performed. No error should appear on screen after compilation (look for “…failed updating X targets…” message). Now, how to include it in our own project? One of the best solution is to make use of the new import-module feature of NDK R5.

      当你看到屏幕上面没有出现错误的时候(去找找“…failed updating X targets…”的消息,那代表着错误),这最大的一步就已经执行完毕了。现在开始下一步,怎么把编译好的库包含到我们自己的项目中去?一个最好的解决方案就是使用NDK R5的新引入模块的特性。

 

      Create an Android.mk file in $ANDROID_NDK/sources/boost. It needs to contain one module declaration per library. Variable LOCAL_EXPORT_C_INCLUDES is important as it is the one which automatically append boost directory to include file directories. Here, we declared two static libs Boost thread and iostreams. All available libraries can be found in $ANDROID_NDK/sources/boost/android/lib:

      (注:Android.mk文件内容)

      LOCAL_PATH:= $(call my-dir)

      include $(CLEAR_VARS)

      LOCAL_MODULE:= boost_thread

      LOCAL_SRC_FILES:= android/lib/libboost_thread.a

      LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)

      include $(PREBUILT_STATIC_LIBRARY)

 

      include $(CLEAR_VARS)

      LOCAL_MODULE:= boost_iostreams

      LOCAL_SRC_FILES:= android/lib/libboost_iostreams.a

      LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)

      include $(PREBUILT_STATIC_LIBRARY)

     

      在$ANDROID_NDK/sources/boost目录下创建一个Android.mk文件。每个库都需要包含一个模块声明。变量LOCAL_EXPORT_C_INCLUDES是一个很重要的变量,它用来自动添加Boost库的路径到包含目录。我们现在先声明两个静态库:Boost thread 和 iostreams. 所有可用的库文件否存放在$ANDROID_NDK/sources/boost/android/lib目录下。

      (代码略)

 

      To include Boost in an application, we need to link with a STL which supports exception. Hopefully, we don’t need to build one manually anymore as NDK R5 now provides STLport and GNU STL. However, only GNU STL support exceptions right now. But on the opposite to STLport, only static linking is supported. So to make use of GNU STL, just open (or create) your Application.mk file and add (or complete) the two following lines:

      APP_STL      = gnustl_static

      APP_CPPFLAGS = -fexceptions

      在一个程序中包含Boost ,我们需要链接一个支持异常处理的STL库。所幸的是,我们不需要自己再手动去编译一个库,因为NDK R5已经支持STLport 和 GNU STL. 然而,现在只有GNU STL支持异常处理。但是相对于STLport,GNU STL只支持静态链接。所以想要使用GUN STL,只需要打开(或创建)你的Application.mk文件并添加(或完善)以下两行代码。

      (代码略)

 

      Finally, open your Application (not Boost one) Android.mk file, include the boost module and call import-module function:

      LOCAL_STATIC_LIBRARIES := boost_thread

      $(call import-module,boost)

      

      最后,打开你程序的Android.mk文件(不是Boost文件夹下面那个),包含boost模块并调用引用模块函数。

      (代码略)

 

      That’s it! Now enjoy the (almost) full power of Boost

      (最后一句,唉~胜利了,就这样吧,不翻译了)

 

 

————————————————————————分割线—————————————————————————————

        作者英语水平有限,只能翻译这个程度,有些句子都是意译的,不要跟我计较啊。

      告诉读者一个秘密,就算你按照这个教程来做,你也是有一个Boost的子库是无法编译通过的,那就是filesystem,这是为什么呢?因为这个作者在博客上面压根就没有写全,我真想揍他啊!!!

      解决方案一定是有的,不然我也不会写。我在这里透露一点,在《Android.NDK.Beginner's.Guide》这本书上有相应的解决方案。若是您不喜欢去看这本书,那么您就等我下一篇博文吧,我准备在那里面给出解答。

      很晚了,赶地铁,回家。

    

      欢迎转载,转载请标明原文出处:http://www.cnblogs.com/xiaomiao/admin/EditPosts.aspx?opt=1

 

 

 

 

 

 

 

 

 

 

 

原文链接: https://www.cnblogs.com/xiaomiao/archive/2012/03/19/2406913.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    【博文翻译】Building Boost with NDK R5

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/44574

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月8日 下午9:12
下一篇 2023年2月8日 下午9:13

相关推荐