我的这个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_SOURCE
libstlport_cppflags := -fuse-cxa-atexit
libstlport_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/stl
LOCAL_MODULE := stlport_static
LOCAL_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_shared
LOCAL_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)
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_SOURCE
libstlport_cppflags := -fuse-cxa-atexit
libstlport_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/stl
LOCAL_MODULE := stlport_static
LOCAL_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_shared
LOCAL_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_MTD
LOCAL_SRC_FILES := debug.cpp hash.cpp listex.cpp md5.cpp main.cpp http_fetcher.cpp spthreadpool.cpp storex.cpp markup.cpp
fusex.cpp
LOCAL_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 -O2
INCLIB := /home/android-ndk-r6b/samples
LOCAL_LDLIBS := $(INCLIB)/fuse-android/obj/local/armeabi/libfuse.a \
$(INCLIB)/libiconv/obj/local/armeabi/libiconv.a \
$(INCLIB)/sqlite-android/obj/local/armeabi/libsqlite3.a
include $(BUILD_EXECUTABLE)
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := cloudstore_MTD
LOCAL_SRC_FILES := debug.cpp hash.cpp listex.cpp md5.cpp main.cpp http_fetcher.cpp spthreadpool.cpp storex.cpp markup.cpp
fusex.cpp
LOCAL_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 -O2
INCLIB := /home/android-ndk-r6b/samples
LOCAL_LDLIBS := $(INCLIB)/fuse-android/obj/local/armeabi/libfuse.a \
$(INCLIB)/libiconv/obj/local/armeabi/libiconv.a \
$(INCLIB)/sqlite-android/obj/local/armeabi/libsqlite3.a
include $(BUILD_EXECUTABLE)
我应用程序用到了STL,所以还得需要一个 Application.mk 文件, 内容如下 :
APP_CFLAGS += -fexceptions
APP_STL := gnustl_static
APP_CFLAGS += -fexceptions
APP_STL := gnustl_static
另外, 由于我的应用程序还需要用到 libiconv.a这个库, 但是android并没有这方面的支持,
所以我下载了libiconv的源码来通过NDK编译。
编译libiconv的 Android.mk文件内容如下:
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
include $(CLEAR_VARS)
LOCAL_MODULE :=iconv
LOCAL_SRC_FILES :=libcharset/lib/localcharset.c lib/iconv.c lib/relocatable.c
LOCAL_C_INCLUDES :=includelib libcharset/include
LOCAL_CFLAGS := -Wno-multichar -D_ANDROID-DLIBDIR="c"-DBUILDING_LIBICONV -DIN_LIBRARY
#LOCAL_PRELINK_MODULE :=false
include $(BUILD_STATIC_LIBRARY)
LOCAL_PATH:= $(call my-dir)
# first lib, which will be built statically
include $(CLEAR_VARS)
LOCAL_MODULE := iconv
LOCAL_SRC_FILES := libcharset/lib/localcharset.c lib/iconv.c lib/relocatable.c
LOCAL_C_INCLUDES := include lib libcharset/include
LOCAL_CFLAGS := -Wno-multichar -D_ANDROID -DLIBDIR="c" -DBUILDING_LIBICONV -DIN_LIBRARY
#LOCAL_PRELINK_MODULE := false
include $(BUILD_STATIC_LIBRARY)
编译libiconv的Application.mk文件内容如下:
APP_MODULES := libiconv
APP_MODULES := libiconv
至此, 应用程序编译完毕, 下来在Android平台上慢慢调试吧, 希望不要有问题。