2016-01-04 65 views
2

我剛剛通過執行ndk-build開發了使用C++代碼的Android應用程序。我一直使用OpenCV的,所以在我Android.mk有:Gradle無法找到用於NDK調試的OpenCV庫

include $(CLEAR_VARS) 

OPENCV_LIB_TYPE:=SHARED 
OPENCV_CAMERA_MODULES:=off 
OPENCV_INSTALL_MODULES:=on 

include /Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/OpenCV.mk 

include $(OPENCV_PATH) 

,並一直工作良好。

現在我想用Android Studio調試C++代碼,我不得不在Gradle中做一些修改。

第一件事,gradle沒有看到包含文件,but I read this question in StackOverflow教會了我如何包含這些文件。現在,當我嘗試構建我的應用程序時,出現以下錯誤:

Error:(60) undefined reference to 'cv::_InputArray::_InputArray(cv::Mat const&)'

Error:(60) undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'

Error:(60) undefined reference to 'cv::cvtColor(cv::_InputArray const&, cv::_OutputArray const&, int, int)'

Error:(62) undefined reference to 'cv::_InputArray::_InputArray(cv::Mat const&)'

Error:(62) undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'

Error:(62) undefined reference to 'cv::resize(cv::_InputArray const&, cv::OutputArray const&, cv::Size, double, double, int)'

Error:(73) undefined reference to 'cv::Mat::t() const'

Error:(73) undefined reference to 'cv::_InputArray::_InputArray(cv::MatExpr const&)'

Error:(73) undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'

這使我懷疑沒有找到庫。 我嘗試將其添加到我的cFlags爲:

productFlavors { 
    x86 { 
     flavorDimension "abi" 
     ndk { 
      abiFilter "x86" 
      stl "stlport_shared" 
      cFlags "-std=c99 " + 
        "-fexceptions " + 
        "-I/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/include " + 
        "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/x86 " + 
        "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab" 
     } 
    } 
    arm { 
     flavorDimension "abi" 
     ndk { 
      abiFilter "armeabi-v7a" 
      stl "stlport_shared" 
      cFlags "-std=c99 " + 
        "-fexceptions " + 
        "-I/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/include " + 
        "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/armeabi-v7a " + 
        "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab" 
     } 
    } 
} 

我曾經嘗試這樣做,以及:

buildTypes { 
    debug { 
     debuggable true 
     jniDebuggable = true 
     ndk { 
      abiFilter "x86" 
      stl "stlport_shared" 
      cFlags "-std=c99 " + 
        "-fexceptions " + 
        "-I/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/jni/include " + 
        "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/x86 " + 
        "-L/Users/rafaelruizmunoz/Desktop/Android_Tools/OpenCV-2.4.9-android-sdk/sdk/native/libs/armeabi-v7a " + 
        "-lopencv_calib3d -lopencv_contrib -lopencv_core -lopencv_features2d -lopencv_flann -lopencv_gpu -lopencv_highgui -lopencv_imgproc -lopencv_legacy -lopencv_ml -lopencv_nonfree -lopencv_objdetect -lopencv_ocl -lopencv_photo -lopencv_stitching -lopencv_superres -lopencv_ts -lopencv_video -lopencv_videostab" 
    } 
    release { 
     debuggable true 
     minifyEnabled false 
     proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 
    } 

} 

但錯誤依然存在。這就像它無法找到這些庫。

我應該怎麼做?

預先感謝您。

回答

0

您需要ldLibsldFlags指定鏈接庫。隨着exeprimental插件,我用下面的代碼片段:

def appAbi = "armeabi-v7a" 
def OpenCV_sdk = "~/opencv/sdk" 
… 
model { android.ndk { 
… 
    ldFlags += "-L$OpenCV_sdk/native/libs/$appAbi".toString() 
    ldFlags += "-L$OpenCV_sdk/native/3rdparty/libs/$appAbi".toString() 
    ldLibs += ['opencv_imgproc', 'opencv_core', 'opencv_hal', 'tbb'] 
} } 
+0

感謝亞歷克斯,但是,如果我想編譯超過1個架構是什麼? –

+0

您可以爲每種口味添加這三行。 –

+0

嗨@AlexCohn,你能幫助我:https://stackoverflow.com/questions/47903357/android-studio-ndk-linker-error-undefined-reference-to-cv-outputarray-outpu –