0

我想在我的 Java 代码中使用 C++ 方法。所以我决定使用JNI。但是由于我在执行时的错误,链接接缝无法正常工作No implementation found for void com.me.Native.helloWorld() (tried Java_com_me_Native_helloWorld and Java_com_me_Native_helloWorld__)

Native.java(在其他地方称为Native.helloWorld()):

package com.me;
public class Native{
    static {
        System.loadLibrary("detection_based_tracker");
    }
    public static native void helloWorld();
}

安卓.mk

...
LOCAL_SRC_FILES  += com_me_Native.cpp
LOCAL_C_INCLUDES += $(LOCAL_PATH)
LOCAL_MODULE     := detection_based_tracker
include $(BUILD_SHARED_LIBRARY)

com_me_Native.h(使用 javah 命令生成):

/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class com_me_Native */

#ifndef _Included_com_me_Native
#define _Included_com_me_Native
#ifdef __cplusplus
extern "C" {
#endif
/*
 * Class:     com_me_Native
 * Method:    helloWorld
 * Signature: ()V
 */
JNIEXPORT void JNICALL Java_com_me_Native_helloWorld
  (JNIEnv *, jclass);

#ifdef __cplusplus
}
#endif
#endif

com_me_Native.cpp

#include <com_me_Native.h>
#include <iostream>
#include <android/log.h>

#define LOG_TAG "HelloWorld"
#define LOGD(...) ((void)__android_log_print(ANDROID_LOG_DEBUG, LOG_TAG, __VA_ARGS__))

#ifdef __cplusplus
extern "C" {
#endif
    using namespace std;
    /*
     * Class:     com_me_Native
     * Method:    helloWorld
     * Signature: ()V
     */
    JNIEXPORT void JNICALL Java_com_me_Native_helloWorld
      (JNIEnv *, jclass)
    {
      LOGD("Hello from c++");
    }

#ifdef __cplusplus
}
#endif

正如你看到的一个用途JNIEXPORTJNICALL我的方法。我也extern "C"用于 C++ 使用。我.h是由javah. 我检查了Android.mk,我没有忘记将我的.cpp文件添加到LOCAL_SRC_FILES. 我将我的库静态加载到 中Native.java以使用我的静态函数。

现在我不知道错误可能来自哪里......知道吗?!

4

1 回答 1

0

包含保护应该只在.h文件中,而不是在.cpp文件中。

所以在你的.cpp文件中,删除这些行:

#ifndef _Included_com_me_Native
#define _Included_com_me_Native

以及决赛#endif

您当前的代码会发生什么是_Included_com_me_Native在您包含头文件时定义的,因此文件#ifndef _Included_com_me_Native中的.cpp将是错误的,并且没有任何代码被编译。

于 2019-02-02T15:23:02.313 回答