4

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
Michael
  • 57,169
  • 9
  • 80
  • 125
Tortxu13
  • 107
  • 2
  • 9
  • Are you linking somehow (statically or dynamically) PBC libs? – LPs Nov 09 '16 at 07:40
  • In the C program I have only included them, I have to make something else? – Tortxu13 Nov 09 '16 at 07:50
  • It depends: you can link the lib statically to your shared lib, or you can add shared PBC libs to the linking command of your final application. – LPs Nov 09 '16 at 07:52
  • How do I link them to the shared PBC lib to the final application? And how can I link the that shared lib statically to my own shared lib? – Tortxu13 Nov 09 '16 at 07:58
  • How did you link your native C lib to your application? Same way for PBC libs... – LPs Nov 09 '16 at 08:00
  • Used System.loadLibrary("libname"); to link the shared library but if I also put the other shared library there if doesn't work. Should I use dlopen or sthing like that? Sorry, im quite new :S – Tortxu13 Nov 09 '16 at 08:16
  • I'm not a Java expert. What is the error? You should post the code that load libs and the errors. BTW try to take a look at [this SO question](http://stackoverflow.com/questions/5425034/java-load-shared-librariees-with-dependencies) – LPs Nov 09 '16 at 08:24
  • Ok, After the edit, surely you must switch load library instructions: first load the `PBC` and after `myLibrary`. In other words: first the when you load a lib that depends on some other libs, those libs must be loaded before. – LPs Nov 09 '16 at 08:36
  • 1
    Switched them and still same error. Im going to try using dlOpen :s – Tortxu13 Nov 09 '16 at 08:54

1 Answers1

4

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.

Oo.oO
  • 12,464
  • 3
  • 23
  • 45