需要在VS2022上编译JNI程序的帮助
我正在尝试编译一个非常基本的JNI程序。 Java代码是:
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello World !!");
new Helloworld().sayHello();
}
private native void sayHello();
}
我采取的下一步是使用Javac -H ...的命令行选项在IDE(Intellij Idea)之外进行编译。 这会产生一个生成的helloworld.h文件。 接下来,我在此处编写了一个同样的基本C程序,代码:
#include "Helloworld.h"
JNIEXPORT void JNICALL Java_Helloworld_sayHello(JNIEnv *, jobject){
printf("\nSay something... Anything...");
}
我尝试使用VS 2022 DLL选项编译,首先使用预编译的标头,然后没有预编译的标头,但没有运气。一系列错误表现出来。在VS环境中进行编译是理想的选择,因为计划从Java称为CAVA的实际代码包含C和AVX512组件的组合,因此拥有IDE支持是理想的选择(尤其是在调试期间)。关于如何将其编译到.dll库的任何建议,这将是非常欢迎Java所接受的。
然后我搬出了Vs 2022,并使用以下命令行编译字符串:
cl /c /I "C:\\Program Files\\Microsoft\\jdk\\include\\" /I "C:\\Program Files\\Microsoft\\jdk\\include\\win32\\" .\Helloworld.c
这给出以下非常持久的错误:
Microsoft(R)C/C ++优化编译器版本19.31.31106.2 for x64 版权(C)Microsoft Corporation。版权所有。
helloworld.c
。\ helloworld.c(3):错误c2055:预期的正式参数列表,不是类型列表,
我尝试将正式参数从jobight更改为jobight j,但这并没有成功(显然,因为JNI中的定义。 我试图使用CL命令选项 /u __cplusplus来不确定__cplusplus,但没有欢乐。不知道接下来要做什么。
欢迎任何建议。 谢谢
Am trying to compile a very elementary JNI program. The Java code is :
public class Helloworld {
public static void main(String[] args) {
System.out.println("Hello World !!");
new Helloworld().sayHello();
}
private native void sayHello();
}
The next step I took was to compile it outside the IDE (Intellij IDEA), using the command line option of javac -h ...
This produces a machine generated Helloworld.h file.
Next I wrote an equally elementary C program, code here :
#include "Helloworld.h"
JNIEXPORT void JNICALL Java_Helloworld_sayHello(JNIEnv *, jobject){
printf("\nSay something... Anything...");
}
This I tried compiling using VS 2022 dll option, first with pre-compiled headers, and then without the precompiled headers, but no luck. A vast array of errors manifested themselves. It would have been ideal to have this compiled within the VS environment, since the actual code that is planned to be called from Java contains a mix of C and AVX512 assembly, so having the IDE support would have been ideal (especially during debugging). Any advice on how to compile this to a .dll library that would be acceptable to Java would be very welcome.
Then I moved out of VS 2022, and used the following command line compilation string :
cl /c /I "C:\\Program Files\\Microsoft\\jdk\\include\\" /I "C:\\Program Files\\Microsoft\\jdk\\include\\win32\\" .\Helloworld.c
This gives the following very persistent error :
Microsoft (R) C/C++ Optimizing Compiler Version 19.31.31106.2 for x64
Copyright (C) Microsoft Corporation. All rights reserved.
Helloworld.c
.\Helloworld.c(3): error C2055: expected formal parameter list, not a type list
I tried changing the formal parameter from jobject to jobject j, but that did not succeed (obviously, since the definition in the jni.h states jobject is a typedef for a _jobject *).
I tried to undefine the __cplusplus by using the cl command option /U __cplusplus but no joy. Not sure what to try next.
Any advice is welcome.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
来自 Microsoft的文档:
您的函数定义是.c文件中的一个,即
您需要更改为类似
任何相应函数声明(例如,.h文件中)的功能定义。
From Microsoft's documentation:
Your function definition is the one in your .c file, i.e.
which you need to change into something like
Any corresponding function declaration (e.g. in a .h file) should work with or without parameter names.