现在的位置: 首页 > 综合 > 正文

在NDK中使用shared_ptr

2018年02月15日 ⁄ 综合 ⁄ 共 1408字 ⁄ 字号 评论关闭

NDK版本:r10c

Application.mk文件内容如下:

APP_STL := gnustl_shared
NDK_TOOLCHAIN_VERSION := 4.8
APP_CPPFLAGS += -std=c++0x
APP_CPPFLAGS += -fexceptions
APP_CPPFLAGS += -frtti

解释:

我使用的是C++0x中的std::shared_ptr。所以,GCC版本必须要支持C++0x。

NDK_TOOLCHAIN_VERSION:=4.8就指定了GCC的版本为4.8。

关键要指定STL的版本为gnustl_shared,而不能是stlport_shared。

在gnustl版本中,shared_ptr定义在NDK根目录\sources\cxx-stl\gnu-libstdc++\4.8\include\memory文件中,具体内容如下:

#if __cplusplus >= 201103L
#  include <exception>        	  // std::exception
#  include <typeinfo>         	  // std::type_info in get_deleter
#  include <iosfwd>           	  // std::basic_ostream
#  include <ext/atomicity.h>
#  include <ext/concurrence.h>
#  include <bits/functexcept.h>
#  include <bits/stl_function.h>  // std::less
#  include <bits/uses_allocator.h>
#  include <type_traits>
#  include <functional>
#  include <debug/debug.h>
#  include <bits/unique_ptr.h>
#  include <bits/shared_ptr.h>
#  if _GLIBCXX_USE_DEPRECATED
#    include <backward/auto_ptr.h>
#  endif
#else
#  include <backward/auto_ptr.h>
#endif

只要GCC的版本够高,__cplusplus就会大于201103L。就可以使用shared_ptr了。

而stlport使用boost库,支持shared_ptr。

在stlport版本中,shared_ptr定义在NDK根目录\sources\cxx-stl\stlport\stlport\memory文件中,具体内容如下:

#if !defined(_STLP_NO_EXTENSIONS) && defined(_STLP_USE_BOOST_SUPPORT)

namespace boost {

class bad_weak_ptr;
template<class T> class shared_ptr;
template<class T> class weak_ptr;
template<class T> class enable_shared_from_this;
template<class D, class T> D * get_deleter(shared_ptr<T> const & p);

我不想使用Boost,就没有深入研究如何使用stlport的shared_ptr。

顺便说一下,如果你的程序由多个so构成,如果使用c++开发,一定要链接动态运行库。具体原因见NDK官方文档。

抱歉!评论已关闭.