undefined reference to `cv::DescriptorMatcher::knnMatch
Asked Answered
Y

2

6

I'm trying to use opencv native library in an android studio project. I get an error for undefined reference for the function knnMatch.

I have added openCVLibrary to my project and had success to use other openCV features.

  1. Is there a specific why to use libopencv_features2d?
  2. What is to proper way to compile and link features2d?

UPDATE: Add my compiling files

My App build.gradle :

apply plugin: 'com.android.application'
//
android {
    compileSdkVersion 27
    buildToolsVersion '27.0.3'
//
    defaultConfig {
        minSdkVersion 21
        targetSdkVersion 27
        ...
        externalNativeBuild {
            cmake {
                arguments "-DANDROID_ARM_NEON=TRUE", "-DANDROID_TOOLCHAIN=clang" , "-DANDROID_STL=c++_static","-DCMAKE_BUILD_TYPE=Release", "-DANDROID_CPP_FEATURES=rtti exceptions"
                cppFlags    "-D__STDC_FORMAT_MACROS" , '-O3','-fopenmp','-fsigned-char', "-std=c++14", "-frtti", "-fexceptions", "-mfloat-abi=softfp", "-Wall"
            }
            ndk {
                abiFilters 'arm64-v8a'
            }
        }
    }
    //
    externalNativeBuild {
        cmake {
            path "CMakeLists.txt"
        }
    }
    ...
}
//
dependencies {
        compile fileTree(include: ['*.jar'], dir: 'libs')
        implementation project(':openCVLibrary340') //Module
}

My CMakeList.txt :

cmake_minimum_required(VERSION 3.4.1)
#
include_directories(/.../OpenCV-android-sdk/sdk/native/jni/include)
add_library( libopencv SHARED IMPORTED )
set_target_properties(libopencv PROPERTIES IMPORTED_LOCATION ${CMAKE_CURRENT_SOURCE_DIR}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)
#
FILE(GLOB CPP_SRC
"src/main/cpp/*.c"
"src/main/cpp/*.h"
"src/main/cpp/*.cpp"
"src/main/cpp/*.hpp"
)
#
add_library( myLib SHARED ${CPP_SRC} )
#
find_library( log-lib log )
#
target_link_libraries( myLib libopencv ${log-lib} )
target_link_libraries( myLib android log EGL GLESv2 )

In my jniLibs -> arm64-v8a I have those cv libs:

libopencv_calib3d.a, libopencv_core.a, libopencv_dnn.a, libopencv_features2d.a libopencv_flann.a, libopencv_highgui.a, libopencv_imgcodecs.a, libopencv_imgproc.a, libopencv_java3.so, libopencv_ml.a, libopencv_objdetect.a, libopencv_photo.a, libopencv_shape.a libopencv_stitching.a, libopencv_superres.a, libopencv_video.a, libopencv_videoio.a, libopencv_videostab.a

As I said other cv function like cv::solvePnP work fine. only KnnMatch throw undifined.

UPDATE: If I'm remove -DANDROID_STL=c++_shared I can compile knnMatch but I need it, any suggestions?

Yann answered 19/2, 2018 at 9:21 Comment(1)
Are you getting this error during compilation and linking? How are you compiling your application using make file?Capparidaceous
C
1

Hi It is clearly a linking error. While compiling your code, method you are referring from external library could not be included or linked. During compilation a linker look for reference to that method in all libraries and if it could not find that method definition in the libraries or obj files it gives you this error. If you are using a make file check whether you have properly added the required library in it. Also check whether library has been set to the path environment variable or not.

Capparidaceous answered 19/2, 2018 at 11:31 Comment(0)
R
0

For OpenCV 3.4.10 using older ndk version fixed this error for me.

ndkVersion "16.1.4479499" //in build.gradle
Rumpus answered 24/9 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.