Android NDK編譯帶STL的 C/C++ 程式

來源:互聯網
上載者:User

我的這個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平台上慢慢調試吧, 希望不要有問題。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.