如何将 boost 库(包括共享指针)与 Android NDK 和 STLport 结合使用
这更像是一个答案而不是一个问题,因为我已经弄清楚了,至少就干净地编译库而言是这样。对我来说主要问题是让shared_ptr 工作。
成分:
Boost v. 1.45.0
STLport 版本位于 http://www. anddev.org/viewtopic.php?p=29939。
NDK 的 r4b 版本。
说明:
在 Android.mk 文件中添加:
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。
在第 261 行之后编辑 boost/boost/smart_ptr/shared_ptr.hpp 以消除对 shared_ptr 构造函数中的 boost:: throw_exception 的调用。我在方法的整个主体中使用了#ifndef BOOST_EXCEPTION_DISABLE。 (但请参阅下面的答案。)
接下来您需要提供一些缺失的部分。创建一个包含以下内容的头文件:
#ifdef OS_ANDROID
#include <exception>
namespace std
{
struct bad_alloc : public exception { bad_alloc operator()(){}};
}
#endif
以及一个带有精简异常类的源文件以支持 bad_alloc:
#ifdef OS_ANDROID
#include <exception>
namespace std
{
exception::exception() {}
exception::~exception() {}
const char* exception::what() const {}
}
#endif
在包含 boost/shared_ptr.hpp 的任何位置都包含该标头。编译源代码并将其添加到您的库中。
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.
Ingredients:
Boost v. 1.45.0
The version of STLport at http://www.anddev.org/viewtopic.php?p=29939.
Version r4b of the NDK.
Directions:
In your Android.mk file add:
LOCAL_CFLAGS += -DBOOST_EXCEPTION_DISABLE -D_STLP_NO_EXCEPTIONS -DOS_ANDROID -D_STLP_USE_SIMPLE_NODE_ALLOC
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.
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
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
Include the header wherever you're including boost/shared_ptr.hpp. Compile the source and add it to your library.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
事实证明,这种方法在编译可调试库时并不完全有效。发布库是使用 -O2 编译的,它优化了一些缺陷,但调试库是使用 -O0 完成的,这揭示了一些额外的问题。此外,我对编辑 boost 文件不太高兴。因此,通过一些额外的研究,我提出了以下解决方案。
首先,不要编辑任何 boost 文件。而是将以下内容添加到 std 命名空间内的标头:
接下来将以下内容添加到源文件:
即使 AndroidManifest.xml 中包含 android:debuggable="true",现在也会编译并链接到应用程序。它不在模拟器中运行,但在我包含这个库之前它也没有这样做。
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.
First, don't edit any of the boost files. Instead add the following to the header within the std namespace:
Next add the following to the source file:
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.
值得注意的是,NDK r5 附带了 STLport 和 GNU STL,因此现在不再需要进行黑客攻击,因为 NDK C++ 编译器中存在 a) STL 支持 b) 异常支持。
Notably, NDK r5 comes with STLport, and the GNU STL, and so the hacks here are no longer going to be necessary now that there is a) STL support b) exception support in the NDK C++ compiler.
特别是针对shared_ptr 的另一个解决方法是使用boost::intrusive_ptr 来代替。这并不总是可能的,但适合我的情况。
Another workaround for shared_ptr in particular is to use boost::intrusive_ptr instead. This is not always possible, but worked for my situation.
当前版本的 Android NDK (r9) 现在支持异常。
各种运行时的功能各不相同。请参阅此表:
stlport 可以在非 GPL 二进制文件中使用。它仍然被标记为实验性的,但您可以将它与 clang 和 gcc 一起使用。
请参阅http://developer.android.com/tools/sdk/ndk/
The current version of Android NDK (r9) now supports exceptions.
The capabilities of the various runtimes vary. See this table:
stlport can get used in non-GPL binarys. It's still flagged as experimantal, but you can use it with clang and gcc.
See http://developer.android.com/tools/sdk/ndk/