使用OpenCV 3.4.0和NDK在Android上链接错误 [英] Linking errors on Android with OpenCV 3.4.0 and NDK

查看:120
本文介绍了使用OpenCV 3.4.0和NDK在Android上链接错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将使用OpenCV用C ++编写的现有计算机视觉代码移植到Android NDK.通过遵循此处提供的信息,我成功导入了适用于Java和NDK的OpenCV库版本3.4.0(使用官方的预构建Android软件包):

I am trying to port existing computer vision code, written in C++ using OpenCV, to Android NDK. I successfully imported the OpenCV library version 3.4.0 (using the official pre-built Android package) for both Java and NDK by following the information provided here: Satck Overflow Answer - CMake configuration of OpenCV on Android.

我能够使用Java和C ++中的OpenCV功能编译并运行一些代码.但是,我遇到了2个与某些OpenCV函数有关的未定义引用"链接错误:持久性JSON阅读器和功能2D描述符匹配器.

I am able to compile and run some code with OpenCV functionalities in Java and in C++. However, I am stuck with 2 "undefined reference" linking errors related to some OpenCV functions: persistence JSON reader and feature 2D descriptor matcher.

这是我收到的错误消息:

Here are the error messages I get:

Build command failed.
Error while executing process D:\Librairies\Android_SDK\cmake\3.6.4111459\bin\cmake.exe with arguments {--build D:\Dev\Android\PageDetector\app\.externalNativeBuild\cmake\debug\x86_64 --target page-recognition-lib}
[1/1] Linking CXX shared library ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\recognition-lib.so
FAILED: cmd.exe /C "cd . && D:\Librairies\Android_SDK\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=x86_64-none-linux-android --gcc-toolchain=D:/Librairies/Android_SDK/ndk-bundle/toolchains/x86_64-4.9/prebuilt/windows-x86_64 --sysroot=D:/Librairies/Android_SDK/ndk-bundle/sysroot -fPIC -isystem D:/Librairies/Android_SDK/ndk-bundle/sysroot/usr/include/x86_64-linux-android -D__ANDROID_API__=21 -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -std=c++11 -std=c++11 -frtti -fexceptions -std=gnu++11 -O0 -fno-limit-debug-info  -Wl,--exclude-libs,libgcc.a -Wl,--exclude-libs,libatomic.a -nostdlib++ --sysroot D:/Librairies/Android_SDK/ndk-bundle/platforms/android-21/arch-x86_64 -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -LD:/Librairies/Android_SDK/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86_64 -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libpage-recognition-lib.so -o ..\..\..\..\build\intermediates\cmake\debug\obj\x86_64\recognition-lib.so.so [...] -llog -llog ../../../../src/main/jniLibs/x86_64/libopencv_java3.so -latomic -lm "D:/Librairies/Android_SDK/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86_64/libc++_static.a" "D:/Librairies/Android_SDK/ndk-bundle/sources/cxx-stl/llvm-libc++/libs/x86_64/libc++abi.a" && cd ."
D:/Librairies/OpenCV-android-sdk_340/sdk/native/jni/include\opencv2/core/persistence.hpp:1264: error: undefined reference to 'cv::read(cv::FileNode const&, std::__ndk1::vector<cv::KeyPoint, std::__ndk1::allocator<cv::KeyPoint> >&)'
D:\Dev\Android\PageDetector\app\src\main\cpp/PageMatcher.cpp:170: error: undefined reference to 'cv::DescriptorMatcher::radiusMatch(cv::_InputArray const&, cv::_InputArray const&, std::__ndk1::vector<std::__ndk1::vector<cv::DMatch, std::__ndk1::allocator<cv::DMatch> >, std::__ndk1::allocator<std::__ndk1::vector<cv::DMatch, std::__ndk1::allocator<cv::DMatch> > > >&, float, cv::_InputArray const&, bool) const'
clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed.

以下是编译器无法链接的代码段:

Below are the pieces of code that the compiler fails to link:

//code that reads data from a JSON file
this->JSONFeatureFilePath = JSONFeatureFilePath;
cv::FileStorage reader(JSONFeatureFilePath, cv::FileStorage::READ);

this->bookTitle = (string) reader["book_title"];
this->pageNumber = (int) reader["page_num"];

string descType = (string)reader["desc_type"];
replace(descType.begin(), descType.end(), '_', '.');
this->descriptorType = descType;

reader["img_size"] >> this->imageSize;

//this instruction causes the linker error
reader["keypoints"] >> this->keyPoints;

reader["descriptors"] >> this->keyPointDescriptors;
reader["fsum2d"] >> this->fsum2DFeatureSummary;

reader.release();

//code performing key point descriptors matching
cv::Ptr<cv::DescriptorMatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::BRUTEFORCE_HAMMING);

vector<vector<cv::DMatch>> matchesTmp;

//instruction responsible for the link error
matcher->radiusMatch(this->sortedMatches.at(refSortedIndex).refImage->getDescriptors(),
    this->testImage->getDescriptors(), matchesTmp, matchThreshold);

我已清楚地识别出导致链接器错误的行,如上面的代码示例中所述.如果我将其注释掉,则编译会顺利进行,并且程序可以正常运行(当然,我没有尝试在NDK中实现的功能).

I have clearly identified the lines that cause the linker errors, as commented in the code samples above. If I comment them out, the compilation goes through and the program runs fine (of course without the functionalities I am trying to implement in the NDK).

我的猜测是,我调用的OpenCV函数在预构建的库中丢失,或者与我用于NDK开发的编译器不兼容.我已经尝试过更改OpenCV版本(3.3.0和3.4.0).

My guess is that the OpenCV functions I call are missing in the pre-built library or are incompatible with the compiler I am using for NDK development. I have already tried changing OpenCV version (3.3.0 and 3.4.0).

有人知道导致此问题的原因以及我该如何解决吗?是OpenCV中的已知错误,还是我的配置不被支持或者是错误的?

我正在使用Windows 10计算机以及Android Studio 3.1.2,NDK r17,构建工具27.0.3和OpenCV 3.4.0预先构建的android程序包(我自己没有从源代码进行编译).以下是CMake和build.gradle文件:

I am using a Windows 10 computer with Android Studio 3.1.2, NDK r17, build tools 27.0.3 and OpenCV 3.4.0 pre-built android package (I did not compile it from source myself). Below are the CMake and build.gradle files:

CMake:

cmake_minimum_required(VERSION 3.4.1)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")
set(opencv_340_dir D:/Librairies/OpenCV-android-sdk_340/sdk/native/jni)
set(app_dir D:/Dev/Android/PageDetector/app)

# native recognition library API
add_library(recognition-lib
        SHARED
        src/main/cpp/recognition-lib.h
        src/main/cpp/recognition-lib.cpp
        # + my classes' h and cpp files
        )

# OpenCV lib linking and includes
include_directories(${opencv_340_dir}/include)
add_library(opencv-lib SHARED IMPORTED)
set_target_properties(opencv-lib PROPERTIES IMPORTED_LOCATION ${app_dir}/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)


find_library(log-lib log)

target_link_libraries(
                   recognition-lib
                   opencv-lib
                   ${log-lib}
                   )

target_link_libraries(recognition-lib ${log-lib})

等级:

apply plugin: 'com.android.application'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.companyname.detector"
    minSdkVersion 21
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    externalNativeBuild {
        cmake {
            cppFlags "-std=c++11 -frtti -fexceptions"
        }
    }
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
externalNativeBuild {
    cmake {
        path "CMakeLists.txt"
    }
}
sourceSets {
    main {
        jniLibs.srcDirs = ['src/main/jniLibs/']
    }
}
}

dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation project(':openCVLibrary340')
}

推荐答案

最近,NDK切换为libc ++作为默认STL,但是OpenCV是使用gnustl构建的.

Recently, NDK switched to libc++ as default STL, but OpenCV is built with gnustl.

externalNativeBuild {
  cmake {
    arguments "-DANDROID_STL=gnustl_shared"
  }
}

您的图书馆将解决此问题.

for your library will fix that.

或者,您可以使用c ++ _ shared重建OpenCV .

更新:好消息!您可以简单地下载OpenCV 4.0.1,它将与NDK r.18 +兼容.

Update: Good news! You can simply download OpenCV 4.0.1 and it will work smoothly with NDK r.18+.

这篇关于使用OpenCV 3.4.0和NDK在Android上链接错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆