我最近尝试将Caffe模型(c ++)的一些预测函数移动到Android平台(ndk级别)this is the link: 此模型需要 OpenCV / OpenCV2 和 google :: protobuf 的支持。 我已经在Android.mk中包含了所有需要的OpenCV lib(.a)来编译成.so lib,如下面的java所提供的(在 Android.mk 中):
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_features2d
LOCAL_SRC_FILES := libopencv_features2d.a
LOCAL_EXPORT_C_INCLUDES := /home/wm/Downloads/OpenCV-android-sdk/sdk/native/jni/include
include $(PREBUILT_STATIC_LIBRARY)
然后我用ubuntu14.04中的android-ndk-r11c编译google :: protobuf-2.5.0以获得三个静态库(libprotobuf.a,libprotobuf-lite.a,libprotoc.a),这里是全部 build_library.sh 的脚本:
#!/bin/bash
export NDK=/home/wm/Downloads/android-ndk-r11c
export SYSROOT=$NDK/platforms/android-15/arch-arm/
export TOOLCHAIN=$NDK/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/
export PATH=$PATH:$TOOLCHAIN/bin
#export CC="arm-linux-androideabi-gcc --sysroot $SYSROOT"
export CXX="arm-linux-androideabi-g++ --sysroot $SYSROOT"
export CXXSTL=$NDK/sources/cxx-stl/gnu-libstdc++/4.9
function build_one
{
mkdir build
./configure --prefix=$(pwd)/build \
--host=arm-linux-androideabi \
--with-sysroot=$SYSROOT \
--enable-static \
--disable-shared \
--enable-cross-compile \
--with-protoc=protoc LIBS="-lc-lstdc++" \
# CFLAGS="-march=armv7-a" \
CXXFLAGS="-march=armv7-a -I$CXXSTL/include -I$CXXSTL/libs/armeabi-v7a/include -L$CXXSTL/libs/armeabi-v7a/ -lgnustl_static"
make clean
make
make install
}
CPU=arm
PREFIX=$(pwd)/android/$CPU
ADDI_CFLAGS="-marm"
build_one
# Inspect the library architecture specific information
# arm-linux-androideabi-readelf -A build/lib/libprotobuf-lite.a
以及用于支持Opencv2 / dnn模块的三个库(也在 Android.mk 中):
include $(CLEAR_VARS)
LOCAL_MODULE := google_protobuf
LOCAL_SRC_FILES := libprotobuf.a
LOCAL_EXPORT_C_INCLUDES := $(call my-dir)/sources/protobuf-2.5.0/build/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := google_protobuflite
LOCAL_SRC_FILES := libprotobuf-lite.a
LOCAL_EXPORT_C_INCLUDES := $(call my-dir)/sources/protobuf-2.5.0/build/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := google_protoc
LOCAL_SRC_FILES := libprotoc.a
LOCAL_EXPORT_C_INCLUDES := $(call my-dir)/sources/protobuf-2.5.0/build/include
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)
LOCAL_MODULE := opencv_dnn
LOCAL_SRC_FILES := libopencv_dnn.a
LOCAL_STATIC_LIBRARIES := google_protobuf google_protoc google_protobuflite
LOCAL_EXPORT_C_INCLUDES := /home/wm/Downloads/OpenCV-android-sdk/sdk/native/jni/include
include $(PREBUILT_STATIC_LIBRARY)
但问题来自于我建立 android.mk 来获取MyLib.so:
LOCAL_MODULE := MyLib
LOCAL_C_INCLUDES :=/home/wm/Downloads/OpenCV-android-sdk/sdk/native/jni/include/opencv
LOCAL_C_INCLUDES +=/home/wm/Downloads/OpenCV-android-sdk/sdk/native/jni/include
LOCAL_SRC_FILES := com_example_alexandroskarargyris_myapplication_NativeClass.cpp
LOCAL_STATIC_LIBRARIES := opencv_dnn opencv_xphoto opencv_xobjdetect opencv_ximgproc opencv_xfeatures2d opencv_videostab opencv_videoio opencv_video opencv_ts opencv_line_descriptor
LOCAL_STATIC_LIBRARIES += opencv_tracking opencv_text opencv_surface_matching opencv_superres opencv_structured_light opencv_aruco opencv_bgsegm opencv_bioinspired opencv_imgproc
LOCAL_STATIC_LIBRARIES += opencv_calib3d opencv_ccalib opencv_core opencv_datasets opencv_dpm opencv_face opencv_features2d opencv_flann opencv_hal opencv_highgui opencv_imgcodecs opencv_ml
LOCAL_STATIC_LIBRARIES += opencv_objdetect opencv_optflow opencv_photo opencv_reg opencv_rgbd opencv_saliency opencv_shape opencv_stereo opencv_stitching
LOCAL_STATIC_LIBRARIES += libjpeg libwebp libpng libtiff libjasper IlmImf tbb
LOCAL_LDLIBS += -llog -lz -ldl -lm **-lstdc++**
include $(BUILD_SHARED_LIBRARY)
问题似乎缺少stl-library的某些部分,但是在build_library.sh中: CXXFLAGS =“ - march = armv7-a -I $ CXXSTL / include -I $ CXXSTL / libs / armeabi-v7a / include -L $ CXXSTL / libs / armeabi-v7a / -lgnustl_static”
我已经包含gnu_stl static并且在android.mk的同一目录中, Application.mk 也说明了gnustl:
APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15
我真的希望有人可以就这种情况给我一些建议。
答案 0 :(得分:6)
可能你知道,但是有一个特定的android-caffe端口,例如:
库: https://github.com/sh1r0/caffe-android-lib
在caffe / android文件夹中有JNI包装器和caffe_mobile文件
示例:
https://github.com/sh1r0/caffe-android-demo
它使用android-21本机API级别,并且它不支持某些io库,例如(leveldb,lmdb和hdf5,只有cpu)。
该库有许多脚本可以下载每个3rparty依赖项(protobuf,gflags,eigem,openblas)。 Eigen3是默认定义的,但它只能用于arm处理器的openblas_library。
我有类似的问题,但在我的情况下,当我尝试使用cmake交叉编译工具链接我自己的.so包装库时,我的stl库问题出现了。 gnustl_static没有工作,它在boost库链接上失败了。
directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*): error: undefined reference to 'stpcpy'
无论如何,我希望这些参考资料可以帮助您解决问题。
干杯