0

I have a JNI library "lib.so" that has a dependancy on "lib2.so". When I compiled on another platform with lib2.so as static, all was fine, but when compiling dynamically, I get an UnsatisfiedLinkError. In particular, it says "lib.so: undefined symbol: mysymbol_name". I have verified that mysymbol_name is inside of "lib2.so" using the nm tool. I am also loading "lib2.so" to Java before "lib.so".

Java Code:

System.load("/pathto/lib2.so"); // no error
System.load("/pathto/lib.so"); // triggers UnsatisfiedLinkError

C++ compile stage

g++ -c -fPIC {{{.cpp files}}} {{-I,-L directives}}  --std=c++11 -Wl,-soname,lib2.so

C++ link stage

g++ {-L,-I directives} -o lib.so {{.o files}} -l:lib2.so

I am inexperienced with compiling and linking for C/C++, so I'm not sure where the error lies. The C++ compiles and links, so that would mean there is an issue with loading it into Java right?

Update: I have checked ldd lib.so and found that lib2.so is not linked.

user1122069
  • 1,767
  • 1
  • 24
  • 52
  • who uses lib2.so? lib.so? "Thrown if the Java Virtual Machine cannot find an appropriate native-language definition of a method declared native" Are you executing any methods from lib2.so from java? – bichito Feb 13 '17 at 01:56
  • The "lib.so" is the one with the JNI methods. It internally uses mysymbol_name from "lib2.so". I'm guessing at this point that in creating lib.so, something was not linked properly. Also, I don't think Java gets to the point of actually calling anything from lib.so, since it fails right at the load stage and there is no on load callback. – user1122069 Feb 13 '17 at 02:28
  • I finally got it linked. Added -l2 to the end of the link command. Sort of based off this post. http://stackoverflow.com/questions/45135/why-does-the-order-in-which-libraries-are-linked-sometimes-cause-errors-in-gcc – user1122069 Feb 13 '17 at 02:44
  • yes that was my impression. It wasn't coming from the jvm – bichito Feb 13 '17 at 04:18
  • Take a look here: https://github.com/mkowsiak/jnicookbook/tree/master/recipeNo023 - this is the sample you are looking for. There is one lib that call something from another lib. Both are shared. Make sure to put lib2.so on LD_LIBRARY_PATH. Note that inside lib.so you no longer depend on what is loaded by JVM. – Oo.oO Feb 13 '17 at 11:23

0 Answers0