加载JNI .DLL,GCC编译的作品,G ++编译失败 [英] Loading JNI .DLL, GCC compiled works, G++ compiled fails

查看:163
本文介绍了加载JNI .DLL,GCC编译的作品,G ++编译失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的JNI .DLL,我试图在测试Java应用程序中使用。它是一个 .c 文件,它由一对函数组成,头文件由 javah 生成。 (我正在使用MinGW btw编译)



如果我使用GCC编译并链接此代码,则可以使用System.loadLibrary()加载.DLL,并使用它。如果我使用G ++编译它, loadLibrary()将会失败并显示可怕的UnsatisfiedLinkError。



这是我的GCC系列:


gcc -Wl, - add -stdcall -alias -IC:\程序文件(x86)\Java\jdk1.7.0_45\include-IC:\程序文件(x86)\Java\jdk1.7.0_45 \include\win32-shared -o TestJNI.dll TestJNI.c


这是我的G ++代码:


g ++ -Wl, - add-stdcall-alias -IC:\程序文件(x86)\Java\jdk1.7.0_45 \include-IC:\ Program Files(x86)\Java\jdk1.7.0_45\include\win32-shared -o TestJNI.dll TestJNI.c


有什么想法?我假设G ++命名函数的方式有所不同,但我不知道是什么...

解决方案

谢谢给Greatwolf的提示:

事实证明我有一个对另一个共享库libgcc_s_dw2-1.dll的引用。我在我的G ++编译中添加了-static标志,并且引用消失了。现在它从Java加载得很好!

以防其他人正在与JNI地狱搏斗;我真的应该更仔细地查看Java异常,因为它实际上提到了这个问题(无法找到依赖库)。我假设这意味着它无法找到/读取我的库,但这实际上是指其他.DLL依赖项。


I have a simple JNI .DLL that I am trying to use in a test Java application. It is a .c file that consists of a couple functions, with the header generated by javah. (I am compiling using MinGW btw)

If I compile and link this code with GCC, I can load the .DLL just fine with System.loadLibrary(), and use it. If I compiled it with G++ however, loadLibrary() will fail with the dreaded "UnsatisfiedLinkError".

This is my GCC line:

gcc -Wl,--add-stdcall-alias -I"C:\Program Files (x86)\Java\jdk1.7.0_45\include" -I"C:\Program Files (x86)\Java\jdk1.7.0_45\include\win32" -shared -o TestJNI.dll TestJNI.c

This is my G++ line:

g++ -Wl,--add-stdcall-alias -I"C:\Program Files (x86)\Java\jdk1.7.0_45\include" -I"C:\Program Files (x86)\Java\jdk1.7.0_45\include\win32" -shared -o TestJNI.dll TestJNI.c

Any thoughts? I am assuming something is different in the way G++ names the functions, but I don't know what...

解决方案

Thanks to Greatwolf's tip:

It turns out I had a reference to another shared library, libgcc_s_dw2-1.dll. I added the "-static" flag to my G++ compile, and the reference went away. Now it loads fine from Java!

And just in case anybody else is wrestling with JNI hell; I really should have looked at the Java exception more closely, because it actually mentioned the issue ( "Can't find dependent libraries" ). I had assumed this meant that it couldn't find/read MY library, but this actually referred to the other .DLL dependency.

这篇关于加载JNI .DLL,GCC编译的作品,G ++编译失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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