从另一个库调用一个库函数
Calling one library function from another library
我有一个库,比如libraryOne.so(File1.c(,它包含纯c代码。现在我想从我的Java文件中访问这些代码。为此,我将使用标准的JNI程序。但对于使用JNI,我还应该修改File1.C中的C代码(比如包括JNI的头文件和一些标准的JNIEXPORT(。因此,我正在创建一个包装器库(libraryWrapper.So由say File2.c制成(。这个库将包含所需的JNI声明,并将与我的Java文件交互现在我想知道如何从File2.C 调用File1.C的纯C函数
首先,您应该阅读ndk教程。因为你需要设置ndk等等。之后你可以寻找ndk样本来学习如何做事。以及如何构建
实际上上面是你的答案来回答你的问题。
你应该有libraryOne.so的源代码,正如你所说的那样。导致android针对许多平台,而你的so无法在这些平台上运行。因此需要使用ndk 进行编译
好的,让我们的文件File1.c
int myfunction(const char* st){
int answer=0;
//stufs here
return answer;
}
首先,添加我们想要访问的Java类
package example.com.myapplication;
public class JniClass {
static {
System.loadLibrary("libraryWrapper");
}
public native int myFunction(String a );
}
现在我们需要包装。为此,我们应该了解jni接口。幸运的是,有一个名为javah的工具,它可以为我们生成它,并使我们免于为所有调用手动执行它。我们应该构建我们的android项目,以便生成类文件。我用debug构建它,所以我生成的文件将位于/app/build/mediater/classes/debug文件夹中。
好的,然后我们运行javahjavah -jni example.com.myapplication.JniClas
s
如果你有错误,在JNI 中使用Javah时会看到错误
它生成了名为example_com_myapplication_JniClass.h
的头文件,其中包含
#include <jni.h>
/* Header for class example_com_myapplication_JniClass */
#ifndef _Included_example_com_myapplication_JniClass
#define _Included_example_com_myapplication_JniClass
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: example_com_myapplication_JniClass
* Method: myFunction
* Signature: (Ljava/lang/String;)I
*/
JNIEXPORT jint JNICALL Java_example_com_myapplication_JniClass_myFunction
(JNIEnv *, jobject, jstring);
#ifdef __cplusplus
}
#endif
#endif
好的,现在你的答案。简单的方法是复制生成的代码并将其粘贴在File1.c代码的底部。并实施它:注意,我在粘贴后将参数命名为env、obj和str,并清除了注释
//..here your File1.c source code content
//
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_example_com_myapplication_JniClass_myFunction
(JNIEnv * env, jobject obj, jstring str){
const char * x=(*env)->GetStringUTFChars(env,str,0);
//call our function
int ret= myfunction(x);
(*env)->ReleaseStringUTFChars(env,str, x);
return ret;
}
#ifdef __cplusplus
}
#endif
但实际上,您可能会在c代码中使用头文件例如,您可以添加生成的头文件并在.c文件上实现它。在.c文件中,您可以包含File1.c头。这样更干净
假设您已经阅读了ndk教程。您设置了ndk工具并知道如何构建
最后,我们将File1.c添加到jni文件夹中,并更改Android.mk文件
LOCAL_MODULE:=库包装LOCAL_SRC_FILES:=文件1.c和
APP_MODULES:=库包装
然后我们用ndk构建它,它将把我们的so文件添加到libs 中
Op只需要File1.c和File2.cFile2.c 内部
//declaration of funtions inside File1.c
extern int myfunction(const char* st);
#include <jni.h>
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT jint JNICALL Java_example_com_myapplication_JniClass_myFunction
(JNIEnv * env, jobject obj, jstring str){
const char * x=(*env)->GetStringUTFChars(env,str,0);
//call our function
int ret= myfunction(x);
(*env)->ReleaseStringUTFChars(env,str, x);
return ret;
}
#ifdef __cplusplus
}
#endif
并更改Android.mk
LOCAL_SRC_FILES := File1.c
LOCAL_SRC_FILES += File2.c
- 在一个模板函数中,若输入的类型是enum类,我该如何使用std::underlying_type
- 如何制作一个将函数作为参数的类方法
- 库函数需要一个 std::function<void(void)>,如何传入类函数?
- 如何将指针从一个void函数传递到另一个C++
- 有没有一个 c++ gmp 库函数与 python gmpy2 库 divm(..) 函数相同?
- 如何封装一个函数,以便它只能由同一类中的一个其他函数调用?
- CMake 将库包含在另一个库中
- 如何使用 swig 修改类构造函数以保留对其中一个构造函数参数的引用?
- 将值传递到另一个类函数在打印时为零
- 为什么从另一个构造函数内部调用C++构造函数不修改类变量?
- 如何处理没有默认构造函数但在另一个构造函数中构造的对象?
- 从另一个动态链接库项目调用静态库函数
- 将 libstdc++ 和其他函数组合到一个库中
- C/C++:一个奇怪的库函数声明
- 有没有一个标准的库函数可以用来防止转义符被识别为转义符
- Java - 我有一个库(.a/.so)和header(.h),如何使用Java调用它们的函数?
- 从另一个库调用一个库函数
- 是否有一个标准库函数来反转STL堆栈?
- 是否有一个标准的库函数,与addressof相反
- 一个库中的函数调用另一个库中的重写delete[]