简体   繁体   English

如何将共享库正确加载到 Eclipse 中

[英]How to correctly load shared libraries into Eclipse

I'm using JNI on linux and I'm having troubles to retrieve shared libraries.我在 linux 上使用 JNI,但在检索共享库时遇到了麻烦。

In particular, I use native functions contained in a single SO file, but it has dependencies with other SO files (which I put in the same directory).特别是,我使用包含在单个 SO 文件中的本机函数,但它与其他 SO 文件(我放在同一目录中)具有依赖性。

Now, I'm using System.load(absolutePath) to load the main SO, but I get this error:现在,我正在使用System.load(absolutePath)加载主 SO,但出现此错误:

...GMApiJNI64.so: libgpc64.so: cannot open shared object file: No such file or directory

where GMApiJNI64.so is the main library I'm using其中GMApiJNI64.so是我使用的主库

Until now I tried to:直到现在我试图:

What else can I do?我还可以做些什么?

Just a short recap.只是一个简短的回顾。 I don't know your exact env.我不知道你的确切环境。 but I can reproduce and fix the issue similar to yours:但我可以重现并修复与您类似的问题:

> git clone https://github.com/mkowsiak/jnicookbook.git
> cd jnicookbook/recipes/recipeNo023
> make
> make test
/usr/lib64/jvm/java/bin/java -Djava.library.path=:./lib -cp target recipeNo023.HelloWorld
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/test/workspace/jnicookbook/recipes/recipeNo023/lib/libHelloWorld.so: libAnotherFunction.so: cannot open shared object file: No such file or directory
    at java.lang.ClassLoader$NativeLibrary.load(Native Method)
    at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1941)
    at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1857)
    at java.lang.Runtime.loadLibrary0(Runtime.java:870)
    at java.lang.System.loadLibrary(System.java:1122)
    at recipeNo023.HelloWorld.<clinit>(HelloWorld.java:11)
Makefile:14: recipe for target 'test' failed
make: *** [test] Error 1

Now, let's see what happens with the lib现在,让我们看看 lib 会发生什么

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> ldd lib/libHelloWorld.so 
    linux-vdso.so.1 (0x00007ffd34936000)
    libAnotherFunction.so => not found
    libc.so.6 => /lib64/libc.so.6 (0x00007f470c182000)
    /lib64/ld-linux-x86-64.so.2 (0x0000556276681000)

It's not there.它不在那里。 What we can do is to add it on the LD_LIBRARY_PATH我们可以做的是将它添加到 LD_LIBRARY_PATH

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:`pwd`/lib

and try again.然后再试一次。 Works.作品。

test@linux-875l:~/workspace/jnicookbook/recipes/recipeNo023> make test/usr/lib64/jvm/java/bin/java -Djava.library.path=:/home/test/workspace/jnicookbook/recipes/recipeNo023/lib:./lib -cp target recipeNo023.HelloWorld
library: :/home/test/workspace/jnicookbook/recipes/recipeNo023/lib:./lib
Hello world!
Hello from another function!

What you can do - in Eclipse - is to provide location of your libs inside project settings:您可以做的 - 在 Eclipse 中 - 是在项目设置中提供您的库的位置:

Project -> Properties -> Java Build Path 
    -> Libraries -> JRE System Library 
    -> Native library location -> Edit... 
    -> External folder

Update:更新:

There still might be an issue if you don't have libgpc64.so on LD_LIBRARY_PATH.如果您在 LD_LIBRARY_PATH 上没有 libgpc64.so,仍然可能存在问题。

There is one more thing you can try.您还可以尝试一件事。

While building GMAapiJNI64.so, try to use following:在构建 GMAapiJNI64.so 时,尝试使用以下内容:

-Wl,-rpath=$LOCATION_OF_LIBGPC -lgpc64

This time, Eclipse should be able to properly start your code even though you don't have your lib in LD_LIBRARY_PATH这一次,即使您在 LD_LIBRARY_PATH 中没有您的库,Eclipse 也应该能够正确启动您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM