如何用NDK引用OpenCV的hpp文件?

时间:2016-10-02 11:40:38

标签: android c++ opencv android-ndk

我无法让我的项目打开OpenCV的头文件:

enter image description here

但是,通过在OpenCVLoader.initDebug()中调用onCreate(),我可以看到OpenCV运行正常(如果我注释掉那些包含):

enter image description here

read您需要编辑我首先没有的 Android.mk 。那我错过了什么?

编辑(3小时后):

好的,所以我想出了如何包含.hpp文件我将其添加到CMakeLists.txt的末尾:

include_directories("${CMAKE_SOURCE_DIR}/include"
    "${CMAKE_SOURCE_DIR}/include/opencv"
    "${CMAKE_SOURCE_DIR}/include/opencv2"
    "${CMAKE_SOURCE_DIR}/include/opencv2/core"
    "${CMAKE_SOURCE_DIR}/include/opencv2/features2d"
    "${CMAKE_SOURCE_DIR}/include/opencv2/imgproc")

    add_library(opencv_core STATIC IMPORTED)
    set_target_properties(opencv_core PROPERTIES
        IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt/libs/armeabi/libopencv_core.a)

    add_library(opencv_imgproc STATIC IMPORTED)
    set_target_properties(opencv_imgproc PROPERTIES
        IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt/libs/armeabi/libopencv_imgproc.a)

    add_library(opencv_features2d STATIC IMPORTED)
    set_target_properties(opencv_features2d PROPERTIES
        IMPORTED_LOCATION ${CMAKE_SOURCE_DIR}/prebuilt/libs/armeabi/libopencv_features2d.a)

现在链接时出现undefined reference错误:

> Build command failed.
Error while executing 'C:\Users\kfjier\AppData\Local\Android\sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build E:\Dropbox\Freelance\BowlingOpenCV\Bowlingtrainer\app\.externalNativeBuild\cmake\debug\mips64 --target native-lib}
[1/2] Building CXX object CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o
[2/2] Linking CXX shared library ..\obj\mips64\libnative-lib.so
FAILED: cmd.exe /C "cd . && C:\Users\kfjier\AppData\Local\Android\sdk\ndk-bundle\toolchains\llvm\prebuilt\windows-x86_64\bin\clang++.exe  -target mips64el-none-linux-android -gcc-toolchain C:/Users/kfjier/AppData/Local/Android/sdk/ndk-bundle/toolchains/mips64el-linux-android-4.9/prebuilt/windows-x86_64 --sysroot=C:/Users/kfjier/AppData/Local/Android/sdk/ndk-bundle/platforms/android-21/arch-mips64 -fPIC -g -DANDROID -ffunction-sections -funwind-tables -fstack-protector-strong -no-canonical-prefixes -Wa,--noexecstack -Wformat -Werror=format-security -fno-exceptions -fno-rtti  -O0 -fno-limit-debug-info  -Wl,--build-id -Wl,--warn-shared-textrel -Wl,--fatal-warnings -Wl,--no-undefined -Wl,-z,noexecstack -Qunused-arguments -Wl,-z,relro -Wl,-z,now -shared -Wl,-soname,libnative-lib.so -o ..\obj\mips64\libnative-lib.so CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o  -llog -lm "C:/Users/kfjier/AppData/Local/Android/sdk/ndk-bundle/sources/cxx-stl/gnu-libstdc++/4.9/libs/mips64/libgnustl_static.a" && cd ."
CMakeFiles/native-lib.dir/src/main/cpp/native-lib.cpp.o: In function `Java_eu_codehunter_bowlingtrainer_MainActivity_FindFeatures':
  E:\Dropbox\Freelance\BowlingOpenCV\Bowlingtrainer\app\src\main\cpp/native-lib.cpp:18: undefined reference to `cv::FastFeatureDetector::create(int, bool, int)'
  E:\Dropbox\Freelance\BowlingOpenCV\Bowlingtrainer\app\src\main\cpp/native-lib.cpp:19: undefined reference to `cv::noArray()'
  E:\Dropbox\Freelance\BowlingOpenCV\Bowlingtrainer\app\src\main\cpp/native-lib.cpp:23: undefined reference to `cv::circle(cv::_InputOutputArray const&, cv::Point_<int>, int, cv::Scalar_<double> const&, int, int, int)'
  clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation)
  ninja: build stopped: subcommand failed.

为什么?

1 个答案:

答案 0 :(得分:0)

我认为cv :: FastFeatureDetector类属于libopencv_features2d.a而cv :: circle()函数属于libopencv_highgui.a。所以你必须包括这些库。一种更简单的方法是将单个libopencv_java3.so添加为共享库,该库包含所有OpenCV库。