Android NDK/JNI-对自定义头文件中定义的函数的未定义引用 [英] Android NDK / JNI - undefined reference to function defined in custom header file

查看:65
本文介绍了Android NDK/JNI-对自定义头文件中定义的函数的未定义引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JNI,我试图为Android NDK编写本机C ++方法,该方法调用自定义头文件中定义的C函数.但是,我的C函数调用收到未定义的引用错误.

Using JNI, I am trying to write a native C++ method for the Android NDK that makes a call to a C function defined in a custom header file. However, I am getting an undefined reference error for my C function call.

这是我的C ++代码,它调用C函数并将其结果作为jstring返回给Java:

Here is my C++ code that makes a call to the C function and returns its result to Java as a jstring:

#include <jni.h>

#include "gesture_detector.h"

JNIEXPORT jstring JNICALL Java_com_example_bmtitest_JavaAbstractionLayer_callGestureAnalysis(JNIEnv *env, jobject obj, jfloat previousX, jfloat previousY, jfloat currentX, jfloat currentY) {
    return env->NewStringUTF(gestureAnalysis(previousX, previousY, currentX, currentY));
}

这是我的C函数:

#include <stdio.h>

#include "gesture_detector.h"

//implemented from gesture_detector.h
const char* gestureAnalysis(float previousX, float previousY, float currentX, float currentY)
{
    float xOffset = currentX - previousX;
    float yOffset = currentY - previousY;

    if(xOffset == 0 && yOffset == 0)
    {
        return "TAP";
    }
    return "0";
}

这是我的Android.mk代码:

Here is my Android.mk code:

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE    := gestureDetector
LOCAL_SRC_FILES := gestureDetector.c NativeAbstractionLayer.cpp
LOCAL_LDLIBS    := -landroid

include $(BUILD_SHARED_LIBRARY)

显然,似乎找不到自定义头文件( gesture_detector.h )中定义的函数定义.我认为这可能是我的Android.mk文件中的问题.

Apparently, it seems the function definition defined in the custom header file (gesture_detector.h) isn't being found. I think it may be a problem in my Android.mk file.

有人可以告诉我我在做什么错吗?

Could anyone tell me what am I doing wrong here?

推荐答案

链接器出现未定义引用"错误.您的头文件仅满足编译器的要求.

An "undefined reference" error comes from the linker. Your header file only satisfied the compiler.

但是,由于您混合使用C和C ++,因此您的问题很可能是名称修改.基本上,您需要告诉C ++编译器您要调用的函数是由C编译器而不是C ++编译器创建的,因此其名称上没有嫁接参数类型代码.现在它还不知道,所以试图用C ++样式修饰的名称来调用该函数,该名称不同于链接器实际可用的普通C名称.

However, since you are mixing C and C++ your problem is likely name mangling. Basically, you need to tell the C++ compiler that the function you are trying to call was created by a C compiler rather than a C++ one, and so does not have argument type codes grafted onto its name. Right now it doesn't know that, so is trying to call the function by a C++ style decorated name which differs from the plain C name of the function the linker actually has available.

将此添加到您的gesture_detector.h文件的开头:

Add this at the beginning of your gesture_detector.h file:

#ifdef __cplusplus
extern "C" {
#endif

最后是这个

#ifdef __cplusplus
}
#endif

进行干净的重建.

如果您真正的jni胶合逻辑与此处介绍的版本一样琐碎,则也可以选择将其切换为C版本-但请注意,jni语法在C和C ++中是不同的,因此您不能只是更改文件扩展名.

If your real jni glue logic is as trivial as the version presented here, switching to a C version of that could also be an option - but beware that jni syntax is different in C and C++, so you can't just change the file extension.

这篇关于Android NDK/JNI-对自定义头文件中定义的函数的未定义引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆