2016-09-24 65 views
5

我在使用原生OpenCv 3.0.0與Android Studio 2.2,與新的ndk支持即使用CMAKE生成腳本時遇到問題。下面是我得到的錯誤。我在我的gradel或cmake文件中丟失了任何東西?請告訴我。OpenCv與Android studio 2.2+使用新的gradel與cmake - 未定義的參考

Error:FAILURE: Build failed with an exception.

  • 出了什麼問題: 執行失敗的任務 ':應用程序:externalNativeBuildDebug'。

    Build command failed. Error while executing 'C:\Users\User\AppData\Local\Android\sdk\cmake\3.6.3155560\bin\cmake.exe' with arguments {--build E:\OpenCvAndroid\OPecvTry2CSupport\app.externalNativeBuild\cmake\debug\mips64 --target native-lib} [1/1] Linking CXX shared library ..\obj\mips64\libnative-lib.so FAILED: 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: In function ~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: In function cv::Mat::release()': E:/OpenCVSdk/sdk/native/jni/include\opencv2/core/mat.hpp:367: undefined reference to cv::Mat::deallocate()' clang++.exe: error: linker command failed with exit code 1 (use -v to see invocation) ninja: build stopped: subcommand failed.

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

我的格拉德爾構建文件

{ 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 "" 

     } 
    } 
} 
+0

包括目錄應留下cmake語法,我想:include_directories(path/to/your/sdk/native/jni/include) target_link_libraries(native-lib log) – Gerry

+0

我在這裏與導入的ffmpeg共享庫有類似的問題,您是否找到解決方案? –

+0

我仍然沒有找到解決方案 – vicky

回答

2

設置OpenCV_DIR很適合我用OpenCV的3.1和2.2 AS和最新的Gradle插件。這是我的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) 
+0

'''include_directories(...)如果使用CMake> = 2.8.11來編譯OpenCV,則不需要''' – ahasbini

3

我固定我的問題,通過添加 「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")