Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

google::protobuf used in android ndk develop

I recently try to move some predict function of Caffe model(c++) to Android platform(ndk level)this is the link: And this model needs support of OpenCV/OpenCV2 and the google::protobuf. I have include all the needed lib(.a) of OpenCV in the Android.mk to compile into .so lib provided for the java like below(in 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)

THen I compile the google::protobuf-2.5.0 with the android-ndk-r11c in ubuntu14.04 to get three static library(libprotobuf.a,libprotobuf-lite.a,libprotoc.a),here's all the script of 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

and the three library used to support the Opencv2/dnn module like this(also in the 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)

but the problem comes when i ndk-build the android.mk to get the 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)

$NDK|ndk-build meet the error of std

the problem seems to lack some part of stl-library ,but in build_library.sh: CXXFLAGS="-march=armv7-a -I$CXXSTL/include -I$CXXSTL/libs/armeabi-v7a/include -L$CXXSTL/libs/armeabi-v7a/ -lgnustl_static"

I have include gnu_stl static and in the same directory of android.mk, the Application.mk is also state the gnustl:

APP_STL := gnustl_static
APP_CPPFLAGS := -frtti -fexceptions
APP_ABI := armeabi-v7a
APP_PLATFORM := android-15

I really hope someone can give me some advice about this situation.

like image 510
Miao Wang Avatar asked May 29 '26 22:05

Miao Wang


1 Answers

Probably you know, but there is a specific android-caffe port, with examples:

library: https://github.com/sh1r0/caffe-android-lib

In caffe/android folder there are JNI wrapper and caffe_mobile files

Example:

https://github.com/sh1r0/caffe-android-demo

It uses android-21 native API level, and it doesn't support some io libraries such as (leveldb, lmdb and hdf5, only cpu).

The library has many scripts to download every 3rparty dependencies (protobuf, gflags, eigem, openblas, ). Eigen3 is defined by default, but it is able to use openblas_library only for arm processors.

I have similar problem, but in my case my stl library problem comes when I'm trying to link my own .so wrapper library using cmake cross-compiling tools. gnustl_static doesn't work and it fails on boost library linking.

directory_iterator_increment(boost::filesystem::directory_iterator&, boost::system::error_code*): error: undefined reference to 'stpcpy'

anyway, I hope this references help you to address the solution.

Cheers

like image 74
uelordi Avatar answered Jun 01 '26 11:06

uelordi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!