The latest AndoirdNDK r8efinally supports some of the most important C++11 library features: thread, mutex, atomic and so on. It is now straightforward to write multi-threaded programs using C++11 on Android, without hacking and jumping through hoops. All you need is downloading NDK r8e.

The most natural way to organize any C/C++ native code for an Android project, is to follow the Google guide: use a jni sub-directory to house Android.mk file (and optionally Application.mk file). The actual source code files, however, could be littered around; just refer them in Android.mk.

To enable C++11 threading, settings in both Application.mk and Android.mk are important. Below is the minimal examples of both files.

# Application.mk

# In NDK r8e, we have to use GCC 4.7 for C++11 features like thread, mutex, etc.
# The default GCC is 4.6, which does NOT define macro _GLIBCXX_HAS_GTHREADS,
# so you’ll get errors like "mutex in namespace std does not name a type" etc.

NDK_TOOLCHAIN_VERSION := 4.7

# GNU STL implements most C++11 features. Use either gnustl_static or gnustl_shared
# Without this your C++ code will not be able to access headers like <thread>, <mutex>

APP_STL := gnustl_static

# Use advanced ARM ABI
APP_ABI := armeabi-v7a

And Android.mk:

# Android.mk

# Regular boilerplate required by NDK
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)

# C++11 and threading enabling features.
# Otherwise c++11, pthread, rtti and exceptions are not enabled by default

LOCAL_CPPFLAGS := -std=c++11 -pthread -frtti -fexceptions

LOCAL_ARM_MODE := arm

LOCAL_MODULE := foo
LOCAL_SRC_FILES := foo.cpp

include $(BUILD_SHARED_LIBRARY)

Notice theredsettings in both files. Now, in foo.cpp, you can include headers like <thread> and <mutex>, and start using std::thread and std::mutex.


http://blog.csdn.net/liaowenfeng/article/details/12747465

更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. 一网打尽!二分查找解题模版与题型全面解析
  2. 数据结构与算法——最小生成树
  3. 钻漏洞,他用爬虫非法获取 1500 万余条个人
  4. 有效解决U盘文件变成exe格式的方法
  5. 几道和「黑洞照片」那种海量数据有关的算
  6. 2020 年 Python 开发者调查报告:PyCharm
  7. 程序员用python给了女友一个七夕惊喜!
  8. GitHub 热门:实用 Python 编程
  9. 使用Nginx 代理应用服务的端口,以及ssh连
  10. LeetCode 上最难的链表算法题,没有之一!