0

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

greatwolf
  • 20,287
  • 13
  • 71
  • 105
jujumbura
  • 425
  • 3
  • 9
  • 1
    Can you check the exported function names in the dll and see if there's any difference between the two? You can do this with depwalker or objdump or dumpbin etc. – greatwolf Dec 10 '13 at 03:08
  • THANK YOU greatwolf! I downloaded depwalker, and it immediately told me that I was missing a referenced .DLL ( libgcc_s_dw2-1.dll ). That was what I needed to know! – jujumbura Dec 10 '13 at 15:26

2 Answers2

1

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

Alex Cohn
  • 56,089
  • 9
  • 113
  • 307
  • I assume you are referring to adding extern "C" in the function implementations ( the .C file )? I had thought this was the issue too, but adding it seems to have no effect on my problem. – jujumbura Dec 10 '13 at 04:53
1

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.

jujumbura
  • 425
  • 3
  • 9
  • 1
    Note you can also use `-static-libgcc` to force it to link against the static version. Additionally, if your library uses C++ code using the standard library you can also force a static link to that using `-static-libstdc++`. – greatwolf Dec 11 '13 at 00:04