Skip to content Skip to sidebar Skip to footer

Loading A Shared Library With The System.loadLibrary Call Never Returns

I have a problem loading a native library using System.loadLibrary('my_shared_lib'); The problem is that this call never returns. Here is the context : In my project I have several

Solution 1:

I've found out what was wrong in my code.

Actually when the loadLibrary loads a .so library, it creates all the global variables/constants declared in the .so, including of course every global of every .a lib contained in the .so.

Some of these global are built through constructors executing some code. I've found out that in my .a libs architecture, this code was deadlocking at some point because it was called too early (some needed stuff didn't exist yet). I didn't expect it to be called at the loadLibrary time.

So if that can help anyone: keep in mind that the loadLibrary involves creating all the global objects contained in the .so lib that you're trying to load.

Not knowing that was my mistake.


Solution 2:

In general, when you build a shared library (.so), some kind of compiler options such as -fPIC are used to generate position-independent code. On the other hand, when you build a static library (.a), such an option is not used. Even if you pack static libraries into a shared library, binary code in the static libraries are not position-independent. It is the reason why loadLibrary() fails, I guess.

If I were you, I would avoid packing static libraries into a shared library and instead would compile all C++ files with -fPIC or something equivalent.


Post a Comment for "Loading A Shared Library With The System.loadLibrary Call Never Returns"