I\'m trying to get familiar with the JNI API but can\'t get a sample c++ program to compile. I got the same sample to compile and run in linux (after posting the question in
The issue you ran into can briefly be summed up as a name decoration problem. The linker couldn't find the function with the given name because it's decorated differently in the jvm.dll
.
Looking at the initial error you got:
undefined reference to '_imp__JNI_CreateJavaVM@12'
it's hinting at two things:
@12
suffix at the end indicates that JNI_CreateJavaVM
supposely uses the stdcall convention._imp_
prefix indicates that this function is from an import library that redirects to an externally loaded dll that has this function visible in its export table.The function prototype in jni.h
:
_JNI_IMPORT_OR_EXPORT_
jint JNICALL JNI_CreateJavaVM(JavaVM **, void **, void *);
probably looks like this after preprocessing:
__declspec(dllimport) jint __stdcall
JNI_CreateJavaVM(JavaVM **, void **, void *);
Now the gnu linker that comes with mingw can work with symbols from .a
, msvc's COFF format .lib
and .dll
directly. In your original command, it only found jvm.dll
in the search path provided (-L ...
) so it tried to use that.
The problem is that in jvm.dll
's export table the JNI_CreateJavaVM
function is un-decorated so it looks like a cdecl function. This name doesn't match what the linker expects so you get the undefined reference error.
From looking at the Java Dev Kit, it includes an import library at jdk1.7.0_21\lib\jvm.lib
which has the proper name decoration for this symbol. Your revised command works because by adding -L jdk1.7.0_21\lib
to the search path it's now linking against jvm.lib
and not jvm.dll
.
Got it! after reading the post below I was able to compile and run the sample with the additional link in the compile command in order to link the jvm.lib:
-L"C:\Program Files (x86)\Java\jdk1.7.0_21\lib"
Link: Linking JNI to visual studio 2008
I'm no c/c++ expert so if anyone wants to explain why this additional link is required when it isn't in linux I would be glad to accept your answer.