error: undefined reference to ‘cv::Mat::~Mat()‘解决AndroidStudio集成OpenVC出现的编译报错问题

本文主要是介绍error: undefined reference to ‘cv::Mat::~Mat()‘解决AndroidStudio集成OpenVC出现的编译报错问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

前言

本来这种类型的博客不是笔者想写的,不过这个问题,笔者经过网上一番搜索却没有可用的解决方案,因此分享出来帮助大家填坑。

集成OpenVC静态库

OpenCV官方的Android SDK在这里下载,集成方法就不多介绍了,可以看这两位博主的文章:

  •  小小情意的​ Android 接入 OpenCV库的三种方式 ​
  •  春末的南方城市的在Android端集成OpenCV的三种方式

CMakeList.txt完整配置文件如下:

# For more information about using CMake with Android Studio, read the
# documentation: https://d.android.com/studio/projects/add-native-code.html# Sets the minimum version of CMake required to build the native library.cmake_minimum_required(VERSION 3.18.1)# Declares and names the project.project("opencv_mobile")include_directories(include)# 设置Debug编译模式,这样才能生成符号表,方便使用addr2line来定位内存地址对应代码行
set(CMAKE_BUILD_TYPE DEBUG)# 设置变量
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -frtti -fexceptions -fopenmp -static-openmp")
set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv/sdk/native/jni)
find_package(OpenCV 4.5.4 REQUIRED core features2d highgui imgproc photo video)
message("OpenCV_FOUND is ${OpenCV_FOUND}")
message("OpenCV_LIBS is ${OpenCV_LIBS}")# Creates and names a library, sets it as either STATIC
# or SHARED, and provides the relative paths to its source code.
# You can define multiple libraries, and CMake builds them for you.
# Gradle automatically packages shared libraries with your APK.add_library( # Sets the name of the library.opencv_mobile# Sets the library as a shared library.SHARED# Provides a relative path to your source file(s).opencv_mobile.cpp)# Searches for a specified prebuilt library and stores the path as a
# variable. Because CMake includes system libraries in the search path by
# default, you only need to specify the name of the public NDK library
# you want to add. CMake verifies that the library exists before
# completing its build.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.opencv_mobile${OpenCV_LIBS}# Links the target library to the log library# included in the NDK.android${log-lib})

官方提供的OpenCVConfig.cmake也告诉了我们怎么集成。

# ===================================================================================
#  The OpenCV CMake configuration file
#
#             ** File generated automatically, do not modify **
#
#  Usage from an external project:
#    In your CMakeLists.txt, add these lines:
#
#    find_package(OpenCV REQUIRED)
#    include_directories(${OpenCV_INCLUDE_DIRS}) # Not needed for CMake >= 2.8.11
#    target_link_libraries(MY_TARGET_NAME ${OpenCV_LIBS})
#
#    Or you can search for specific OpenCV modules:
#
#    find_package(OpenCV REQUIRED core videoio)
#
#    If the module is found then OPENCV_<MODULE>_FOUND is set to TRUE.
#
#    This file will define the following variables:
#      - OpenCV_LIBS                     : The list of all imported targets for OpenCV modules.
#      - OpenCV_INCLUDE_DIRS             : The OpenCV include directories.
#      - OpenCV_ANDROID_NATIVE_API_LEVEL : Minimum required level of Android API.
#      - OpenCV_VERSION                  : The version of this OpenCV build: "4.5.4"
#      - OpenCV_VERSION_MAJOR            : Major version part of OpenCV_VERSION: "4"
#      - OpenCV_VERSION_MINOR            : Minor version part of OpenCV_VERSION: "5"
#      - OpenCV_VERSION_PATCH            : Patch version part of OpenCV_VERSION: "4"
#      - OpenCV_VERSION_STATUS           : Development status of this build: ""
#
# ===================================================================================

代码结构如下:

 源码在这里查看:https://github.com/xiangang/AndroidDevelopmentPractices/tree/develop

编译遇到的问题

集成OpenCV虽然很简单,但有时候也需要一点点运气。像笔者运气就不太好,下面是编译报错的完整日志。

FAILURE: Build completed with 2 failures.1: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':OpenCVMobile:buildCMakeDebug[armeabi-v7a]'.
> Build command failed.Error while executing process /home/xiangang/Android/Sdk/cmake/3.18.1/bin/ninja with arguments {-C /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/.cxx/Debug/4r2c6mo1/armeabi-v7a opencv_mobile}ninja: Entering directory `/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/.cxx/Debug/4r2c6mo1/armeabi-v7a'[1/1] Linking CXX shared library /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/build/intermediates/cxx/Debug/4r2c6mo1/obj/armeabi-v7a/libopencv_mobile.soFAILED: /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/build/intermediates/cxx/Debug/4r2c6mo1/obj/armeabi-v7a/libopencv_mobile.so : && /work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi23 --gcc-toolchain=/work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -frtti -fexceptions -fopenmp -static-openmp -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libopencv_mobile.so -o /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/build/intermediates/cxx/Debug/4r2c6mo1/obj/armeabi-v7a/libopencv_mobile.so CMakeFiles/opencv_mobile.dir/opencv_mobile.cpp.o  -landroid  /work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/23/liblog.so  -latomic -lm && :/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:23: error: undefined reference to 'cv::Mat::Mat(int, int, int, void*, unsigned int)'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:24: error: undefined reference to 'cv::Mat::Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:25: error: undefined reference to 'cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:53: error: undefined reference to 'cv::Mat::release()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:54: error: undefined reference to 'cv::Mat::release()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'clang++: error: linker command failed with exit code 1 (use -v to see invocation)ninja: build stopped: subcommand failed.* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================2: Task failed with an exception.
-----------
* What went wrong:
Execution failed for task ':OpenCVMobile:buildCMakeRelWithDebInfo[armeabi-v7a]'.
> Build command failed.Error while executing process /home/xiangang/Android/Sdk/cmake/3.18.1/bin/ninja with arguments {-C /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/.cxx/RelWithDebInfo/442v101w/armeabi-v7a opencv_mobile}ninja: Entering directory `/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/.cxx/RelWithDebInfo/442v101w/armeabi-v7a'[1/1] Linking CXX shared library /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/build/intermediates/cxx/RelWithDebInfo/442v101w/obj/armeabi-v7a/libopencv_mobile.soFAILED: /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/build/intermediates/cxx/RelWithDebInfo/442v101w/obj/armeabi-v7a/libopencv_mobile.so : && /work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/bin/clang++ --target=armv7-none-linux-androideabi23 --gcc-toolchain=/work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64 --sysroot=/work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot -fPIC -g -DANDROID -fdata-sections -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -D_FORTIFY_SOURCE=2 -march=armv7-a -mthumb -Wformat -Werror=format-security   -frtti -fexceptions -fopenmp -static-openmp -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,libatomic.a -static-libstdc++ -Wl,--build-id -Wl,--fatal-warnings -Wl,--exclude-libs,libunwind.a -Wl,--no-undefined -Qunused-arguments -shared -Wl,-soname,libopencv_mobile.so -o /work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/build/intermediates/cxx/RelWithDebInfo/442v101w/obj/armeabi-v7a/libopencv_mobile.so CMakeFiles/opencv_mobile.dir/opencv_mobile.cpp.o  -landroid  /work/android/android-ndk-r21e/toolchains/llvm/prebuilt/linux-x86_64/sysroot/usr/lib/arm-linux-androideabi/23/liblog.so  -latomic -lm && :/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:23: error: undefined reference to 'cv::Mat::Mat(int, int, int, void*, unsigned int)'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:24: error: undefined reference to 'cv::Mat::Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:25: error: undefined reference to 'cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:53: error: undefined reference to 'cv::Mat::release()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:54: error: undefined reference to 'cv::Mat::release()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'/work/AndroidStudioProjects/AndroidDevelopmentPractices/AndroidJetpackComposeSample/OpenCVMobile/src/main/cpp/opencv_mobile.cpp:55: error: undefined reference to 'cv::Mat::~Mat()'clang++: error: linker command failed with exit code 1 (use -v to see invocation)ninja: build stopped: subcommand failed.* Try:
> Run with --stacktrace option to get the stack trace.
> Run with --info or --debug option to get more log output.
> Run with --scan to get full insights.
==============================================================================* Get more help at https://help.gradle.orgBUILD FAILED in 2s
72 actionable tasks: 4 executed, 68 up-to-date

对应的源码如下:

#include <jni.h>
#include <string>
#include "common.h"
#include <android/bitmap.h>
// for native window JNI
#include <android/native_window_jni.h>
#include <android/native_window.h>
// for native OpenCV JNI
#include "opencv2/core/core.hpp"
#include "opencv2/core/mat.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"using namespace cv;extern "C"
JNIEXPORT void JNICALL
Java_com_nxg_opencv_OpenCVMobile_renderYuvDataOnSurface(JNIEnv *env, jclass clazz, jint width,jint height, jbyteArray yuv_data,jobject surface) {// yuv转rgbajbyte *data = env->GetByteArrayElements(yuv_data, nullptr);cv::Mat yuvImg(height + height / 2, width, CV_8UC1, data);cv::Mat rgbImg;cv::cvtColor(yuvImg, rgbImg, COLOR_YUV2RGBA_IYUV);// 这两行代码用于翻转的,用不到,这里注释掉//cv::transpose(yuvImg, rgbImg);//cv::flip(yuvImg, rgbImg, 1);//获取ANativeWindowANativeWindow *window = ANativeWindow_fromSurface(env, surface);ANativeWindow_acquire(window);//设置ANativeWindow相关参数,包括宽/高/像素格式ANativeWindow_setBuffersGeometry(window, rgbImg.cols, rgbImg.rows, WINDOW_FORMAT_RGBA_8888);ANativeWindow_Buffer outBuffer;// 调用ANativeWindow_lock锁定后台的缓冲部分,并获取surface缓冲区的地址if (int32_t err = ANativeWindow_lock(window, &outBuffer, nullptr)) {LOGE("ANativeWindow_lock failed with error code: %d\n", err);ANativeWindow_release(window);}// 拷贝rgb数据到缓冲区auto *outPtr = reinterpret_cast<uint8_t *>(outBuffer.bits);int dst_line_size = outBuffer.stride * 4;//一行一行拷贝for (int i = 0; i < outBuffer.height; ++i) {//从源src所指的内存地址的起始位置开始拷贝n个字节到目标dest所指的内存地址的起始位置中memcpy(outPtr + i * dst_line_size, rgbImg.data + i * rgbImg.cols * 4, dst_line_size);}//绘制ANativeWindow_unlockAndPost(window);//用完释放ANativeWindow_release(window);env->ReleaseByteArrayElements(yuv_data, data, 0);yuvImg.release();rgbImg.release();
}

简简单单的代码,怎么就出问题了?检查了头文件,没错。源码,也没错。那就只有一个可能,CMake编译出问题。回过头看集成OpenCV的关键CMake代码:

set(OpenCV_DIR ${CMAKE_SOURCE_DIR}/opencv/sdk/native/jni)
find_package(OpenCV 4.5.4 REQUIRED core features2d highgui imgproc photo video)
message("OpenCV_DIR is ${OpenCV_DIR}")
message("OpenCV_FOUND is ${OpenCV_FOUND}")
message("OpenCV_LIBS is ${OpenCV_LIBS}")
target_link_libraries( # Specifies the target library.opencv_mobile${OpenCV_LIBS}# Links the target library to the log library# included in the NDK.android${log-lib})

加了关键变量的打印。最后发现是OpenCV_LIBS变量为空,难怪有问题。

那就看下OpenCVConfig.cmake是在哪里给OpenCV_LIBS赋值的,继续在关键地方加打印如下:

 发现压根没执行到这里。继续往上排查到。最终发现在下图这个红色框部分提前return了。原因是因为编译用的Android版本低于OpenCV_ANDROID_NATIVE_API_LEVEL定义的版本,至此,真相大白!

既然知道原因了,那就好办了。

解决办法

修改minSdkVersion

如果项目没有限制,则直接把对应module的build.gralde的minSdkVersion改为OpenCV_ANDROID_NATIVE_API_LEVEL一样的版本,并重新编译即可。

android {compileSdk = BuildConfig.compileSdkdefaultConfig {minSdk = 24targetSdk = BuildConfig.targetSdkVersiontestInstrumentationRunner = BuildConfig.testInstrumentationRunnerconsumerProguardFiles("consumer-rules.pro")}
}

修改OpenCV_ANDROID_NATIVE_API_LEVEL

你可能会想,我直接改OpenCV_ANDROID_NATIVE_API_LEVEL行不行,比如我项目里是minSdkVersion=23,OpenCV_ANDROID_NATIVE_API_LEVEL的值是24,我直接把OpenCV_ANDROID_NATIVE_API_LEVEL改成23行不行?

笔者实测发现是可以的,但是否有其它未知的问题那就不得而知了。这个方式不是很建议,要相信OpenVC这么要求是有它的道理的。

这篇关于error: undefined reference to ‘cv::Mat::~Mat()‘解决AndroidStudio集成OpenVC出现的编译报错问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/195666

相关文章

SQL Server配置管理器无法打开的四种解决方法

《SQLServer配置管理器无法打开的四种解决方法》本文总结了SQLServer配置管理器无法打开的四种解决方法,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录方法一:桌面图标进入方法二:运行窗口进入检查版本号对照表php方法三:查找文件路径方法四:检查 S

怎样通过分析GC日志来定位Java进程的内存问题

《怎样通过分析GC日志来定位Java进程的内存问题》:本文主要介绍怎样通过分析GC日志来定位Java进程的内存问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、GC 日志基础配置1. 启用详细 GC 日志2. 不同收集器的日志格式二、关键指标与分析维度1.

Java 线程安全与 volatile与单例模式问题及解决方案

《Java线程安全与volatile与单例模式问题及解决方案》文章主要讲解线程安全问题的五个成因(调度随机、变量修改、非原子操作、内存可见性、指令重排序)及解决方案,强调使用volatile关键字... 目录什么是线程安全线程安全问题的产生与解决方案线程的调度是随机的多个线程对同一个变量进行修改线程的修改操

Redis出现中文乱码的问题及解决

《Redis出现中文乱码的问题及解决》:本文主要介绍Redis出现中文乱码的问题及解决,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1. 问题的产生2China编程. 问题的解决redihttp://www.chinasem.cns数据进制问题的解决中文乱码问题解决总结

Visual Studio 2022 编译C++20代码的图文步骤

《VisualStudio2022编译C++20代码的图文步骤》在VisualStudio中启用C++20import功能,需设置语言标准为ISOC++20,开启扫描源查找模块依赖及实验性标... 默认创建Visual Studio桌面控制台项目代码包含C++20的import方法。右键项目的属性:

在Spring Boot中集成RabbitMQ的实战记录

《在SpringBoot中集成RabbitMQ的实战记录》本文介绍SpringBoot集成RabbitMQ的步骤,涵盖配置连接、消息发送与接收,并对比两种定义Exchange与队列的方式:手动声明(... 目录前言准备工作1. 安装 RabbitMQ2. 消息发送者(Producer)配置1. 创建 Spr

全面解析MySQL索引长度限制问题与解决方案

《全面解析MySQL索引长度限制问题与解决方案》MySQL对索引长度设限是为了保持高效的数据检索性能,这个限制不是MySQL的缺陷,而是数据库设计中的权衡结果,下面我们就来看看如何解决这一问题吧... 目录引言:为什么会有索引键长度问题?一、问题根源深度解析mysql索引长度限制原理实际场景示例二、五大解决

Springboot如何正确使用AOP问题

《Springboot如何正确使用AOP问题》:本文主要介绍Springboot如何正确使用AOP问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录​一、AOP概念二、切点表达式​execution表达式案例三、AOP通知四、springboot中使用AOP导出

如何在Spring Boot项目中集成MQTT协议

《如何在SpringBoot项目中集成MQTT协议》本文介绍在SpringBoot中集成MQTT的步骤,包括安装Broker、添加EclipsePaho依赖、配置连接参数、实现消息发布订阅、测试接口... 目录1. 准备工作2. 引入依赖3. 配置MQTT连接4. 创建MQTT配置类5. 实现消息发布与订阅

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到