so I'm trying to use JNI to call my Java class in C++ and everything looks well until I try to run it. In Xcode I get the error
所以我正在尝试使用JNI在C ++中调用我的Java类,一切顺利,直到我尝试运行它。在Xcode中我得到错误
Undefined symbols for architecture x86_64: "_JNI_CreateJavaVM", referenced from:
Which I assume has something to do with the architecture, but don't know how to fix this, any help?
我认为这与架构有关,但不知道如何解决这个问题,有什么帮助吗?
The code I use to initialize the Java VM is
我用来初始化Java VM的代码是
JNI_CreateJavaVM(&internal::gJVM,
(void**)&internal::gEnv, &vm_args);
I'm on a mac, please post a mac solution if you have any ideas, I'm trying to avoid loading libraries at runtime. Thanks
我在Mac上,如果您有任何想法,请发布mac解决方案,我试图避免在运行时加载库。谢谢
2 个解决方案
#1
0
It sounds like you're missing a library. If you're using gcc check all your -l
's. If you're using VS, check your "Additional Dependancies" options under Config->Linker->Input.
听起来你错过了一个图书馆。如果你正在使用gcc检查你所有的-l's。如果您正在使用VS,请检查Config-> Linker-> Input下的“Additional Dependancies”选项。
Also check that you have the x86_64 version of the necessary libraries.
还要检查是否有必要库的x86_64版本。
#2
0
In all the examples I have seen, they avoid the problem by never invoking that function directly, thus it never has to link, thus no error. The trick is to lookup the function at runtime and invoke via function pointer. Here is how to do it on Windows. Don't know the syntax for doing it on other operating systems.
在我看到的所有示例中,他们通过永远不直接调用该函数来避免问题,因此它永远不必链接,因此没有错误。诀窍是在运行时查找函数并通过函数指针调用。以下是如何在Windows上执行此操作。不知道在其他操作系统上执行此操作的语法。
[Disclaimer - I copied/pasted code from several functions and may have introduced compiler errors. This may or may not compile but it should get you started]
[免责声明 - 我从多个函数复制/粘贴代码,可能引入了编译器错误。这可能会也可能不会编译,但它应该让你开始]
First create your own typedef for a pointer to the function
首先为指向函数的指针创建自己的typedef
typedef jint (JNICALL* JvmCreateProcTypeDef)(JavaVM **, void **, void);
Look up the JVM dll using LoadLibrary. It's up to your application to figure out where to find the JVM DLL. In our case we distributed a 3rd party JRE and knew where to expect the DLL.
使用LoadLibrary查找JVM dll。由您的应用程序决定在哪里可以找到JVM DLL。在我们的例子中,我们分发了第三方JRE并知道DLL的位置。
HMODULE jvmDll = LoadLibrary(jvmDllPath);
Next lookup the address of the function from the JVM dll using GetProcAddress
接下来使用GetProcAddress从JVM dll查找函数的地址
JvmCreateProcTypeDef jvmCreateProc = (JvmCreateProcTypeDef) GetProcAddress(jvmDll,"JNI_CreateJavaVM");
Now replace your code which calls the function directly, to something like the following which calls it via function pointer:
现在替换直接调用函数的代码,类似于以下通过函数指针调用它的代码:
jvmCreateProc(&internal::gJVM, (void**)&internal::gEnv, &vm_args);
This should get you passed all the compile link errors. Now all you have to do is deal with the runtime errors when your code cannot find the DLL :)
这应该让你传递所有的编译链接错误。现在你所要做的就是当你的代码找不到DLL时处理运行时错误:)
Hope this helps!
希望这可以帮助!
#1
0
It sounds like you're missing a library. If you're using gcc check all your -l
's. If you're using VS, check your "Additional Dependancies" options under Config->Linker->Input.
听起来你错过了一个图书馆。如果你正在使用gcc检查你所有的-l's。如果您正在使用VS,请检查Config-> Linker-> Input下的“Additional Dependancies”选项。
Also check that you have the x86_64 version of the necessary libraries.
还要检查是否有必要库的x86_64版本。
#2
0
In all the examples I have seen, they avoid the problem by never invoking that function directly, thus it never has to link, thus no error. The trick is to lookup the function at runtime and invoke via function pointer. Here is how to do it on Windows. Don't know the syntax for doing it on other operating systems.
在我看到的所有示例中,他们通过永远不直接调用该函数来避免问题,因此它永远不必链接,因此没有错误。诀窍是在运行时查找函数并通过函数指针调用。以下是如何在Windows上执行此操作。不知道在其他操作系统上执行此操作的语法。
[Disclaimer - I copied/pasted code from several functions and may have introduced compiler errors. This may or may not compile but it should get you started]
[免责声明 - 我从多个函数复制/粘贴代码,可能引入了编译器错误。这可能会也可能不会编译,但它应该让你开始]
First create your own typedef for a pointer to the function
首先为指向函数的指针创建自己的typedef
typedef jint (JNICALL* JvmCreateProcTypeDef)(JavaVM **, void **, void);
Look up the JVM dll using LoadLibrary. It's up to your application to figure out where to find the JVM DLL. In our case we distributed a 3rd party JRE and knew where to expect the DLL.
使用LoadLibrary查找JVM dll。由您的应用程序决定在哪里可以找到JVM DLL。在我们的例子中,我们分发了第三方JRE并知道DLL的位置。
HMODULE jvmDll = LoadLibrary(jvmDllPath);
Next lookup the address of the function from the JVM dll using GetProcAddress
接下来使用GetProcAddress从JVM dll查找函数的地址
JvmCreateProcTypeDef jvmCreateProc = (JvmCreateProcTypeDef) GetProcAddress(jvmDll,"JNI_CreateJavaVM");
Now replace your code which calls the function directly, to something like the following which calls it via function pointer:
现在替换直接调用函数的代码,类似于以下通过函数指针调用它的代码:
jvmCreateProc(&internal::gJVM, (void**)&internal::gEnv, &vm_args);
This should get you passed all the compile link errors. Now all you have to do is deal with the runtime errors when your code cannot find the DLL :)
这应该让你传递所有的编译链接错误。现在你所要做的就是当你的代码找不到DLL时处理运行时错误:)
Hope this helps!
希望这可以帮助!