jujumbura
jujumbura

Reputation: 423

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

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

Upvotes: 0

Views: 906

Answers (2)

jujumbura
jujumbura

Reputation: 423

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.

Upvotes: 1

Alex Cohn
Alex Cohn

Reputation: 57203

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

Upvotes: 1

Related Questions