如何在 Android NDK 和 STLport 中使用 boost 库(包括 shared_ptr)

2021-12-24 00:00:00 android-ndk c++ boost stlport

这与其说是一个问题,不如说是一个答案,因为我已经弄清楚了,至少就干净利落地编译库而言.我的主要问题是让 shared_ptr 工作.

This is more of an answer than a question, because I've figured it out, at least as far as cleanly compiling the library. The main issue for me was to get shared_ptr working.

成分:

Boost v. 1.45.0

Boost v. 1.45.0

http://www.anddev.org/viewtopic.php?p=29939.

NDK 的 r4b 版本.

Version r4b of the NDK.

路线:

在您的 Android.mk 文件中添加:

In your Android.mk file add:

LOCAL_CFLAGS += -DBOOST_EXCEPTION_DISABLE -D_STLP_NO_EXCEPTIONS -DOS_ANDROID -D_STLP_USE_SIMPLE_NODE_ALLOC

在 stlport/stl/_string.h 的第 613 行删除对 __stl_throw_length_error 的调用.如果您愿意,可以使用 _STLP_NO_EXCEPTIONS.

Remove the call to __stl_throw_length_error at line 613 of stlport/stl/_string.h. You can use _STLP_NO_EXCEPTIONS if you like.

在第 261 行之后编辑 boost/boost/smart_ptr/shared_ptr.hpp 以消除 shared_ptr 构造函数中对 boost::throw_exception 的调用.我在方法的整个主体周围使用了 #ifndef BOOST_EXCEPTION_DISABLE.(但请参阅下面的答案.)

Edit boost/boost/smart_ptr/shared_ptr.hpp after line 261 to get rid of the call to boost::throw_exception in the shared_ptr constructor. I used #ifndef BOOST_EXCEPTION_DISABLE around the entire body of the method. (But see the answer below.)

接下来您需要提供一些缺失的部分.使用以下内容创建头文件:

Next you need to supply some missing pieces. Create a header file with the following:

#ifdef OS_ANDROID

#include <exception>

namespace std
{
    struct bad_alloc : public exception { bad_alloc operator()(){}};
}

#endif

和一个带有精简异常类以支持 bad_alloc 的源文件:

and a source file with a stripped-down exception class to support bad_alloc:

#ifdef OS_ANDROID

#include <exception>

namespace std
{
    exception::exception() {}
    exception::~exception() {}
    const char* exception::what() const {}
}

#endif

在包含 boost/shared_ptr.hpp 的任何位置包含标题.编译源代码并将其添加到您的库中.

Include the header wherever you're including boost/shared_ptr.hpp. Compile the source and add it to your library.

推荐答案

事实证明,这种方法在编译可调试库时并不完全有效.发布库是用 -O2 编译的,它优化了一些缺陷,但调试库是用 -O0 完成的,这揭示了一些额外的问题.此外,我对必须编辑 boost 文件不太满意.所以通过一些额外的研究,我想出了以下解决方案.

It turned out that this approach does not entirely work when compiling a debuggable library. The release library is compiled with -O2 which optimizes out some infelicities, but the debug library is done with -O0 which reveals some additional problems. Furthermore, I wasn't too happy about having to edit the boost files. So with some additional study, I've come up with the following solution.

首先,不要编辑任何 boost 文件.而是将以下内容添加到 std 命名空间内的标头中:

First, don't edit any of the boost files. Instead add the following to the header within the std namespace:

struct bad_cast : public exception {bad_cast operator()(){}};

接下来将以下内容添加到源文件中:

Next add the following to the source file:

namespace boost
{
    void throw_exception(std::exception const&) {}
}

现在即使在 AndroidManifest.xml 中使用 android:debuggable="true" 也可以编译并链接到应用程序中.它不在模拟器中运行,但是在我包含这个库之前它也没有这样做.

This now compiles and links into the application even with android:debuggable="true" in AndroidManifest.xml. It doesn't run in the emulator, but then it wasn't doing that before I included this library either.

相关文章