Jni 和共享库
Jni and shared libraries
我在 Java 中编写了一个程序,该程序调用了本地语言 C 中的某些函数。制作了该 C 函数文件的共享库并制作了一个共享库,并且一切正常。
我的问题是当我尝试调用其他函数时,例如在 PBC(基于配对的密码术)库中。共享库中的 C 文件包含了解 PBC 中函数所需的 .h 文件,但我无法使用它们,我不知道为什么。我应该怎么办?如何调用其他库中的函数?
Java 加载库的代码。
static {
System.loadLibrary("myLibrary");
System.loadLibrary("pbc");
}
执行我自己的 Java 程序时出错:
undefined symbol: pairing_init_set_buf
确保 link 您的 JNI 代码与您要使用的共享库。
您可以在此处查看示例代码:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023
在此示例中,您具有 JNI 函数:
JNIEXPORT void JNICALL Java_recipeNo023_HelloWorld_displayMessage
(JNIEnv *env, jclass obj) {
printf("Hello world!\n");
/* We are calling function from another source */
anotherFunction();
}
从一些外部共享库调用函数
void anotherFunction() {
// we are printing message from another C file
printf("Hello from another function!\n");
}
您必须确保您的 JNI 库 link 与您要使用的库一起编辑:
cc -g -shared -fpic -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(ARCH) c/recipeNo023_HelloWorld.c -L./lib -lAnotherFunction -o lib/libHelloWorld.$(EXT)
在这个示例中
-L./lib -lAnotherFunction
告诉编译器使用这个 "other" 库,它包含在包含 JNI 代码的库中不可用的符号。
我在 Java 中编写了一个程序,该程序调用了本地语言 C 中的某些函数。制作了该 C 函数文件的共享库并制作了一个共享库,并且一切正常。
我的问题是当我尝试调用其他函数时,例如在 PBC(基于配对的密码术)库中。共享库中的 C 文件包含了解 PBC 中函数所需的 .h 文件,但我无法使用它们,我不知道为什么。我应该怎么办?如何调用其他库中的函数?
Java 加载库的代码。
static {
System.loadLibrary("myLibrary");
System.loadLibrary("pbc");
}
执行我自己的 Java 程序时出错:
undefined symbol: pairing_init_set_buf
确保 link 您的 JNI 代码与您要使用的共享库。
您可以在此处查看示例代码:
https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023
在此示例中,您具有 JNI 函数:
JNIEXPORT void JNICALL Java_recipeNo023_HelloWorld_displayMessage
(JNIEnv *env, jclass obj) {
printf("Hello world!\n");
/* We are calling function from another source */
anotherFunction();
}
从一些外部共享库调用函数
void anotherFunction() {
// we are printing message from another C file
printf("Hello from another function!\n");
}
您必须确保您的 JNI 库 link 与您要使用的库一起编辑:
cc -g -shared -fpic -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(ARCH) c/recipeNo023_HelloWorld.c -L./lib -lAnotherFunction -o lib/libHelloWorld.$(EXT)
在这个示例中
-L./lib -lAnotherFunction
告诉编译器使用这个 "other" 库,它包含在包含 JNI 代码的库中不可用的符号。