在NDK中使用shared_ptr

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官方文档。


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值