NDK下OpenCV undefined reference to `cv::imencode std::__ndk1::vector 异常

报错日志

CMakeFiles/enhance_lib.dir/src/main/cpp/ImageProcess.cpp.o: In function `ImageEnhancementWithDecode':
/home/albertsnow/WorkSpace/NubiyaARThai/documentlibrary/src/main/cpp/ImageProcess.cpp:347: undefined reference to `cv::imencode(cv::String const&, cv::_InputArray const&, std::__ndk1::vector >&, std::__ndk1::vector > const&)'
clang++: error: linker command failed with exit code 1 (use -v to see invocation)
ninja: build stopped: subcommand failed

解决方案

StackOverFlow
Recently, NDK switched to libc++ as default STL, but OpenCV is built with gnustl. for your library will fix that.
Alternatively, you can rebuild OpenCV with c++_shared.
Update: Good news! You can simply download OpenCV 4.0.1 and it will work smoothly with NDK r.18+.

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

新版本NDK使用libc++作为STL,但OpenCV使用的gnustl。
所以:

  1. NDK降级
  2. 重新编译OpenCV 使用libc++作为STL

我采用的NDK降级

mv ~/Android/Sdk/ndk-bundle ~/Android/Sdk/ndk-bundle-backup
wget https://dl.google.com/android/repository/android-ndk-r17c-linux-x86_64.zip
unzip android-ndk-r17c-linux-x86_64.zip
mv android-ndk-r17c ~/Android/Sdk/ndk-bundle

问题修复

PS

有人说是缺乏 libavcodec,但我测试结果是没用

你可能感兴趣的:(Android,Opencv,ndk)