我正在尝试在Android Studio项目中使用OpenCV本地库。我会因功能knnmatch的未定义参考而出现错误。
我在项目中添加了OpenCvlibrary,并成功地使用了其他OpenCV功能。
- 是否有特定的原因为什么要使用libopencv_features2d?
- 正确编译和链接功能的正确方法是什么?
更新:添加我的编译文件
我的应用程序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
}
我的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 )
在我的 jniLibs -> arm64-v8a
中,我有那些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
正如我所说的其他CV功能,例如cv::solvePnP
工作正常。只有KnnMatch
投掷未划分。
更新:如果我删除-DANDROID_STL=c++_shared
,我可以编译knnMatch
,但我需要它,任何建议?
嗨,这显然是一个链接错误。在编译代码时,您从外部库中引用的方法无法包括或链接。在编译过程中,链接器在所有库中查找有关该方法的参考,如果在库或OBJ文件中找不到该方法定义,它会给您带来此错误。如果您使用的是make文件检查是否正确添加了其中所需的库。还要检查是否已将库设置为路径环境变量。