问题描述
我正在尝试使用 Android 4.1.1 配置 OpenCV 4.5.1 以进行一些图像处理,但遇到了以下错误。我已经通过互联网搜索了 2 天,但找不到解决方案。
构建命令失败。
使用参数 {-CC:\Users\User_folder\opencv6test\sdk.cxx\cmake\debug 执行进程 C:\Users\User_folder\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe 时出错\arm64-v8a 原生库 opencv_jni_shared}
忍者:进入目录 C:\Users\User_folder\opencv6test\sdk\.cxx\cmake\debug\arm64-v8a' [1/1] Linking CXX shared library C:\Users\User_folder\opencv6test\sdk\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so Failed: C:/Users/User_folder/opencv6test/sdk/build/intermediates/cmake/debug/obj/arm64-v8a/libnative-lib.so cmd.exe /C "cd . && C:\Users\User_folder\AppData\Local\Android\Sdk\ndk\21.1.6352462\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe --target=aarch64-none-linux-android21 --gcc-toolchain=C:/Users/User_folder/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64 --sysroot=C:/Users/User_folder/AppData/Local/Android/Sdk/ndk/21.1.6352462/toolchains/llvm/prebuilt/windows-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -Wformat -Werror=format-security -std=gnu++11 -O0 -fno-limit-debug-info -Wl,--exclude-libs,libgcc.a -Wl,libgcc_real.a -Wl,libatomic.a -Wl,--build-id -Wl,--fatal-warnings -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libnative-lib.so -o C:\Users\User_folder\opencv6test\sdk\build\intermediates\cmake\debug\obj\arm64-v8a\libnative-lib.so CMakeFiles/native-lib.dir/native-lib.cpp.o -llog -latomic -lm && cd ." CMakeFiles/native-lib.dir/native-lib.cpp.o: In function
Java_com_example_opencv6test_MainActivity_testFunction':
C:/Users/User_folder/opencv6test/sdk/libcxx_helper/native-lib.cpp:12: 未定义对`cv::cvtColor(cv::_InputArray const&,cv::_OutputArray const&,int,int)'的引用
clang++:错误:链接器命令失败,退出代码为 1(使用 -v 查看调用)
ninja:构建停止:子命令失败。
我的理解是底层问题是链接 CXX 共享库失败,这是由于 NDK 版本和 OpenCV 版本之间的不兼容。但我不知道如何为我的 native-lib.cpp 解决这个问题。我试图在 OpenCV gradle 中使用 gnustatic,但收到一个错误,说它不再受支持。
任何帮助将不胜感激。
我的 CMAKELIST:
cmake_minimum_required(VERSION 3.6)
project(opencv6test CXX)
add_library(opencv_jni_shared STATIC dummy.cpp)
set(OpenCV_DIR C:/Users/User_folder/OpenCV-android-sdk/sdk/native/jni)
set(CMAKE_VERBOSE_MAKEFILE on)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
include_directories(${OpenCV_DIR}/include)
add_library( # Sets the name of the library.
native-lib
# Sets the library as a shared library.
SHARED
# Provides a relative path to your source file(s).
native-lib.cpp)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION C:/Users/User_folder/opencv6test/sdk/native/libs/${ANDROID_ABI}/libopencv_java4.so)
find_library( # Sets the name of the path variable.
log-lib
# Specifies the name of the NDK library that
# you want CMake to locate.
log )
# Specifies libraries CMake should link to your target library. You
# can link multiple libraries,such as libraries you define in this
# build script,prebuilt third-party libraries,or system libraries.
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib})
native-lib.cpp:
#include "opencv2/core.hpp"
#include <jni.h>
#include <string>
#include <opencv2/imgproc.hpp>
using namespace cv;
using namespace std;
extern "C" JNIEXPORT void JNICALL
Java_com_example_opencv6test_MainActivity_testFunction(jnienv *env,jclass type,jlong addrRGBA){
Mat &img = *(Mat *) addrRGBA; //convert long to a MAT
cvtColor(img,img,COLOR_RGB2GRAY);
}
打开简历gradle:
android {
compileSdkVersion 30
defaultConfig {
minSdkVersion 21
targetSdkVersion 30
versionCode openCVersionCode
versionName openCVersionName
externalNativeBuild {
cmake {
arguments "-DANDROID_STL=c++_shared"
targets "opencv_jni_shared","native-lib"
}
}
}
我的 NDK:
已安装 21.1.6352462
解决方法
您似乎错过了链接导入库的操作:
target_link_libraries( # Specifies the target library.
native-lib
# Links the target library to the log library
# included in the NDK.
${log-lib}
lib_opencv # missed
)