【问题标题】:OpenCV 4.0.1 link failure in AndroidAndroid 中的 OpenCV 4.0.1 链接失败
【发布时间】:2019-01-26 06:45:49
【问题描述】:

我正在将 OpenCV 库从 3.2.0 迁移到 4.0.1。在这个过程中发现很多宏已经被重新定义为更多的 C++ 风格,比如 CV_BGR2RGB 现在是 COLOR_BGR2RGB。

目前在尝试链接../native/libs/x86_64/libopencv_java4.so时遇到以下链接错误

native/jni/include\opencv2/core/mat.inl.hpp:548: error: undefined reference to 'cv::error(int, std::string const&, char const*, char const*, int)'

native/jni/include\opencv2/core/mat.inl.hpp:561: error: undefined reference to 'cv::error(int, std::string const&, char const*, char const*, int)'

xxx_std.h:355: error: undefined reference to 'cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'

LabelUtils.cpp:1225: error: undefined reference to 'cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'

LabelUtils.cpp:2512: error: undefined reference to 'cv::imwrite(std::string const&, cv::_InputArray const&, std::vector<int, std::allocator<int> > const&)'

TrackingCodeDeSkew.cpp:553: error: undefined reference to 'cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'

TrackingCodeDeSkew.cpp:557: error: undefined reference to 'cv::putText(cv::_InputOutputArray const&, std::string const&, cv::Point_<int>, int, double, cv::Scalar_<double>, int, int, bool)'

这是链接器命令行

    C:\Users\koush\AppData\Local\Android\Sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  --target=x86_64-none-linux-android --gcc-toolchain=C:/Users/koush/AppData/Local/Android/Sdk/ndk-bundle/toolchains/x86_64-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/koush/AppData/Local/Android/Sdk/ndk-bundle/sysroot -fPIC -isystem C:/Users/koush/AppData/Local/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
<<list of generated object files>> -llog OpenCV401/native/libs/x86_64/libopencv_java4.so src/main/jniLibs/x86_64/libzbarjni.so src/main/jniLibs/x86_64/libiconv.so -landroid -latomic -lm "C:/Users/koush/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86_64/libsupc++.a" "C:/Users/koush/AppData/Local/Android/Sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/x86_64
/libgnustl_shared.so"

【问题讨论】:

  • 由于时间不够,我已经升级到3.4.5,保留了类似于OpenCV 3.2.0的分发格式。未报告任何错误。

标签: android c++ opencv linker


【解决方案1】:

首先,如果您考虑随时切换回 OpenCV 3.x,您将需要像这样的向后兼容标头:


/*
 * OpenCV 4.0.1 backward compatibility header.
 * */

#ifndef ANDROID_DOXCAPTUREUI_OPENCV4_COMPAT_H
#define ANDROID_DOXCAPTUREUI_OPENCV4_COMPAT_H

#include <opencv2/opencv.hpp>
#include <cmath>

#if CV_MAJOR_VERSION > 3

// namespace cv;
//{
#ifndef CV_TERMCRIT_ITER
#define CV_TERMCRIT_ITER cv::TermCriteria::MAX_ITER
#endif

#ifndef CV_BGR2GRAY
#define CV_BGR2GRAY cv::COLOR_BGR2GRAY
#endif

#ifndef CV_BGR2RGB
#define CV_BGR2RGB cv::COLOR_BGR2RGB
#endif

#ifndef CV_RGB2GRAY
#define CV_RGB2GRAY cv::COLOR_RGB2GRAY
#endif

#ifndef CV_BGRA2GRAY
#define CV_BGRA2GRAY cv::COLOR_BGRA2GRAY
#endif

#ifndef CV_GRAY2RGBA
#define CV_GRAY2RGBA cv::COLOR_GRAY2RGBA
#endif

#ifndef CV_GRAY2RGB
#define CV_GRAY2RGB cv::COLOR_GRAY2RGB
#endif

#ifndef CV_GRAY2BGRA
#define CV_GRAY2BGRA cv::COLOR_GRAY2BGRA
#endif

#ifndef CV_GRAY2BGR565
#define CV_GRAY2BGR565 cv::COLOR_GRAY2BGR565
#endif

#ifndef CV_RGBA2BGR565
#define CV_RGBA2BGR565 cv::COLOR_RGBA2BGR565
#endif

#ifndef CV_RGBA2RGB
#define CV_RGBA2RGB cv::COLOR_RGBA2RGB
#endif

#ifndef CV_RGB2RGBA
#define CV_RGB2RGBA cv::COLOR_RGB2RGBA
#endif

#ifndef CV_RGB2BGR565
#define CV_RGB2BGR565 cv::COLOR_RGB2BGR565
#endif

#ifndef CV_RGBA2BGR
#define CV_RGBA2BGR cv::COLOR_RGBA2BGR
#endif

#ifndef CV_RGBA2GRAY
#define CV_RGBA2GRAY cv::COLOR_RGBA2GRAY
#endif

#ifndef CV_ADAPTIVE_THRESH_GAUSSIAN_C
#define CV_ADAPTIVE_THRESH_GAUSSIAN_C cv::ADAPTIVE_THRESH_GAUSSIAN_C
#endif

#ifndef CV_THRESH_BINARY
#define CV_THRESH_BINARY cv::THRESH_BINARY
#endif

#ifndef CV_BGR2HSV
#define CV_BGR2HSV cv::COLOR_BGR2HSV
#endif

#ifndef CV_BGR2Lab
#define CV_BGR2Lab cv::COLOR_BGR2Lab
#endif

#ifndef CV_RGB2HSV
#define CV_RGB2HSV cv::COLOR_RGB2HSV
#endif

#ifndef CV_HSV2RGB
#define CV_HSV2RGB cv::COLOR_HSV2RGB
#endif

#ifndef CV_THRESH_OTSU
#define CV_THRESH_OTSU cv::THRESH_OTSU
#endif


#ifndef CV_THRESH_BINARY
#define CV_THRESH_BINARY THRESH_BINARY
#endif

#ifndef CV_MOP_CLOSE
#define CV_MOP_CLOSE cv::MORPH_CLOSE
#endif

#ifndef CV_RETR_CCOMP
#define CV_RETR_CCOMP cv::RETR_CCOMP
#endif

#ifndef CV_RETR_LIST
#define CV_RETR_LIST cv::RETR_LIST
#endif

#ifndef CV_RETR_TREE
#define CV_RETR_TREE cv::RETR_TREE
#endif

#ifndef CV_RETR_FLOODFILL
#define CV_RETR_FLOODFILL cv::RETR_FLOODFILL
#endif

#ifndef CV_RETR_EXTERNAL
#define CV_RETR_EXTERNAL cv::RETR_EXTERNAL
#endif

#ifndef CV_CHAIN_APPROX_SIMPLE
#define CV_CHAIN_APPROX_SIMPLE cv::CHAIN_APPROX_SIMPLE
#endif

#ifndef CV_CHAIN_APPROX_TC89_KCOS
#define CV_CHAIN_APPROX_TC89_KCOS cv::CHAIN_APPROX_TC89_KCOS
#endif

#ifndef CV_FILLED
#define CV_FILLED cv::FILLED
#endif


#ifndef CV_REDUCE_SUM
#define CV_REDUCE_SUM cv::REDUCE_SUM
#endif

#ifndef CV_StsBadArg
#define CV_StsBadArg cv::Error::StsBadArg
#endif
//}

#ifndef cvFastArctan
#define cvFastArctan( Y, X )  ( std::atan2( (Y), (X) ) )
#endif

#endif //endif CV_MAJOR_VERSION

#endif //ANDROID_DOXCAPTUREUI_OPENCV4_COMPAT_H

自 NDK r16 版本以来,对于 LLVM 的 libc++,Android NDK 是 switched。随着新的主要版本 4.0,OpenCV 也从 GNU 的 libstdc++ 切换到 libc++。如果您设置“-DANDRID_STL=gnustl_shared”,它将不起作用,因为默认的 OpenCV 二进制文件是使用 libc++ 而不是 gnustl 构建的。您应该在 build.gradle 文件中设置 cmake 参数“-DANDROID_STL=c++_shared”,如下所示:

    externalNativeBuild {
            cmake {
               //sample cpp flag parameters
                cppFlags "-std=c++14 -Ofast -Rpass-analysis=loop-vectorize -fsave-optimization-record -fdiagnostics-show-hotness"
           //sample abi filter parameters
                abiFilters 'x86', 'x86_64', 'armeabi-v7a', 'arm64-v8a'
    //set -DANDROID_STL to c++_shared
                arguments "-DANDROID_STL=c++_shared" 
            }
        }

【讨论】:

  • @Yashin 感谢您的回复,等我回来再试一试。不过有一个问题——我为什么要定义宏?这些方法在 4.x 中的方法是否发生了变化?
  • OpenCV 4.x 更改了一些宏名称(例如 CV_BGR2HSV --> COLOR_BGR2GRAY)。如果您有一个使用 OpenCV 3.x 编写的代码库,那么您的代码库中使用的一些宏将会出错。此外,我附加到我的答案的标题是一种 hack,我认为除了定义宏之外,应该有更好的方法来支持向后兼容性。
  • 很高兴分享已经使用 OpenCV 4.2.0 一段时间了。不需要定义无关的宏。使用 @Yasin-Yildirim 概述的新命名风格,如 COLOR_BGR2GRAY
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多