【问题标题】:Android NDK / JNI - undefined reference to function defined by using prebuild libraryAndroid NDK / JNI - 对使用预构建库定义的函数的未定义引用
【发布时间】:2016-07-18 16:11:41
【问题描述】:

我尝试在 JNI 中使用预构建共享库。 但是当我构建 JNI 库时,它总是抱怨函数未定义的引用。

这是我的 Android.mk 文件。

LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE := mtestlib
LOCAL_SRC_FILES := mtest/arm64-v8a/lib/libtest.so
LOCAL_EXPORT_C_INCLUDES := mtest/arm64-v8a/include
LOCAL_EXPORT_LDLIBS := mtest/arm64-v8a/lib/libtest.so
LOCAL_PRELINK_MODULE := true
include $(PREBUILT_SHARED_LIBRARY)


include $(CLEAR_VARS)
LOCAL_MODULE := myJNI
LOCAL_SRC_FILES := myJNI.cpp
LOCAL_C_INCLUDES := $(LOCAL_PATH)/mtest/arm64-v8a/include
LOCAL_SHARED_LIBRARY := mtestlib
include $(BUILD_SHARED_LIBRARY)

这是我的 JNI 文件。

#include "lucien_example_com_jtest_myNDK.h"
#include <android/log.h>
#include <iostream>
#include <iostream>
#include "Mtest.h"

JNIEXPORT jint JNICALL Java_lucien_example_com_jtest_myNDK_test
    (JNIEnv *env, jobject, jstring tmp){
    const char* str;
    str = env->GetStringUTFChars(tmp,false);
    std::cout<< str << std::endl;
    env->ReleaseStringUTFChars(tmp,str);
    mtest(str);
}

Mtest.h 头文件:

#include <stdio.h>
#include <stdlib.h>
#include <cutils/properties.h>
#include <utils/Log.h>
int mtest(const char *str);

mtest 源文件:

#include "Mtest.h"
#define LOG_TAG "Test"
int mtest(const char *str){
    ALOGE("[Lucien] test(%s)", str);
    return 0;
}

我不知道为什么当 JNI 加载 mtest() 函数时我会抱怨 undefined reference。 头文件和预编译库已经包含在内。 哪个错误的步骤导致了这个错误? 请帮助我,谢谢。

【问题讨论】:

    标签: android android-ndk java-native-interface


    【解决方案1】:

    .h文件和源文件中你的函数名不一样:

    int mtest(const char *str);
    
    int mjpeg(const char *str){
        ALOGE("[Lucien] test(%s)", str);
        return 0;
    }
    

    尝试在源文件中调用 mtest:

    int mtest(const char *str){
        ALOGE("[Lucien] test(%s)", str);
        return 0;
    }
    

    为了匹配你的 jni 调用:

    JNIEXPORT jint JNICALL Java_lucien_example_com_jtest_myNDK_test
        (JNIEnv *env, jobject, jstring tmp){
        ...
        mtest(str);
    }
    

    编辑 1

    你的 jni 函数期望返回一个 jint,但你没有返回任何东西。尝试在函数末尾使用 return 语句:

    JNIEXPORT jint JNICALL Java_lucien_example_com_jtest_myNDK_test
        (JNIEnv *env, jobject, jstring tmp){
        const char* str;
        str = env->GetStringUTFChars(tmp,false);
        std::cout<< str << std::endl;
        env->ReleaseStringUTFChars(tmp,str);
        mtest(str);
    
        //Example:
        return 0;
    
    }
    

    【讨论】:

    • 对不起,我粘贴了错误的源文件。在我的源文件中,header、source、JNI调用的函数是匹配的。
    猜你喜欢
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2012-07-26
    • 2018-02-24
    • 2018-10-22
    • 1970-01-01
    相关资源
    最近更新 更多