转自http://blog.csdn.net/keensword007/article/details/5720636

用NDK编译时出现这么个错误undefined reference to `android::Mutex::lock()'

起初以为没有链接必要的so,结果加上了 -lutils 也不行。所以google 了一下,发现有人遇到过此问题,如下:

察看了一下ndk中的STABLE-APIS.TXT文档,上面有这样一句:

Note that the Android C library includes support for pthread (<pthread.h>),

so "LOCAL_LIBS := -lpthread" is not needed. The same is true for real-time

extensions (-lrt on typical Linux distributions).

也就是说使用多线程,并不需要增加-lpthread编译选项,看来解决问题还需要从源代码入手。察看frameworks/base/include/utils/threads.h文件,关于Mutex有如下代码片断:

#if defined(HAVE_PTHREADS)

inline Mutex::Mutex() {

pthread_mutex_init(&mMutex, NULL);

}

inline Mutex::Mutex(const char* name) {

pthread_mutex_init(&mMutex, NULL);

}

inline Mutex::Mutex(int type, const char* name) {

if (type == SHARED) {

pthread_mutexattr_t attr;

pthread_mutexattr_init(&attr);

pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_SHARED);

pthread_mutex_init(&mMutex, &attr);

pthread_mutexattr_destroy(&attr);

} else {

pthread_mutex_init(&mMutex, NULL);

}

}

inline Mutex::~Mutex() {

pthread_mutex_destroy(&mMutex);

}

inline status_t Mutex::lock() {

return -pthread_mutex_lock(&mMutex);

}

inline void Mutex::unlock() {

pthread_mutex_unlock(&mMutex);

}

inline status_t Mutex::tryLock() {

return -pthread_mutex_trylock(&mMutex);

}

#endif // HAVE_PTHREADS

也就是说如果定义了HAVE_PTHREADS宏,就有Mutex的相关方法实现,如果没有定义这个宏,Mutex的方法又是如何实现的,下面看看frameworks/base/libs/utils/Threads.cpp中的代码:
#if defined(HAVE_PTHREADS) // implemented as inlines in threads.h #elif defined(HAVE_WIN32_THREADS) Mutex::Mutex() { HANDLE hMutex; assert(sizeof(hMutex) == sizeof(mState)); hMutex = CreateMutex(NULL, FALSE, NULL); mState = (void*) hMutex; } Mutex::Mutex(const char* name) { // XXX: name not used for now HANDLE hMutex; assert(sizeof(hMutex) == sizeof(mState)); hMutex = CreateMutex(NULL, FALSE, NULL); mState = (void*) hMutex; } Mutex::Mutex(int type, const char* name) { // XXX: type and name not used for now HANDLE hMutex; assert(sizeof(hMutex) == sizeof(mState)); hMutex = CreateMutex(NULL, FALSE, NULL); mState = (void*) hMutex; } Mutex::~Mutex() { CloseHandle((HANDLE) mState); } status_t Mutex::lock() { DWORD dwWaitResult; dwWaitResult = WaitForSingleObject((HANDLE) mState, INFINITE); return dwWaitResult != WAIT_OBJECT_0 ? -1 : NO_ERROR; } void Mutex::unlock() { if (!ReleaseMutex((HANDLE) mState)) LOG(LOG_WARN, "thread", "WARNING: bad result from unlocking mutex/n"); } status_t Mutex::tryLock() { DWORD dwWaitResult; dwWaitResult = WaitForSingleObject((HANDLE) mState, 0); if (dwWaitResult != WAIT_OBJECT_0 && dwWaitResult != WAIT_TIMEOUT) LOG(LOG_WARN, "thread", "WARNING: bad result from try-locking mutex/n"); return (dwWaitResult == WAIT_OBJECT_0) ? 0 : -1; } #else #error "Somebody forgot to implement threads for this platform." #endif
也就是说,要么定义HAVE_PTHREADS宏,要么定义HAVE_WIN32_THREADS宏,否则,Mutex类的方法就没有实现代码。对于andorid来说,需要定义HAVE_PTHREADS宏。从上面的代码中还可以看出,android的线程实现实际上还是使用了pthread库,只是在上面进行了一个封装。在Android.mk中增加下面一条语句,问题解决:
LOCAL_CXXFLAGS := -DHAVE_PTHREADS

更多相关文章

  1. android listView 总结
  2. listview 常用智识总结
  3. 详解Android数据存储―使用SQLite数据库
  4. android 使用mediaplayer播放报java.io.IOException: setDataSou
  5. android 程序运行出现错误 Unable to execute dex: java.nio.Buf
  6. Eclipse 插件安装方法和插件加载失败解决办法
  7. node.js+Android(安卓)http请求响应
  8. android小注意
  9. Android(安卓)Canvas drawArc方法介绍

随机推荐

  1. Android各个版本API的区别
  2. [Android(安卓)Pro] 注册 Google Play 开
  3. 2月收藏
  4. Android控件及API说明(二)
  5. Android(安卓)浅谈MatrixCursor
  6. Android(安卓)Studio - 第四十四期 倒计
  7. SJ64 拿起工具(Android(安卓)Studio)开始造
  8. Android(安卓)app实现重启
  9. Android: android sensors, HAL范例
  10. Android移动端音视频的快速开发教程(五)