Loading JNI .DLL, GCC compiled works, G++ compiled fails

北城余情 提交于 2019-12-12 01:05:48

问题


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


回答1:


All JNI exported functions need extern "C" when compiled as C++.




回答2:


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.



来源:https://stackoverflow.com/questions/20484810/loading-jni-dll-gcc-compiled-works-g-compiled-fails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!