2013-07-24 68 views
0

Hy!編譯包含OpenCV頭文件的JNI庫

我有一個需要編譯的cpp文件的Android項目。該文件包含一些OpenCV庫的頭文件。 OpenCV源文件位於jni/include/opencv2文件夾下,如Android.mk文件中所示。當我嘗試用編譯NDK,打造編譯器提供了以下錯誤的文件:

"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有什麼問題?

編輯:控制檯錯誤是:

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) 
+0

你能發佈完整的錯誤嗎?這看起來像一個鏈接器錯誤。 – dudebrobro

回答

2

我想你忘了加的OpenCV給你的項目進行libriries。所以鏈接器找不到OpenCV相關的功能。

From OpenCV documentation:

If your application project doesn’t have a JNI part, just copy the corresponding OpenCV native libs from /sdk/native/libs/ to your project directory to folder libs/.

In case of the application project with a JNI part, instead of manual libraries copying you need to modify your Android.mk file: add the following two code lines after the "include $(CLEAR_VARS)" and before "include path_to_OpenCV-2.4.6-android-sdk/sdk/native/jni/OpenCV.mk OPENCV_CAMERA_MODULES:=on OPENCV_INSTALL_MODULES:=on

所以,你應該在你的Android.mk下面幾行:

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

我做了你在這裏寫的和「未定義的參考」OpenCV消失了。 Android.mk在SECOND EDIT中。現在我有另一個問題:我在jni文件夾中有一個名爲iqindex.h的頭文件。 cpp文件包含它,但是現在當我嘗試構建時,我有「未定義的引用」iqindex.h。我嘗試了Android.mk中指向頭文件的LOCAL_C_INCLUDES命令,但是在編譯時它不再看到openCV。我如何包含這個頭文件來編譯這些文件? –

+0

鏈接是我的問題! 我在添加這些依賴關係後解決了它。 – AnkitRox

+0

有時,在Android的情況下;在Android.mk中提及'LOCAL_LDLIBS:= -lz -lm -llog -landroid -ldl'將會工作.. – AnkitRox

1

這已經晚了,但在應對:

Now I have another problem: I have in the jni folder a header file named iqindex.h . The cpp file includes it, but now I have "undefined reference to" iqindex.h when I try to build. I tried the LOCAL_C_INCLUDES command in Android.mk which pointed to the header file, but then it doesn't see openCV anymore at compile time. How do I include this header to make the files to compile ?

的問題可能是你用過LOCAL_C_INCLUDES :=/foo/bar

嘗試LOCAL_C_INCLUDES +=/foo/bar。 您應該添加包含目錄,而不是覆蓋現有的包含。

+0

重要注意事項:'LOCAL_C_INCLUDES'列出目錄,而不是單個包含文件! –