2016-08-03 25 views
0

我試圖用現有的使用OpenCV的C++代碼來構建Android應用程序。 但是Android NDK說: 「未定義的引用 'TestMath :: getHello()'」未定義的函數Android NDK

這裏是我的Android.mk:

LOCAL_PATH := $(call my-dir) 

include $(CLEAR_VARS) 
#opencv 
OPENCVROOT := /mypath/OpenCV-android-sdk 
OPENCV_CAMERA_MODULES := off 
OPENCV_INSTALL_MODULES := off 
OPENCV_LIB_TYPE := SHARED 
include ${OPENCVROOT}/sdk/native/jni/OpenCV.mk 

LOCAL_MODULE := CrossMath 
LOCAL_SRC_FILES := com_testapp_recognition_TestMath.cpp 
LOCAL_SHARED_LIBRARIES := -lopencv_java3 
include $(BUILD_SHARED_LIBRARY) 

Application.mk:

APP_ABI := all 
APP_CPPFLAGS := -frtti -fexceptions -std=c++11 
APP_STL := gnustl_static 
APP_PLATFORM := android-16 

com_testapp_recognition_TestMath。 hpp:

#include <jni.h> 
#include "CrossMath/TestMath.hpp" 
#ifndef _Included_com_testapp_recognition_TestMath 
#define _Included_com_testapp_recognition_TestMath 

#ifdef __cplusplus 
extern "C" { 
#endif 

JNIEXPORT jint JNICALL Java_com_testapp_recognition_TestMath_recognize(JNIEnv *, jobject, cv::Mat& originalImage); 

#ifdef __cplusplus 
} 
#endif 
#endif 

com_testapp_recognition_TestMath.cpp:

#include "com_testapp_recognition_TestMath.hpp" 
JNIEXPORT jint JNICALL Java_com_testapp_recognition_TestMath_recognize(JNIEnv *, jobject, cv::Mat& originalImage) { 
    return TestMath::getHello().size(); 
} 

最後TestMath.cpp其位於子文件夾CrossMath:

#include "TestMath.hpp" 

namespace TestMath { 
    string getHello() { 
     return "Hello"; 
    } 
} 

TestMath.hpp:

#ifndef TestMath_hpp 
#define TestMath_hpp 

#include <stdio.h> 
#include <iostream> 
#include "opencv2/core/core_c.h" 
#include "opencv2/opencv.hpp" 
#include "opencv2/highgui.hpp" 

namespace TestMath { 
    string getHello(); 
} 

Java類和定義,我檢查路徑和包括文件的其他人員。

錯誤:

Error:(13) undefined reference to `TestMath::getHello()' 
+0

請添加完整且準確的錯誤信息。我真的不明白'CrossMath/TestMath.hpp'的重點。函數實現不應該放在頭文件中。 – Michael

+0

@Michael顯然是錯字... –

+0

字符串應該是什麼? '的std :: string'?如果是這樣,你在哪裏包括'',以及你在哪裏聲明你希望它是來自'std'命名空間的'string'? – Michael

回答

2

你在你的LOCAL_SRC_FILES失蹤CrossMath/TestMath.cpp

除此之外,如果你指的是你的代碼string應該是std::string您需要在TestMath.hpp<string>和類型更改爲std::string

+0

根據[android.mk](http://android.mk/)「你不需要列出你的Android.mk中生成的文件之間的頭文件或明確的依賴關係」或者它的意思是不要放在那裏.hpp文件? –

+0

這是指頭文件和生成的文件(不管是什麼意思)。 'LOCAL_SRC_FILES'應該包含_「構建系統用於生成模塊的源文件列表」_。 – Michael

0

錯誤消息 「未定義的引用 'TestMath :: getHello()'」 說,NDK工具無法找到TestMath::getHello()實施。

試試下面com_testapp_recognition_TestMath.cpp:

#include "TestMath.hpp" 
namespace TestMath { 
    string getHello() { 
     return "Hello"; 
    } 
} 
#include "com_testapp_recognition_TestMath.hpp" 
    JNIEXPORT jint JNICALL Java_com_testapp_recognition_TestMath_recognize(JNIEnv *, jobject, cv::Mat& originalImage) { 
    return TestMath::getHello().size(); 
} 
+0

主要目標不是在com_testapp_recognition_TestMath.cpp中編寫整個實現。但是要包含部分純粹的C++代碼以從JNI包裝器運行。 –