undefined reference to `android::Mutex::lock()'


轉自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
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM