android ndk opencv 3,c++ - Linking errors on Android with OpenCV 3.4.0 and NDK - Stack Overflow

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.

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<:keypoint std::__ndk1::allocator> >&)'

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<:__ndk1::vector std::__ndk1::allocator> >, std::__ndk1::allocator<:__ndk1::vector std::__ndk1::allocator> > > >&, 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();

and

//code performing key point descriptors matching

cv::Ptr<:descriptormatcher> matcher = cv::DescriptorMatcher::create(cv::DescriptorMatcher::BRUTEFORCE_HAMMING);

vector> matchesTmp;

//instruction responsible for the link error

matcher->radiusMatch(this->sortedMatches.at(refSortedIndex).refImage->getDescriptors(),

this->testImage->getDescriptors(), matchesTmp, matchThreshold);

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).

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).

Does anyone know what can cause this and how I could fix it? Is it a known bug in OpenCV or is it my configuration that is not supported or just wrong?

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})

gradle:

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')

}

内容概要:本文详细介绍了由中国某顶尖科技公司推出的专注于通用人工智能的应用工具DeepSeek。首先是入门篇,阐述了DeepSeek的基本介绍、所需前期准备工作以及具体的使用步骤,指导使用者如何正确提问以获取最佳效果;然后是进阶部分,深入探讨各种实用功能的操作,如三种模式间的转换、文件处理能力、多语言翻译、温度参数设定和提示语的设计方法等内容;最终到达精通层面,展示了一系列应用场景,包括但不限于协助撰写学术论文、助力自媒体创作、构建个人学习计划及编程协助等方面的具体实施,并讲解了一些高阶技巧,比如思维链开放机制、创建专属领域内训练好的自定义AI模型,还有结合其他生产力软件提升工作效率的方法。同时提及如何利用多轮连续交互的方式解决更加错综复杂的问题。 适用人群:面向对DeepSeek这款AI工具感兴趣的开发者、研究员或是寻求提升工作效率的专业人士。 使用场景及目标:适用于初次接触DeepSeek的新手,希望通过循序渐进的学习路径来逐步掌握这个强大的工具;也为有经验用户提供更广泛的想象空间和技术支持,以充分发挥DeepSeek的价值并促进工作流优化。对于研究人员和教育工作者来说,它也可以成为科研助手和教学资源。 其他说明:该工具不仅局限于单一领域的使用,在多个方面均有涉猎并展现出色的表现力。无论您身处哪个行业,都可以基于自身需求发掘出DeepSeek更多的潜力所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值