Tortxu13
Tortxu13

Reputation: 117

Jni and shared libraries

I have made a program in Java that calls to some functions in native language C. Made a shared library of that C function file and made a shared library, and all worked perfectly.

My problem is when I try to call other functions for example in PBC (Pairing Based Cryptography) library. The C files that are in the shared library include the required .h files for knowing the functions in PBC but I can't use them, I don't know why. What should I do? How can I call functions that are in another libraries?

Java code for loading the libraries.

static {

    System.loadLibrary("myLibrary");
    System.loadLibrary("pbc");
}

Error when executing my own Java program:

undefined symbol: pairing_init_set_buf

Upvotes: 4

Views: 3688

Answers (1)

Oo.oO
Oo.oO

Reputation: 13375

Make sure to link your JNI code with shared library you want to use.

You can take a look at sample code here:

https://github.com/mkowsiak/jnicookbook/tree/master/recipes/recipeNo023

In this sample you have JNI function:

JNIEXPORT void JNICALL Java_recipeNo023_HelloWorld_displayMessage
  (JNIEnv *env, jclass obj) {

    printf("Hello world!\n");
    /* We are calling function from another source */

    anotherFunction();
}

that calls function from some external shared library

void anotherFunction() {
    // we are printing message from another C file
    printf("Hello from another function!\n");
}

You have to make sure that your JNI library is linked with the library you want to use:

cc -g -shared -fpic -I${JAVA_HOME}/include -I${JAVA_HOME}/include/$(ARCH) c/recipeNo023_HelloWorld.c -L./lib -lAnotherFunction -o lib/libHelloWorld.$(EXT)

In this sample

-L./lib -lAnotherFunction

tells compiler to use this "other" library that contains symbols not available inside library that contains JNI code.

Upvotes: 4

Related Questions