简体   繁体   English

Android NDK JNI未定义对C ++源方法的引用

[英]Android NDK JNI undefined reference to C++ source methods

I am starting to learn about the Android NDK. 我开始学习Android NDK。 I haven't learned much about C/C++ coding and would like to start. 我对C / C ++编码了解不多,所以想开始。 I imported the simple HelloJni sample and have been messing around with this to learn as well as the docs. 我导入了简单的HelloJni示例,并且一直在弄混它以学习文档。 I am stuck and feel it may be how I am using the Android.mk. 我被卡住了,觉得这可能是我使用Android.mk的方式。 Looks like it is right but the build is not working on the second module I am testing. 看起来是正确的,但是构建无法在我正在测试的第二个模块上运行。 Here is what I have in the 这是我在

      <project folder>/jni/
                          HelloJni.cpp
                          Android.mk
                          Application.mk
                          mytools/
                                  simplemath.h
                                  simplemath.cpp

I would like to use my java to call a method in HelloJni.cpp which in turn calls a method in simplemath.cpp and return a value. 我想用我的java调用HelloJni.cpp中的方法,后者依次调用simplemath.cpp中的方法并返回一个值。 Simple test to just carry over number added from simplemath. 简单测试,仅保留从单纯数添加的数字。

Below is all my source for each 以下是我每个人的全部资料

---------------------Android.mk ---------------------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 --------------------- 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 --------------------- 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 ----------------- simplemath.cpp

    #include <simplemath.h>

namespace std {

simplemath::simplemath(){
}

int simplemath::add(int a, int b){
    return a + b;
}

simplemath::~simplemath(){
}

}

When I do a clean on the project, I get no errors. 当我对项目进行清理时,没有任何错误。 After a build, I get the following: 构建之后,我得到以下信息:

"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

You haven't declared any dependency between your modules, use LOCAL_SHARED_LIBRARIES to do it: 您尚未在模块之间声明任何依赖关系,请使用LOCAL_SHARED_LIBRARIES进行此操作:

LOCAL_PATH := $(call my-dir)
TOOLS_DIR       := $(LOCAL_PATH)/mytools

include $(CLEAR_VARS)
LOCAL_C_INCLUDES  := $(TOOLS_DIR)
LOCAL_EXPORT_C_INCLUDES  := $(TOOLS_DIR)
LOCAL_MODULE    := simplemath
LOCAL_SRC_FILES := $(TOOLS_DIR)/simplemath.cpp
LOCAL_LDLIBS := -llog 
include $(BUILD_SHARED_LIBRARY)

include $(CLEAR_VARS)
LOCAL_MODULE    := HelloJni
LOCAL_SRC_FILES := HelloJni.cpp
LOCAL_SHARED_LIBRARIES := simplemath
LOCAL_LDLIBS :=  -llog 
include $(PREBUILT_SHARED_LIBRARY)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM