How to Use Linux Shared Libraries in Java

How can I use Linux shared libraries in Java?

The answer is "JNI" :)

Here are a couple of links:

  • How to compile dynamic library for a JNI application on linux?

  • http://learn-from-the-guru.blogspot.com/2007/12/java-native-interface-jni-tutorial-hell.html

  • http://docs.oracle.com/javase/6/docs/technotes/guides/jni/

How to link shared library to another shared library

Let's decode your symbol:

$ c++filt _ZN5vatps6PosAPI8sendDataB5cxx11Ev
vatps::PosAPI::sendData[abi:cxx11]()

So, your code expects sendData with std::string with C++11 ABI, whereas libPosAPI.so provides sendData with pre-C++11 ABI std::string.

abi:cxx11 hints to GCC5 and the C++11 ABI:

Users that depend on third-party libraries or plugin interfaces that still use the old ABI can build their code with -D_GLIBCXX_USE_CXX11_ABI=0 and everything should work fine. In most cases, it will be obvious when this flag is needed because of errors from the linker complaining about unresolved symbols involving __cxx11.

Java: load shared libraries with dependencies

OK;

I have found an acceptable solution in the end, but not without significant amount of hoops. What I do is

  1. Use the normal JNA mechanism to map the dlopen() function from the dynamic linking library (libdl.so).
  2. Use the dlopen() function mapped in with JNA to load external libraries "ext1" and "ext2" with the option RTLD_GLOBAL set.

It actually seems to work :-)

Shared library on Linux does not contain reference to one of its dependencies

Your version of cc (or the link editor used by it) seem to default to -Wl,--as-neeeded. In this case, the command line order matters. If -lcpdf comes first, there are no references to its symbols yet, and so no dependency is created. -l arguments should come last:

cc -shared -fpic -I$JAVA_HOME/include I$JAVA_HOME/include/linux jcpdfwrapper.c -o libjcpdf.so -L. -lcpdf

Java program that access C++ shared library that uses OpenCV

According to the Makefiles in your gist, the problem seems to be that the pkg-config --libs --cflags opencv, which adds a bunch of -l library arguments to the linker, is mentioned before the objects that actually depend on those libraries. In your Makefile, the easiest fix would be to move your $(INCLUDES) reference to the end of the commands, after $(LIBS).



Related Topics



Leave a reply



Submit