如何使用C ++ 11支持将Android增强为共享库 [英] How to build boost for android as shared library with c++11 support

查看:123
本文介绍了如何使用C ++ 11支持将Android增强为共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为带有c ++ 11支持的android构建boost_1.60.0(作为共享库).我正在使用最新的ndk(当前为android-ndk-r10e).构建主机是Windows-10.

I am trying to build boost_1.60.0 (as shared library) for android with c++11 support. I am using the latest ndk (currently android-ndk-r10e). The build host is Windows-10.

这是针对非开源项目的.据我了解,我不能使用gnustl_shared,我需要将c ++ _ shared用作android c ++运行时.

This is for a non-opensource project. So as far as I understand I cannot use gnustl_shared, and I need to use c++_shared as the android c++ runtime.

我的project-config.jam看起来像这样:

my project-config.jam looks like this:

androidNDKRoot = c:/android-ndk-r10e ;
 using gcc : android :
     $(androidNDKRoot)/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ :
      <root>$(androidNDKRoot)/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/
      <compileflags>-MMD
      <compileflags>-MP
      <compileflags>-MF
      <compileflags>-fpic 
      <compileflags>-ffunction-sections 
      <compileflags>-funwind-tables 
      <compileflags>-fstack-protector 
      <compileflags>-no-canonical-prefixes 
      <compileflags>-march=armv5te 
      <compileflags>-mtune=xscale 
      <compileflags>-msoft-float 
      <compileflags>-fno-rtti 
      <compileflags>-mthumb 
      <compileflags>-Os 
      <compileflags>-g 
      <compileflags>-DNDEBUG 
      <compileflags>-fomit-frame-pointer 
      <compileflags>-fno-strict-aliasing 
      <compileflags>-finline-limit=64
      <compileflags>-IC:/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libcxx/include
      <compileflags>-IC:/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include
      <compileflags>-IC:/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../../android/support/include
      <compileflags>-IC:/android-ndk-r10e/platforms/android-9/arch-arm/usr/include
      <compileflags>-Wa,--noexecstack 
      <compileflags>-Wformat 
      <compileflags>-Werror=format-security
      <compileflags>-DUNIX 
      <compileflags>-DANDROID 
      <compileflags>-Wl,--no-undefined
      <cxxflags>-fexceptions 
      <linkflags>-lc++_shared
      <archiver>$(androidNDKRoot)/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-ar
      <ranlib>$(androidNDKRoot)/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-ranlib
        ;        

build命令是:

b2 --toolset=gcc-android cxxflags="-std=c++11 " --prefix=..\boost_android_arm --builddir=./boost_android_arm/builddir target-os=linux --with-filesystem define=BOOST_FILESYSTEM_VERSION=3 link=shared runtime-link=shared threading=multi 

为了确定project-config.jam中的参数,我使用ndk-build构建了一个示例共享库,获取其调试消息,并提取了其使用的编译和链接命令.

In order to determine the parameters in the project-config.jam I've build a sample shared library using the ndk-build, get its debug messages, and extracted the compile and link commands it uses.

编译:

C:\android-ndk-r10e\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe,C:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -MMD -MP -MF ./obj/local/armeabi/objs/someLib/./Unity1.o.d -fpic -ffunction-sections -funwind-tables -fstack-protector -no-canonical-prefixes -march=armv5te -mtune=xscale -msoft-float -fno-exceptions -fno-rtti -mthumb -Os -g -DNDEBUG -fomit-frame-pointer -fno-strict-aliasing -finline-limit=64 -Ijni/../../library/../../../../ -Ijni/../../library/../../../../src/ -IC:/android-ndk-r10e/sources/cxx-stl/llvm-libc++/libcxx/include -IC:/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../llvm-libc++abi/libcxxabi/include -IC:/android-ndk-r10e/sources/cxx-stl/llvm-libc++/../../android/support/include -Ijni/../../library -DANDROID -DHAVE_CONFIG_H -DSESTEK_ANDROID_XERCES_HACK -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -fno-strict-aliasing -frtti -fexceptions -DUNIX -DANDROID -IC:/android-ndk-r10e/platforms/android-9/arch-arm/usr/include -c jni/../../library/./Unity1.cpp -o ./obj/local/armeabi/objs/someLib/./Unity1.o,...)

链接:

C:\android-ndk-r10e\toolchains\arm-linux-androideabi-4.8\prebuilt\windows-x86_64\bin\arm-linux-androideabi-g++.exe,C:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++ -Wl,-soname,libsomeLib.so -shared --sysroot=C:/android-ndk-r10e/platforms/android-9/arch-arm ./obj/local/armeabi/objs/someLib/./Unity1.o -lgcc ./obj/local/armeabi/libc++_shared.so -no-canonical-prefixes -Wl,--no-undefined -Wl,-z,noexecstack -Wl,-z,relro -Wl,-z,now -mthumb -lc -lm -o ./obj/local/armeabi/libsomeLib.so,...)

为简洁起见,我仅为该试用版构建文件系统,但最终我计划至少构建线程,文件系统,date_time,asio和日志库.
最后,我得到的错误如下.

For brevity I've only build filesystem for this trial but in the end I plan to build at least thread, filesystem, date_time, asio and log libraries.
Finally the error I get is as follows.

...patience...
...found 660 targets...
...updating 13 targets...
gcc.compile.c++ bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\error_code.o
gcc.link.dll bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\libboost_system-gcc-mt-1_60.so.1.60.0
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot open crtbegin_so.o: No such file or directory
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lrt
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot open crtend_so.o: No such file or directory
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lc++_shared
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lstdc++
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lm
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lc
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -ldl
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: cannot find -lc
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\error_code.o: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\error_code.o: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC
c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/../lib/gcc/arm-linux-androideabi/4.8/../../../../arm-linux-androideabi/bin/ld.exe: error: bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\error_code.o: requires unsupported dynamic reloc R_ARM_REL32; recompile with -fPIC
    ./boost/system/detail/error_code.ipp:458: error: undefined reference to '__dso_handle'
    ./boost/system/detail/error_code.ipp:464: error: undefined reference to '__dso_handle'
    ./boost/system/detail/error_code.ipp:158: error: undefined reference to '__dso_handle'
collect2.exe: error: ld returned 1 exit status
    "c:/android-ndk-r10e/toolchains/arm-linux-androideabi-4.8/prebuilt/windows-x86_64/bin/arm-linux-androideabi-g++"    -o "bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\libboost_system-gcc-mt-1_60.so.1.60.0"  -shared -Wl,--start-group "bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\error_code.o"  -Wl,-Bstatic  -Wl,-Bdynamic -lrt -Wl,--end-group -lc++_shared -pthread
...failed gcc.link.dll bin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi\libboost_system-gcc-mt-1_60.so.1.60.0...
...skipped <pstage\lib>libboost_system-gcc-mt-1_60.so.1.60.0 for lack of <pbin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi>libboost_system-gcc-mt-1_60.so.1.60.0...
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\codecvt_error_category.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\operations.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\path.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\path_traits.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\portability.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\unique_path.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\utf8_codecvt_facet.o
gcc.compile.c++ bin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi\windows_file_codecvt.o
...skipped <pbin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi>libboost_filesystem-gcc-mt-1_60.so.1.60.0 for lack of <pbin.v2\libs\system\build\gcc-android\release\target-os-linux\threading-multi>libboost_system-gcc-mt-1_60.so.1.60.0...
...skipped <pstage\lib>libboost_filesystem-gcc-mt-1_60.so.1.60.0 for lack of <pbin.v2\libs\filesystem\build\gcc-android\release\target-os-linux\threading-multi>libboost_filesystem-gcc-mt-1_60.so.1.60.0...
...failed updating 1 target...
...skipped 3 targets...
...updated 9 targets...

该错误表明它需要目录才能找到必要的库,但是问题是,在android ndk下,有几个文件名为rt和crtbegin_so.o等等.我想我需要让编译器自己确定正确的目录.

The error tells that it needs the directory to find the necessary libraries, but the problem is that under android ndk there are several files with names rt and crtbegin_so.o and such. I guess I need to make the compiler determine the correct directory itself.

总而言之,我实际上需要的是为具有c ++ 11支持的android构建boost共享库.因此,我可以接受接受您的帮助,方法是使用上述构建参数为我指明正确的方向,或者为我提供一个工作示例,以便我自己制定详细信息.

With all being said what I need actually is to build boost shared libraries for android with c++11 support. So I may accept your help either by pointing me in the right direction using the above build parameters or providing me a working sample so I can work out the details myself.

哦,还有一件事:如果我在构建命令中使用link=static而不是link=shared,则构建成功.但是我还没有尝试过产生的静态库.

Oh also one more thing: if I use link=static instead of link=shared in the build command the build succeeds. But I have not tried the produced static libraries.

推荐答案

使用NDK在Linux上构建Boost

我知道您在问有关Windows的问题,但是我想在macOS上执行此操作,但它失败了,几乎出现了确切的错误.我终于崩溃了,并在我的 linode服务器上进行了工作,它没有任何问题.这对我来说,他们在测试其他平台方面并没有真正做好.只能在macOS上进行静态编译,就像在Windows上一样.

Building Boost on Linux using the NDK

I know you're asking about Windows, but I wanted to do this on macOS and it failed with nearly the exact error. I finally broke down and did it on my linode server it worked without a problem. This says to me that they aren't really doing a good job of testing other platforms. Compiling static only on macOS works as you also discovered on Windows.

参考点

  • NDK R13
  • 提升1.62.0
  • 使用clang ++测试; g ++也可以使用

如果您想知道为什么我使用clang,发行说明中会显示以下消息:

If you're wondering why I'm using clang, the Release Notes have the following message:

不再支持GCC.它不会仅从NDK中删除 还没有,但是不再接受反向移植.直到将其删除 在libc ++变得足够稳定以成为默认值之后,某些部分 的gnustl仍然与Clang不兼容.它可能会被删除 在那之后.

GCC is no longer supported. It will not be removed from the NDK just yet, but is no longer receiving backports. It cannot be removed until after libc++ has become stable enough to be the default, as some parts of gnustl are still incompatible with Clang. It will likely be removed after that point.

user-config.jam

我将此文件放在主目录中. uck.

user-config.jam

I placed this file in my home directory. Yuck.

androidNDKRoot = /path/to/ndk-R13-standalone ;

using clang : android
:
$(androidNDKRoot)/bin/arm-linux-androideabi-clang++
:
;

在boost中修改libtool.m4以避免对库进行版本控制

boost源中tools/build/src/engine/boehm_gc/libtool.m4下的

libtool.m4没有引用android,您需要将linux*)部分中的version_type=linux更改为version_type=none.这将导致出现符号链接,而没有将版本号附加到在输出中链接到版本共享库的末尾.

Modifying libtool.m4 in boost to avoid versioning of the libraries

libtool.m4 under tools/build/src/engine/boehm_gc/libtool.m4 in the boost source has no reference to android and you'll need to change version_type=linux in the section linux*) to version_type=none. This will cause symbolic links to appear without the version number appended to the end linked to the versioned shared libraries in the output.

目标操作系统必须为Android,以避免传递-lrt标志,这将导致共享链接失败.

Target OS MUST be android to avoid the -lrt flag being passed which will cause shared linking to fail.

./b2 \
   -d+2 \
   -j 4 \
   --reconfigure \
   target-os=android \
   toolset=clang-android \
   include=${ANDROID_NDK_STANDALONE}/include/c++/4.9.x \
   link=static,shared \
   variant=debug,release \
   threading=multi \
   --layout=versioned \
   --prefix=${BOOST_INSTALL_DIR} \
   install

这篇关于如何使用C ++ 11支持将Android增强为共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆