如何将 android 的 boost 构建为支持 c++11 的共享库 [英] How to build boost for android as shared library with c++11 support

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

问题描述

我正在尝试为支持 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
        ;        

构建命令是:

b2 --toolset=gcc-android cxxflags="-std=c++11 " --prefix=..oost_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	oolchainsarm-linux-androideabi-4.8prebuiltwindows-x86_64inarm-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	oolchainsarm-linux-androideabi-4.8prebuiltwindows-x86_64inarm-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.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multierror_code.o
gcc.link.dll bin.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multilibboost_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.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multierror_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.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multierror_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.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multierror_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.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multilibboost_system-gcc-mt-1_60.so.1.60.0"  -shared -Wl,--start-group "bin.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multierror_code.o"  -Wl,-Bstatic  -Wl,-Bdynamic -lrt -Wl,--end-group -lc++_shared -pthread
...failed gcc.link.dll bin.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multilibboost_system-gcc-mt-1_60.so.1.60.0...
...skipped <pstagelib>libboost_system-gcc-mt-1_60.so.1.60.0 for lack of <pbin.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multi>libboost_system-gcc-mt-1_60.so.1.60.0...
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multicodecvt_error_category.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multioperations.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multipath.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multipath_traits.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multiportability.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multiunique_path.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multiutf8_codecvt_facet.o
gcc.compile.c++ bin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multiwindows_file_codecvt.o
...skipped <pbin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-multi>libboost_filesystem-gcc-mt-1_60.so.1.60.0 for lack of <pbin.v2libssystemuildgcc-android
elease	arget-os-linux	hreading-multi>libboost_system-gcc-mt-1_60.so.1.60.0...
...skipped <pstagelib>libboost_filesystem-gcc-mt-1_60.so.1.60.0 for lack of <pbin.v2libsfilesystemuildgcc-android
elease	arget-os-linux	hreading-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 上编译 static 的工作方式与您在 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.

用户配置.jam

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

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的引用,你需要改变version_type=linux 部分中的 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

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

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