【问题标题】:OpenCV Android Linking CXX shared FAILEDOpenCV Android Linking CXX shared FAILED
【发布时间】:2021-01-26 10:04:40
【问题描述】:

我正在尝试使用 Android 4.1.1 配置 OpenCV 4.5.1 以进行一些图像处理,但遇到了以下错误。上网查了2天,没找到解决办法。

构建命令失败。 使用参数执行过程 C:\Users\User_folder\AppData\Local\Android\Sdk\cmake\3.10.2.4988404\bin\ninja.exe 时出错 {-C C:\Users\User_folder\opencv6test\sdk.cxx\cmake\debug \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,--exclude-libs,libgcc_real.a -Wl,--exclude-libs,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:构建停止:子命令失败。

我的理解是,根本问题是 Linking CXX shared library FAILED 并且这是由于 NDK 版本和 OpenCV 版本之间的不兼容。但我不知道如何为我的 native-lib.cpp 解决这个问题。我尝试在 OpenCV gradle 中使用 gnustatic,但收到一条错误消息,说它不再受支持。

任何帮助将不胜感激。

我的 CMAKELISTS:

    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 已安装

enter image description here

【问题讨论】:

    标签: android-ndk opencv4android


    【解决方案1】:

    您似乎错过了链接导入的库:

    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
            )
    

    【讨论】:

    • 伙伴!它就像一个魅力!非常感谢。我已经连续 2 天在这个问题上苦苦挣扎,而你指定的那一条线让它工作了。我没有足够的代表来接受它作为正确答案。也许Mods可以提供帮助。对于其他任何人来说,这是正确的答案,它正在发挥作用。
    猜你喜欢
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多