OpenCv与Android studio 2.2+一起使用新的classl与cmake - undefined reference

时间:2016-09-24 15:23:08

标签: android opencv android-ndk android-studio-2.2

我在使用Android Studio 2.2的原生OpenCv 3.0.0时遇到了麻烦,并且支持新的ndk,即使用CMAKE构建脚本。以下是我得到的错误。我在我的gradel或cmake文件中遗漏了什么?请告诉我。

  

错误:FAILURE:构建因异常而失败。

  • 出了什么问题: 任务':app:externalNativeBuildDebug'执行失败。
      

    构建命令失败。   执行' C:\ Users \ User \ AppData \ Local \ Android \ sdk \ cmake \ 3.6.3155560 \ bin \ cmake.exe'时出错带参数{--build E:\ OpenCvAndroid \ OPecvTry2CSupport \ app.externalNativeBuild \ cmake \ debug \ mips64 --target native-lib}   [1/1]链接CXX共享库.. \ _ obj \ mips64 \ libnative-lib.so   失败:cmd.exe / C" cd。 &安培;&安培; C:\ Users \ User \ AppData \ Local \ Android \ sdk \ ndk-bundle \ toolchains \ llvm \ prebuilt \ windows-x86_64 \ bin \ clang ++。exe -target mips64el-none-linux-android -gcc-toolchain C:/ Users / User / AppData / Local / Android / sdk / ndk-bundle / toolchains / mips64el-linux-android-4.9 / prebuilt / windows-x86_64 --sysroot = C:/ Users / User / 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 / User / 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:在函数~Mat': E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:278: undefined reference to cv :: fastFree(void *)'   CMakeFiles / native-lib.dir / src / main / cpp / native-lib.cpp.o:在函数cv::Mat::release()': E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: undefined reference to cv :: Mat :: deallocate()'   clang ++。exe:错误:链接器命令失败,退出代码为1(使用-v查看调用)   忍者:构建停止:子命令失败。

和我的Cmake.txt

 cmake_minimum_required(VERSION 3.4.1)
 add_library( native-lib
         SHARED
         src/main/cpp/native-lib.cpp )
include_directories(E\:\\OpenCVSdk\\sdk\\native\\jni\\include )
find_library( log-lib log )
target_link_libraries(native-lib ${log-lib} )

我的Gradel Build文件

 {    apply plugin: 'com.android.application'

 android {

compileSdkVersion 24
buildToolsVersion "24.0.2"
defaultConfig {
    applicationId "viki.opecvtry2csupport"
    minSdkVersion 16
    targetSdkVersion 24
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"



    externalNativeBuild {
        cmake {
            cppFlags ""

        }
    }
}

2 个答案:

答案 0 :(得分:4)

我通过将“lib_opencv”lib添加到target_link_libraries来修复我的问题

target_link_libraries(
                   native-lib lib_opencv #Just add the lib_opencv
                   ${log-lib} )

不要忘记Link Gradle to your native library

我的完整CMakeList.txt

cmake_minimum_required(VERSION 3.4.1)

add_library( native-lib
             SHARED
             src/main/cpp/native-lib.cpp )

find_library( log-lib
              log )

target_link_libraries(native-lib lib_opencv
                       ${log-lib} )

set(CMAKE_VERBOSE_MAKEFILE on)
add_library(lib_opencv SHARED IMPORTED)
set_target_properties(lib_opencv PROPERTIES IMPORTED_LOCATION /PROJECT_PATH/openCVLibrary320/src/main/jniLibs/${ANDROID_ABI}/libopencv_java3.so)

include_directories(/OpenCV-android-sdk/sdk/native/jni/include)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=gnu++11")

答案 1 :(得分:2)

使用OpenCV 3.1和AS 2.2以及最新的Gradle插件设置OpenCV_DIR对我有用。这是我的CMakeLists.txt的相关内容。

cmake_minimum_required(VERSION 3.6)

SET(OpenCV_DIR $ENV{HOME}/AndroidDevelopment/opencv-3.1.0/sdk/native/jni)

find_package(OpenCV REQUIRED)
message(STATUS "opencv found: ${OpenCV_LIBS}")

include_directories(${CMAKE_CURRENT_SOURCE_DIR}
                    ${OpenCV_DIR}/include/)
[...]
target_link_libraries(myTarget log ${OpenCV_LIBS} m z android )