Java:加载具有依赖项的共享库 [英] Java: load shared libraries with dependencies

查看:71
本文介绍了Java:加载具有依赖项的共享库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JNA用Java包装一个共享库(用C编写).共享库是在内部编写的,但是该库使用另一个外部库中的函数,该库又依赖于另一个外部库.所以情况是这样的:

I am wrapping a shared library (written in C) with Java using JNA. The shared library is written internally, but that library uses functions from another external library, which again depends another external library. So the situation is something like this:

ext1<-ext2<-内部

ext1 <- ext2 <- internal

即内部使用外部库ext2,而外部库又使用外部库ext1.我尝试过的是:

I.e. the internal uses external library ext2 which again uses external library ext1. What I have tried is:

System.loadLibrary("ext1");
System.loadLibrary("ext2");
NativeLIbrary.loadLibrary("internal",xxx.class);  

加载库"ext2"时,此方法失败,并显示"UnresolvedException";链接器抱怨库"ext1"中确实存在符号.如此看来,System.loadLibrary()函数不会使"ext1"中的符号全局可用?当使用stdlib函数dlopen()时:

This approach fails with "UnresolvedException" when loading the library "ext2"; the linker complains about symbols which are indeed present in the library "ext1". So it semmes that the System.loadLibrary() function does not make the symbols from "ext1" globally available? When using the stdlib function dlopen() as:

handle = dlopen( lib_name , RTLD_GLOBAL );

在@lib_name中找到的所有符号将在后续加载中用于符号解析;我猜我想为Java品种System.loadLibrary()提供类似的东西吗?

All the symbols found in @lib_name will be available for symbol resolution in subsequent loads; I guess what I would like was something similar for the java variety System.loadLibrary()?

致谢-Joakim Hove

Regards - Joakim Hove

推荐答案

确定;

我终于找到了可以接受的解决方案,但并不是没有很多问题.我要做的是

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

  1. 使用常规JNA机制从动态链接库(libdl.so)映射dlopen()函数.
  2. 使用与JNA映射的dlopen()函数来加载带有选项RTLD_GLOBAL设置的外部库"ext1"和"ext2".

它似乎确实有效:-)

这篇关于Java:加载具有依赖项的共享库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆