glibc java,从Java调用C ++,但是Java加载了错误的Glibc版本

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)

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 :(.

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);

}

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

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.

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

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

(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, 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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值