我的這個C/C++程式, 可以成功的交叉編譯到中興,烽火的機頂盒上運行, 原以為編譯到android應該也是一件簡單的事情, 但是做起來遠比我想象的複雜得多。
我的程式中有 .C 檔案 和 .CPP 檔案 , 其中 .CPP 檔案中用到了 STL的string, 編譯成功後死活連結不到std::string庫,提示報錯 undefined reference to `std::basic_string 。
折騰了兩天準備用NDK來 編譯 stlport庫, 但是以失敗告終,報錯如下:
[root@localhost jni]# /home/android-ndk-r6b/ndk-build
Compile++ thumb : stlport_shared <= dll_main.cpp
In file included from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stl/_stdexcept_base.h:25,
from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stl/_ios_base.h:22,
from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stl/_ios.h:23,
from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stl/_ostream.h:24,
from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stl/_string_io.h:23,
from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/string:37,
from /home/android-ndk-r6b/samples/STLport-5.2.1/jni/src/dll_main.cpp:49:
/home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stl/_exception.h:56:34: error: exception: No such file or directory
編譯stlport的Android.mk檔案內容如下所列, 不知道為何會報上面的錯誤, 沒有找到解決辦法, 如果哪位大俠找到原因, 成功編譯了stlport, 還望多多指點為謝。
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)libstlport_path := $(call my-dir)libstlport_src_files := \ src/dll_main.cpp \ src/fstream.cpp \ src/strstream.cpp \ src/sstream.cpp \ src/ios.cpp \ src/stdio_streambuf.cpp \ src/istream.cpp \ src/ostream.cpp \ src/iostream.cpp \ src/codecvt.cpp \ src/collate.cpp \ src/ctype.cpp \ src/monetary.cpp \ src/num_get.cpp \ src/num_put.cpp \ src/num_get_float.cpp \ src/num_put_float.cpp \ src/numpunct.cpp \ src/time_facets.cpp \ src/messages.cpp \ src/locale.cpp \ src/locale_impl.cpp \ src/locale_catalog.cpp \ src/facets_byname.cpp \ src/complex.cpp \ src/complex_io.cpp \ src/complex_trig.cpp \ src/string.cpp \ src/bitset.cpp \ src/allocators.cpp \ src/c_locale.c \ src/cxa.c \libstlport_cflags := -D_GNU_SOURCElibstlport_cppflags := -fuse-cxa-atexitlibstlport_c_includes := $(libstlport_path) \ $(libstlport_path)/stlport /home/android-ndk-r6b/samples/STLport-5.2.1/jni \ /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport \ /home/android-ndk-r6b/samples/STLport-5.2.1/jni/stlport/stlLOCAL_MODULE := stlport_staticLOCAL_SRC_FILES := $(libstlport_src_files)LOCAL_CFLAGS := $(libstlport_cflags)LOCAL_CPPFLAGS := $(libstlport_cppflags)LOCAL_C_INCLUDES := $(libstlport_c_includes)LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)include $(BUILD_STATIC_LIBRARY)include $(CLEAR_VARS)LOCAL_MODULE := stlport_sharedLOCAL_SRC_FILES := $(libstlport_src_files)LOCAL_CFLAGS := $(libstlport_cflags)LOCAL_CPPFLAGS := $(libstlport_cppflags)LOCAL_C_INCLUDES := $(libstlport_c_includes)LOCAL_EXPORT_C_INCLUDES := $(LOCAL_C_INCLUDES)include $(BUILD_SHARED_LIBRARY)
stlport編譯失敗, 只能在另外想辦法了, 最後終於找到了問題所在, 把 .C 檔案尾碼修改為 .CPP後 問題得到解決。
我應用程式的 Android.mk檔案如下 :
LOCAL_PATH := $(call my-dir)include $(CLEAR_VARS)LOCAL_MODULE := cloudstore_MTDLOCAL_SRC_FILES := debug.cpp hash.cpp listex.cpp md5.cpp main.cpp http_fetcher.cpp spthreadpool.cpp storex.cpp markup.cpp fusex.cppLOCAL_C_INCLUDES := /home/android-ndk-r6b/samples/fuse-android/jni/include \ /home/android-ndk-r6b/samples/sqlite-android/jni# /home/android-ndk-r6b/platforms/android-9/arch-arm/usr/include \# /home/android-ndk-r6b/sources/cxx-stl/gnu-libstdc++/include \# /home/android-ndk-r6b/samples/fuse-android/jni/include $(INCDIR)LOCAL_CFLAGS := -Wall -lulockmgr -pipe -D_FILE_OFFSET_BITS=64 -D_DEBUG -DANDROID_SDK -DUSE_OPEN -DUSE_THREAD -DDEBUG -O2INCLIB := /home/android-ndk-r6b/samplesLOCAL_LDLIBS := $(INCLIB)/fuse-android/obj/local/armeabi/libfuse.a \ $(INCLIB)/libiconv/obj/local/armeabi/libiconv.a \ $(INCLIB)/sqlite-android/obj/local/armeabi/libsqlite3.ainclude $(BUILD_EXECUTABLE)
我應用程式用到了STL,所以還得需要一個 Application.mk 檔案, 內容如下 :
APP_CFLAGS += -fexceptionsAPP_STL := gnustl_static
另外, 由於我的應用程式還需要用到 libiconv.a這個庫, 但是android並沒有這方面的支援, 所以我下載了libiconv的源碼來通過NDK編譯。
編譯libiconv的 Android.mk檔案內容如下:
LOCAL_PATH:= $(call my-dir)# first lib, which will be built staticallyinclude $(CLEAR_VARS)LOCAL_MODULE := iconvLOCAL_SRC_FILES := libcharset/lib/localcharset.c lib/iconv.c lib/relocatable.cLOCAL_C_INCLUDES := include lib libcharset/includeLOCAL_CFLAGS := -Wno-multichar -D_ANDROID -DLIBDIR="c" -DBUILDING_LIBICONV -DIN_LIBRARY#LOCAL_PRELINK_MODULE := falseinclude $(BUILD_STATIC_LIBRARY)
編譯libiconv的Application.mk檔案內容如下:
APP_MODULES := libiconv
至此, 應用程式編譯完畢, 下來在Android平台上慢慢調試吧, 希望不要有問題。