从 Java 调用 c 函数
如何从 Java 调用 C 函数。 看来c是基于编译器的。
我想从Java调用Windows中的C函数,并且 GCC 函数也来自 Java。
有什么参考吗?
How to call c function from Java.
Seems c is compiler based.
I would like to call C function in Windows from Java, and
GCC function fron Java too.
Any reference?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
看看 Java 本机接口:入门。
Have a look at Java Native Interface: Getting Started.
简而言之,只需确保加载包含函数定义的相关库,加载遵循 JNI 规范的库并包装第一个库中的目标函数,公开 Java 类中的本机方法,然后就可以开始了。
我建议不要使用原始 JNI,因为它包含大量样板代码,如果您开始包装一个大 C 库,您最终会诅咒自己。无论如何,在开始时请随意涉足 JNI,但在实际工作中请使用诸如 JNA 之类的东西。
In simple terms, just make sure you load the relevant library which contains the function definition, load the library which follows the JNI specification and wraps the target function from the first library, expose native methods from your Java class and you should be good to go.
I'd recommend against raw JNI since it contains a lot of boilerplate code and you would end up cursing yourself if you start wrapping a big C library. By all means do feel free to dabble in JNI when starting out but use something like JNA when it comes to real work.
您的选项包括:
Java 本机接口
请参阅:https://en.wikipedia.org/wiki/Java_Native_Interface
引用:
Java Native Access
请参阅:https://en.wikipedia.org/wiki/Java_Native_Access
引用:
JNR-FFI
请参阅:https://github.com/jnr/jnr-ffi引用
:
You options include:
Java Native Interface
see: https://en.wikipedia.org/wiki/Java_Native_Interface
quote:
Java Native Access
see: https://en.wikipedia.org/wiki/Java_Native_Access
quote:
JNR-FFI
see: https://github.com/jnr/jnr-ffi
quote:
在“异国情调”类别中,请参阅 NestedVM,它将 C 编译为 Mips,并运行 Mips
JVM 内的虚拟机。
http://nestedvm.ibex.org/
In the "exotic" category, see NestedVM, which compiles the C to Mips, and runs a Mips
VM inside JVM.
http://nestedvm.ibex.org/
检查 JNAerator。
https://code.google.com/p/jnaerator/
您需要提供源代码代码和预处理器定义等
Checkout JNAerator.
https://code.google.com/p/jnaerator/
You need to provide the source code and preprocessor definitions etc.
如果您使用的是 Windows 和 MinGW gcc,如果您在 lib 中遇到特定方法的 UnsatisfiedLinkError,则可能需要额外的标志:
If you are using Windows and MinGW gcc you may need additional flag if you are getting UnsatisfiedLinkError for specific method in lib:
@Jonas 给出了一个非常详细的答案,但我认为它也确实值得检查这个网站,你会得到所有重要的答案那里:
https://personal.ntu.edu.sg/ehchua/programming/java/javanativeinterface.html
它解释了如何使用 JNI 调用程序:
@Jonas gave a very elaborated answer, but I think its also really worth checking this website and you will get all your essential answers there:
https://personal.ntu.edu.sg/ehchua/programming/java/javanativeinterface.html
It explains how to call a program using JNI:
我找到了解决这个问题的方法。您需要确保使用 64 位 C++ 编译器来编译代码,以调用在 64 位 JRE 上运行的 java 函数。与此同时,我们需要将创建的dll文件的路径保存在“环境变量”下的“路径”中。
I got a solution for this problem. What you need to ensure is you're compiling the code using 64-bit c++ compiler for calling java function running on 64-bit JRE. Along with it we need to save the path of created dll file in "Path" under "Environment variable".
首先确保通过在属性
java.library.path
设置路径来加载类路径中的本机库或 .dll 文件,然后使用
System.loadLibrary()
First make ensure to load your native library or .dll file in class path by setting path at property
java.library.path
Then use
System.loadLibrary()
为了使 64 位兼容 dll 从下面的语句中删除“-MD”选项
For making 64-bit compatible dll Remove "-MD" option from statement below