我开始了解Android NDK。我还没有学到很多关于C / C ++编码的知识,并且想要开始。我导入了简单的HelloJni示例,并且一直在乱搞这个以及文档。我被困住了,并且觉得我可能正在使用Android.mk。看起来它是正确的但是构建不适用于我正在测试的第二个模块。这是我在
中的内容 <project folder>/jni/
HelloJni.cpp
Android.mk
Application.mk
mytools/
simplemath.h
simplemath.cpp
我想用我的java来调用HelloJni.cpp中的一个方法,而HelloJni.cpp又调用simplemath.cpp中的方法并返回一个值。简单的测试,只是从简单中添加数字。
以下是我的每个
的所有来源--------------------- Android.mk
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
include $(CLEAR_VARS)
TOOLS_DIR := $(LOCAL_PATH)/mytools
LOCAL_C_INCLUDES := $(TOOLS_DIR)
LOCAL_MODULE := simplemath
LOCAL_SRC_FILES := $(TOOLS_DIR)/simplemath.cpp
LOCAL_LDLIBS := -L$(SYSROOT)/usr/lib -llog
include $(BUILD_SHARED_LIBRARY)
--------------------- HelloJni.cpp
#include <stl/_istream.h>
#include <stl/_string.h>
#include <stl/_string_fwd.h>
#define DEBUG_TAG "JNI Native Code..."
using namespace std;
extern "C" {
JNIEXPORT jstring JNICALL Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env, jobject thiz, jstring str){
simplemath mathTools;
mathTools.add(4, 5);
.....
return env->NewStringUTF(str);
}
};
--------------------- simplemath.h
#ifdef __cplusplus
extern "C" {
#endif
#define SIMPLEMATH_H
namespace std {
class simplemath{
public:
simplemath();
int add(int, int);
~simplemath();
};
}
#ifdef __cplusplus
}
#endif
----------------- simplemath.cpp
#include <simplemath.h>
namespace std {
simplemath::simplemath(){
}
int simplemath::add(int a, int b){
return a + b;
}
simplemath::~simplemath(){
}
}
当我对项目进行清理时,我没有错误。构建之后,我得到以下内容:
"Compile++ thumb : HelloJni <= HelloJni.cpp
Prebuilt : libstlport_static.a <= <NDK>/sources/cxx-stl/stlport/libs/armeabi/
SharedLibrary : libHelloJni.so
C:/Users/Geek/android-ndk-r8d/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/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:88: error: undefined reference to 'std::simplemath::simplemath()'
C:/Users/Geek/android-ndk-r8d/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/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:89: error: undefined reference to 'std::simplemath::add(int, int)'
C:/Users/Geek/android-ndk-r8d/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/objs/HelloJni/HelloJni.o: in function Java_com_example_hellojni_HelloJni_stringFromJNI:jni/HelloJni.cpp:92: error: undefined reference to 'std::simplemath::~simplemath()'
collect2: ld returned 1 exit status
make: *** [obj/local/armeabi/libHelloJni.so] Error 1