编译包含OpenCV头文件的JNI库

时间:2013-07-24 11:58:05

标签: android opencv compilation android-ndk java-native-interface

HY!

我有一个带有需要编译的cpp文件的Android项目。此文件包含OpenCV库的一些标头。您可以在Android.mk文件中看到OpenCV源文件位于jni / include / opencv2文件夹下。当我尝试使用ndk-build编译文件时,编译器会出现以下错误:

"undefined reference to"

对于所有这些文件来说,这似乎是一个“错误:未定义的引用”。

Android.mk文件包含以下内容:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

OPENCV_PACKAGE_DIR := $(LOCAL_PATH)/include

LOCAL_MODULE := iqindex

LOCAL_C_INCLUDES := \
    $(LOCAL_PATH) \
    $(OPENCV_PACKAGE_DIR)

LOCAL_SRC_FILES := \
    main.cpp

LOCAL_SHARED_LIBRARIES := iqindex

LOCAL_LDLIBS := -llog -lz

include $(BUILD_SHARED_LIBRARY)

我没有足够的jni / ndk经验,我的问题是:

  1. 编译一个需要另一个头文件的.cpp文件就足以用ndk-build命令编译它了,它会全部完成吗?

  2. 我尝试编译它有什么问题? Android.mk有什么问题?

  3. 编辑:控制台错误如下:

    C:\MyName\My Project App>ndk-build
    
    C:/MyName/android-ndk-r8e/build/core/add-application.mk:128: Android NDK: WARNING: APP_PLATFORM android-14 is larger than android:minSdkVersion 8 in ./AndroidManifest.xml
    SharedLibrary  : libiqindex.so
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o:(.ARM.extab.text.unlikely._ZL6jthrowP7_JNIEnv+0x2c): error: undefined reference to 'typeinfo for cv::Exception'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function cv::Mat::Mat(cv::Mat const&):jni/include/opencv2/core/mat.hpp:125: error: undefined reference to 'cv::Mat::copySize(cv::Mat const&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function cv::Mat::release():jni/include/opencv2/core/mat.hpp:364: error: undefined reference to 'cv::Mat::deallocate()'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function cv::Mat::~Mat():jni/include/opencv2/core/mat.hpp:278: error: undefined reference to 'cv::fastFree(void*)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Mat_submat_1n:jni/include/opencv2/core/mat.hpp:376: error: undefined reference to 'cv::Mat::Mat(cv::Mat const&, cv::Rect_<int> const&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Mat_resize_1n:jni/main.cpp:111: error: undefined reference to 'cv::_InputArray::_InputArray(cv::Mat const&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Mat_resize_1n:jni/main.cpp:111: error: undefined reference to 'cv::_OutputArray::_OutputArray(cv::Mat&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Mat_resize_1n:jni/main.cpp:111: error: undefined reference to 'cv::resize(cv::_InputArray const&, cv::_OutputArray const&, cv::Size_<int>, double, double, int)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Utils_rotateData:jni/main.cpp:124: error: undefined reference to'cv::_InputArray::_InputArray(cv::Mat const&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Utils_rotateData:jni/main.cpp:124: error: undefined reference to'cv::_OutputArray::_OutputArray(cv::Mat&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Utils_rotateData:jni/main.cpp:124: error: undefined reference to'cv::flip(cv::_InputArray const&, cv::_OutputArray const&, int)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_Utils_rotateData:jni/main.cpp:125: error: undefined reference to'cv::Mat::t() const'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_IQLocal_nativeCreate:jni/main.cpp:139: error: undefined reference to 'get_instance()'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_IQLocal_load:jni/main.cpp:156: error: undefined reference to 'IQIndex::load(std::string, std::string)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_IQLocal_match:jni/main.cpp:171: error: undefined reference to 'IQIndex::match(cv::Mat&)'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_IQLocal_train:jni/main.cpp:182: error: undefined reference to 'IQIndex::train()'
    
    C:/MyName/android-ndk-r8e/toolchains/arm-linux-androideabi-4.6/prebuilt/windows/bin/../lib/gcc/arm-linux-androideabi/4.6/../../../../arm-linux-androideabi/bin/ld.exe: ./obj/local/armeabi-v7a/objs/iqindex/main.o: in function Java_project_name_zxing_IQLocal_compute:jni/main.cpp:198: error: undefined reference to 'IQIndex::compute(cv::Mat const&, char const*, char const*)'
    
    collect2: ld returned 1 exit status
    
    make: *** [obj/local/armeabi-v7a/libiqindex.so] Error 1
    

    第二次编辑:现在Android.mk看起来像这样:

    LOCAL_PATH := $(call my-dir)
    
    include $(CLEAR_VARS)
    
    # OpenCV
    OPENCV_CAMERA_MODULES:=on
    OPENCV_INSTALL_MODULES:=on
    
    include C:/Emil/downloads/OpenCV-2.4.6-android-sdk-r2/OpenCV-2.4.6-android-sdk/sdk    /native/jni/OpenCV.mk
    
    LOCAL_MODULE := iqindex
    
    LOCAL_SRC_FILES := \
        main.cpp
    
    LOCAL_C_INCLUDES := \
        $(LOCAL_PATH)/iqindex.h
    
    LOCAL_LDLIBS := -llog -lz
    
    include $(BUILD_SHARED_LIBRARY)
    

2 个答案:

答案 0 :(得分:2)

我想您忘了将OpenCV libriries添加到您的项目中。因此链接器无法找到与OpenCV相关的函数。

From OpenCV documentation:

  

如果您的应用程序项目没有JNI部分,请复制   来自的相应OpenCV原生库   / sdk / native / libs /到你的   项目目录到文件夹libs /.

     

如果应用程序项目带有JNI部分,而不是手动   复制你的库需要修改你的Android.mk文件:添加   在“include $(CLEAR_VARS)”之后的两个代码行之后   “包括path_to_OpenCV-2.4.6-android-sdk / sdk / native / jni / OpenCV.mk   OPENCV_CAMERA_MODULES:= OPENCV_INSTALL_MODULES:= on

所以你的Android.mk中应该有以下几行:

include $(CLEAR_VARS)
# OpenCV
OPENCV_CAMERA_MODULES:=on
OPENCV_INSTALL_MODULES:=on
include ../../sdk/native/jni/OpenCV.mk

答案 1 :(得分:1)

这已经很晚了,但回应:

  

现在我有另一个问题:我在jni文件夹中有一个名为iqindex.h的头文件。 cpp文件包含它,但现在我有&#34;未定义的引用&#34; iqindex.h当我尝试构建时。我在Android.mk中尝试了指向头文件的LOCAL_C_INCLUDES命令,但是在编译时它还没有看到openCV。如何包含此标头以使文件编译?

问题可能是您使用了LOCAL_C_INCLUDES :=/foo/bar

试试LOCAL_C_INCLUDES +=/foo/bar。 您应该添加include目录,而不是覆盖现有的包含。