java的create vm,未定义的引用“JNI_CreateJavaVM”窗口

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 link below) but can't get it compiled in windows; I'm using mingw g++. I've changed all the include paths to windows paths and the jni.h is being located at compile time but not the jvm.dll.

Here is the commands I've tried using to compile:

g++ -g -I"C:\Program Files (x86)\Java\jdk1.7.0_21\include" -I"C:\Program Files (x86)\Java\jdk1.7.0_21\include\win32" -L"C:\Program Files (x86)\Java\jdk1.7.0_21\jre\bin\server" callJava.cpp -ljvm

and...

**same as above with the additional** : -L"C:\Program Files (x86)\Java\jdk1.7.0_21\lib"

The error I get is:

undefined reference to `_imp__JNI_CreateJavaVM@12'

and the cpp being compiled:

#include

int main(){

//firstTest();

JavaVM *jvm;

JNIEnv *env;

JavaVMInitArgs vm_args;

JavaVMOption options[1];

options[0].optionString = "-Djava.class.path=C:/Users/Ron/Dropbox/jni/simple/ctojava/win";

vm_args.version = JNI_VERSION_1_6;

vm_args.options = options;

vm_args.nOptions = 1;

vm_args.ignoreUnrecognized = JNI_FALSE;

int res = JNI_CreateJavaVM(&jvm, (void **)&env, &vm_args);

jclass cls = env->FindClass("Hello");

jmethodID mid = env->GetStaticMethodID(cls, "staticInt", "(I)I");

env->CallStaticVoidMethod(cls, mid,10);

jvm->DestroyJavaVM();

}

I've looked at many examples but still can't find a solution. Any help is appreciated!

UPDATE: I am pretty sure the the jvm.dll is being located because if I remove the -L"path_to_jvm" then I get the error:

mingw32/bin/ld.exe: cannot find -ljvm

Like I said, this exact approach works in linux, What else am I missing for windows?

解决方案

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:

The @12 suffix at the end indicates that JNI_CreateJavaVM supposely uses the stdcall convention.

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值