7

I'm trying to use opencv on android (ndk only). I compiled the latest source of the git repository for armeabi. (Based on: Building_OpenCV4Android_from_trunk)

But I'm getting this errors (with ndk-build):

error: undefined reference to 'cv::Mat::deallocate()'
error: undefined reference to 'cv::fastFree(void*)'
error: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'
error: undefined reference to 'cv::Mat::copyTo(cv::_OutputArray const&)'
error: undefined reference to 'cv::Mat::inv(int) const'

simple test code:

cv::Mat testMat = cv::Mat(cv::Matx44d
(
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, 0.0,
    0.0, 0.0, 0.0, 1.0
));
cv::Mat testMatInv = testMat.inv();

My Android.mk:

LOCAL_C_INCLUDES :=  $(LOCAL_PATH)/../../../../libs/opencv/include
LOCAL_LDLIBS += -L../../../../libs/opencv/lib/android/armeabi
LOCAL_LDLIBS += -llog -lGLESv2 –lz
LOCAL_STATIC_LIBRARIES := libzip libpng libjpeg freetype
LOCAL_STATIC_LIBRARIES += libopencv_calib3d libopencv_contrib libopencv_core libopencv_features2d libopencv_flann libopencv_highgui libopencv_imgproc libopencv_legacy libopencv_ml libopencv_nonfree libopencv_objdetect libopencv_photo libopencv_stitching libopencv_ts libopencv_video libopencv_videostab

Anyone has any clue? Thanks

Bastl
  • 883
  • 2
  • 10
  • 27

2 Answers2

5

I got it working now. I forgot to add the prebuild libraries in the Android.mk like this:

#same for all other openCV Libs
LOCAL_MODULE := libopencv_calib3d
LOCAL_SRC_FILES := ../../opencv/lib/android/$(TARGET_ARCH_ABI)/libopencv_calib3d.a
include $(PREBUILT_STATIC_LIBRARY)
include $(CLEAR_VARS)

.....
LOCAL_STATIC_LIBRARIES += libopencv_contrib libopencv_legacy libopencv_ml libopencv_stitching libopencv_nonfree libopencv_objdetect libopencv_videostab libopencv_calib3d libopencv_photo libopencv_video libopencv_features2d libopencv_highgui libopencv_androidcamera libopencv_flann libopencv_imgproc libopencv_ts libopencv_core
Bastl
  • 883
  • 2
  • 10
  • 27
3

Order of libraries matters.

Try:

LOCAL_STATIC_LIBRARIES += libopencv_contrib libopencv_legacy libopencv_ml libopencv_stitching libopencv_nonfree libopencv_objdetect libopencv_videostab libopencv_calib3d libopencv_photo libopencv_video libopencv_features2d libopencv_highgui libopencv_androidcamera libopencv_flann libopencv_imgproc libopencv_core

And recommended way is not hardcode all names in your .mk file but use OpenCV.mk from OpenCV SDK to add OpenCV to your project. (If you are making custom build from source, then OpenCV.mk is generated at cmake (and make install) step.)

Andrey Kamaev
  • 29,582
  • 6
  • 94
  • 88
  • 2
    I also recommend reading [Introduction into Android Development](http://docs.opencv.org/trunk/doc/tutorials/introduction/android_binary_package/android_dev_intro.html) – karlphillip Feb 01 '13 at 16:09
  • I reported [an issue](http://code.opencv.org/issues/2726) the other day, do you think it's something on my environment or could it be OpenCV? – karlphillip Feb 01 '13 at 16:10
  • Good link :) Regarding that issue - I can't say for sure, I'm not even a Mac user. You probably can debug QTKit version on your side, I'll put few details to the ticket. – Andrey Kamaev Feb 01 '13 at 16:28
  • Thanks. But this does not work for me. If I only use libopencv_core.a the same error occurs. I do not get errors if I add: `LOCAL_MODULE := libopencv_core LOCAL_SRC_FILES := ../../../../libs/opencv/lib/android/$(TARGET_ARCH_ABI)/libopencv_core.a include $(PREBUILT_STATIC_LIBRARY) include $(CLEAR_VARS)` But the app immediately crashes on: `cv::Mat testMatInv = testMat.inv();` – Bastl Feb 01 '13 at 17:32
  • You say you have compiled from source. Please post a cmake command used. – Andrey Kamaev Feb 01 '13 at 17:41
  • Yes. I used the cmake_android.sh script and I modified it to: `cmake -DANDROID_ABI=armeabi -DCMAKE_BUILD_WITH_INSTALL_RPATH=ON -DCMAKE_TOOLCHAIN_FILE=../android.toolchain.cmake $@ ../..` – Bastl Feb 01 '13 at 18:04
  • I tried the prebuild 2.4.3 android libs… same thing. `It crashes on cv::Mat testMatInv = testMat.inv();` – Bastl Feb 01 '13 at 19:04