简体   繁体   English

在Android NDK中的C ++类中调用SO函数

[英]Call a SO function in C++ class in Android NDK

How to call a function from prebuild .so (C class) file into C++ class in new project. 如何在新项目中将预构建.so(C类)文件中的函数调用为C ++类。 For example i have a project-1 which create a prebuild .so file. 例如我有一个project-1,它创建一个prebuild .so文件。 Now in this project i have a C++ file named as "androidNdk". 现在在这个项目中,我有一个名为“ androidNdk”的C ++文件。 In this class i have only one function that return a integer value. 在此类中,我只有一个函数返回整数值。

int myFunction()
{
    int number = 10;
    return number;
}

Header file of this class 此类的头文件

int myFunction(); int myFunction();

I create a new project and load this .so file and call a myfunction() in C++ class. 我创建一个新项目并加载此.so文件,然后在C ++类中调用myfunction()。 like this This is new class named as "newAndroidNdk". 这样的新类名为“ newAndroidNdk”。

void newFunction()
{
    int str = myFunction();

    printf("%s", str);  
}

The problem is when i compile this project using ndk-build command it will give me this error "undefined reference to myfunction". 问题是,当我使用ndk-build命令编译该项目时,它将给我这个错误“对myfunction的未定义引用”。

This is Android.mk in jni -> lib folder 这是jni-> lib文件夹中的Android.mk

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_MODULE := libndkfunction-prebuilt
LOCAL_SRC_FILES := libndkfunction.so
LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/include
include $(PREBUILT_SHARED_LIBRARY)

This is new project Android.mk file 这是新项目Android.mk文件

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

# Here we give our module name and source file(s)
LOCAL_MODULE    := ndkfun
LOCAL_SRC_FILES := ndkfun.c

LOCAL_SHARED_LIBRARIES := libndkfunction-prebuilt        
 LOCAL_EXPORT_C_INCLUDES := $(LOCAL_PATH)/jni/include                
include $(BUILD_SHARED_LIBRARY)

help me to solve this problem 帮我解决这个问题

You cannot call native code directly from java, you need use JNI. 您不能直接从Java调用本机代码,需要使用JNI。 You can read more about it on official java site or android developers site. 您可以在Java官方网站或android开发人员网站上阅读有关此内容的更多信息。 You should use javah to generate native function wrapper for java code, it's more conveniently. 您应该使用javah为Java代码生成本机函数包装器,这更加方便。 Or manually declare static function and write wrapper for its C/C++ declaration Little tutorial 或手动声明静态函数并为其C / C ++声明编写包装器小教程

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

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