fix Android building error on ubuntu 11.10

系統升級 ubuntu 11.10 過程相當順利,瀏覽器資料也多虧 sync 的功能一下子就回復,雲端的時代來了(笑)。
升級後編譯 Android 出現錯誤,仔細比對後因為 gcc/glibc 的版本不同所造成,許多問題 google 真的可以找到解法省下不少時間,不過也要慢慢累積經驗,可以看到問題直覺反應如何解決!
先把遇到的問題跟解法一點一滴記錄下來吧。
  • Xubuntu 11.10 Oneiric Ocelot AMD64
  • gcc version 4.6.1 (Ubuntu/Linaro 4.6.1-9ubuntu3) (system default)
  • Android 2.2.1 (MDK3D BSP)

  • compile the Android 2.3 with your installed GCC 4.6, you will receive the following error message:

    host Executable: acp (out/host/linux-x86/obj/EXECUTABLES/acp_intermediates/acp)
    host SharedLib: libneo_cs (out/host/linux-x86/obj/lib/libneo_cs.so)
    host C++: libutils <= frameworks/base/libs/utils/RefBase.cpp frameworks/base/libs/utils/RefBase.cpp: In member function ‘void android::RefBase::weakref_type::trackMe(bool, bool)’: frameworks/base/libs/utils/RefBase.cpp:483:67: error: passing ‘const android::RefBase::weakref_impl’ as ‘this’ argument of ‘void android::RefBase::weakref_impl::trackMe(bool, bool)’ discards qualifiers [-fpermissive] make: *** [out/host/linux-x86/obj/STATIC_LIBRARIES/libutils_intermediates/RefBase.o] Error 1 make: *** Waiting for unfinished jobs....

    To fix that, open a terminal and run (assuming you are in the folder android):
    diff  frameworks/base/libs/utils/Android.mk
    
    - LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS)
    + LOCAL_CFLAGS += -DLIBUTILS_NATIVE=1 $(TOOL_CFLAGS) -fpermissive
    

  • to solve undefined reference to `pthread_create'
    host Executable: aapt (out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt)
    out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_get':
    android/bin/system/core/libcutils/threads.c:27: undefined reference to `pthread_getspecific'
    out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_set':
    android/bin/system/core/libcutils/threads.c:36: undefined reference to `pthread_key_create'
    android/bin/system/core/libcutils/threads.c:44: undefined reference to `pthread_setspecific'
    collect2: ld returned 1 exit status
    make: *** [out/host/linux-x86/obj/EXECUTABLES/aapt_intermediates/aapt] error 1
    
    diff frameworks/base/tools/appt/Android.mk
    
     ifeq ($(HOST_OS),linux) 
    -LOCAL_LDLIBS += -lrt 
    +LOCAL_LDLIBS += -lrt -lpthread 
     endif 
    
  • another undefined reference to `pthread_create'
    out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_get':
    android/bin/system/core/libcutils/threads.c:27: undefined reference to `pthread_getspecific'
    out/host/linux-x86/obj/STATIC_LIBRARIES/libcutils_intermediates/libcutils.a(threads.o): In function `thread_store_set':
    android/bin/system/core/libcutils/threads.c:36: undefined reference to `pthread_key_create'
    android/bin/system/core/libcutils/threads.c:44: undefined reference to `pthread_setspecific'
    collect2: ld returned 1 exit status
    make: *** [out/host/linux-x86/obj/EXECUTABLES/localize_intermediates/localize] error 1
    
    diff frameworks/base/tools/localize/Android.mk
    
     ifeq ($(HOST_OS),linux) 
    -LOCAL_LDLIBS += -lrt 
    +LOCAL_LDLIBS += -lrt -lpthread 
     endif 
    

後來看到一個大陸的網頁,寫得相當完整。大陸急起直追的力道真的很強勁,該百尺竿頭!