从Java调用C ++,但是Java加载了错误的Glibc版本 [英] Calling C++ from Java, but Java loads the wrong Glibc version

查看:232
本文介绍了从Java调用C ++,但是Java加载了错误的Glibc版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从Java端调用C ++库,并编写了必要的jni代码.但是,当我的Java代码尝试通过System.loadLibrary加载我的C ++库时,它会报告以下错误:

I am trying to call a C++ library from java side and have written necessary jni code. However, when my java code tries to load my C++ library via System.loadLibrary, it complains the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError: libmylib.so: /usr/lib64/libstdc++.so.6: version `GLIBCXX_3.4.15' not found (required by libmylib.so)

后来我发现这是因为我的系统上有两个不同的GLIBC版本,默认版本是较旧的版本,而libmylib.so是从较新的GLIBC生成的.结果,java链接到错误的GLIBC.

I later found that it is because I have two different GLIBC versions on my system, and the default one is the older one, while libmylib.so is required to built from the newer GLIBC. As a result, java links to the wrong GLIBC.

我尝试了以下操作,但不起作用:(.

I tried the following things, but it does not work :(.

第一个是尝试通过我的Java代码中的System.load手动加载正确的库.具体来说,我在加载库之前添加了以下代码:

The first one is to try to load correct library manually via System.load in my java code. Specifically, I added the following codes before it loads my library:

static {
  System.load("/usr/local/gcc-4.8.1-glibc-2.17/lib/libc.so.6");
  System.load("/usr/local/gcc-4.8.1-glibc-2.17/lib/libstdc++.so.6.0.18");      
  System.loadLibrary(mylib);
}

还添加了libc.so.6,因为libstdc ++依赖它(否则Java将加载错误的libc并报告另一个错误).但是,这次Java抱怨了以下错误:

The libc.so.6 is also added because libstdc++ depends on it (otherwise java will load the wrong libc and complains another error). However, this time java complains the following error:

Exception in thread "main" java.lang.UnsatisfiedLinkError:
  /usr/local/gcc-4.8.1-glibc2.17/lib/libc-2.17.so:
      __vdso_time: invalid mode for dlopen(): Invalid argument

并且此错误是由混合两个版本的GLIBC引起的,如此问题. 该问题的解决方案是使用正确设置的-Wl,--dynamic-linker构建C ++程序(另见

And this error is caused by mixing two versions of GLIBC as described in this question. The solution to that question is for building C++ program with -Wl,--dynamic-linker set properly (also described here). However, I don't know how to do that in Java.

我试图将LD_LIBRARY_PATH设置为较新的版本,但是情况是相同的:(.

I tried to set LD_LIBRARY_PATH to the newer version, but the situation is just the same :(.

我可以知道如何使Java链接到正确的库吗?

Can I know how to make java link to the correct library?

(PS:首选没有make install glibc较新版本的解决方案,因为我的机器上的许多其他应用程序都依赖于当前的默认glibc)

(PS: a solution without make install newer version of glibc is preferred, since many other applications in my machine rely on the current default glibc)

几天来我一直在寻找并尝试解决问题的方法,但没有一个成功:(

I have been searching and trying solutions to my problem for days, but none of them succeed :(

Stackoverflow,您是我唯一的希望:〜.

Stackoverflow, you're my only hope :~.

推荐答案

System.load("/usr/local/gcc-4.8.1-glibc-2.17/lib/libc.so.6");

正如我在此处所述,这可能无法正常工作.

As I explained here, this can not possibly work.

当您到达上述语句时,系统 libc.so.6已经加载(在进程启动时),并且您绝对不能拥有两个版本的libc.so.6加载到同一进程中.

By the time you reach above statement, the system libc.so.6 has already been loaded (at process startup), and you absolutely can not have two versions of libc.so.6 loaded into the same process.

这篇关于从Java调用C ++,但是Java加载了错误的Glibc版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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