Reputation: 132
I have a problem calling a C function using an other C function from an other .c file from my Java JNI (don't mind some french words in my code, thank you). The code in C works fine.
Here are the two lines I use to compile my libraries. I am compiling my first library with the function that I use in my second library. Tell me if I am doing it wrong to be able to do this :
gcc -fPIC -shared -I"$$JAVA_HOME/include" -I"$$JAVA_HOME/include/linux" -o Libraries/libfilemot.so ../IndexationTexte/fileMot.c
gcc -fPIC -shared -I"$$JAVA_HOME/include" -I"$$JAVA_HOME/include/linux" ../IndexationTexte/indexationV1.c -L./Libraries/ -l:libfilemot.so -o Libraries/libindexation.so
In my Java class, I have this :
public class MoteurDeRecherche {
static {
try {
System.load("/home/aurelien/Documents/Projects/Projet_fil_rouge/noyau_c/JNI/Libraries/libfilemot.so");
System.load("/home/aurelien/Documents/Projects/Projet_fil_rouge/noyau_c/JNI/Libraries/libindexation.so");
}
catch(UnsatisfiedLinkError e) {
System.out.println("Erreur de chargement des librairies :\n" + e);
System.exit(1);
}
}
public static native void indexationTexte();
public static void main(String args[]) {
MoteurDeRecherche.indexationTexte();
}
}
But I get this error coming from my library that compiles successfully :
Erreur de chargement des librairies :
java.lang.UnsatisfiedLinkError: /home/aurelien/Documents/Projects/Projet_fil_rouge/noyau_c/JNI/Libraries/libindexation.so: libfilemot.so: cannot open shared object file: No such file or directory
Don't hesitate if you want more information. Thank you again.
Upvotes: 0
Views: 570
Reputation: 132
So I finally found why, this is not the way to use multiple functions from multiple files. You simply have to put all of the files you need in the gcc command. I don't know why I tried to make a library for one file dependent from an other library for an other file. Here is the command for my project :
gcc -fPIC -shared -I"$$JAVA_HOME/include" -I"$$JAVA_HOME/include/linux" ../IndexationTexte/indexationV1.c ../IndexationTexte/fileMot.c -o Libraries/libindexation.so
For a more generic approach, if you have :
file1.c :
#include "file2.c"
void function1()
{
function2();
}
file2.c :
void function2()
{
printf("hey from an other file");
}
You have to simply compile your project like this :
gcc -fPIC -shared -I"$$JAVA_HOME/include" -I"$$JAVA_HOME/include/linux" file1.C file2.c -o libmylibrary.so
Upvotes: 0
Reputation: 201447
The native linker can't find your shared object. Add the folder with your so to your LD_LIBRARY_PATH
.
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:Libraries/
Alternatively, you can add that to your local libraries folder (usually /usr/local/lib
) and rerun ldconfig
.
Upvotes: 1