映射现有的 C++ JNI 函数
我有一个库,正在编译并创建一个完全独立的 C++ 程序。有两个 cpp 文件,一个包含主文件,另一个包含所有功能。
目前,该程序是使用带有参数的 Java ProcessBuilder 来实现的,用于调用 C++ 程序,并且该 C++ 程序的结果只是简单地输出到文件中。
现在,我想要将执行此操作的 C++ 函数的结果返回到我的 java 程序。 (C++ 函数中的结果是一个双无符号字符数组)
所以我的问题是 - 有没有办法映射这些现有的库函数,以便我可以直接从我的 java 程序调用它们,并且仍然继续在展台中使用该库-我目前的唯一方式是通过驱动程序 C++ 程序 main()?
我基本上试图避免必须编译相同的库两次 - 一次用于 JNI 功能,一次作为独立的 C++ 程序
谢谢
I have a library that I am compiling and creating a fully standalone C++ program. There are two cpp files, one that has the main, the other with all the functionality.
Currently, this program is implemented with a Java ProcessBuilder with args to call the C++ program and the results of that C++ program just simply go out to a file.
Now, I am wanting to get the results of that C++ function that does that work back to my java program. (The results in the C++ function is a double unsigned char array)
So my question is - is there a way to map those existing library functions so that I can call them from my java program directly, AND still keep using that library in the stand-alone way that I currently am, which is through that driver C++ program main()?
I am basically trying to avoid having to compile the same library twice - once for JNI functionality, and once as a standalone C++ program
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
Java Native Access (JNA) 会做你想做的事。
Java 本机接口 (JNI) 需要在 Java 和 C++ 之间添加一层额外的 C 粘合层。但是JNA可以直接从Java访问一些C函数。
您可能需要将您的函数声明为 extern“C”。您不必重新编译该库,但必须将其与 C++ 主函数链接。
如果您有一个大型 C++ 类库要公开,那么您可能会对 SWIG 感兴趣。但对于单个 C 函数,JNA 可能就足够了。
Java Native Access (JNA) will do what you want.
Java Native Interface (JNI) requires an additional layer of C glue between Java and C++. But JNA can access some C functions directly from Java.
You'll probably need to declare your function extern "C". You won't have to recompile the library, but you will have to link it with your C++ main function.
If you had a large C++ class library to expose, then you'd might be interested in SWIG. But for single C function, JNA is probably sufficient.
我认为 exe 可以导出类似于 dll 的函数(在 Windows 上使用 __declspec(dllexport)),这意味着您可以像 jni-dll 一样加载它。 (不过,您可能需要将其重命名为 .dll 或 .so 才能让 java 加载它。)
I think an exe can export functions similar to a dll (using __declspec(dllexport) on windows), which means you might be able to load it similarly to a jni-dll. (You might need to rename it .dll or .so to have java load it though.)
如果加载不起作用,另一种方法是像现在一样在单独的进程中启动程序,但给它一个命令行选项,该选项与 Java 程序建立某种共享内存区域。
这将避免来回复制 daya,但可能需要一个由 exe 和 java 程序加载的小型实用程序 DLL,即不简单。 (不确定 Java 是否可以轻松设置共享内存,无需本机 C 调用..)
(将投票/评论/接受粒度的答案分开)
If loading doesn't work, another way would be to start your program in a separate process as you do now, but give it an command-line option which establishes some kind of shared-memory-area with the Java program.
That would avoid copying the daya back and forth, but might require a small utility DLL loaded by both the exe and the java program, i.e. not simpler. (Not sure if Java can setup shared-memory easily w/o native c calls ..)
(keeping answers separate for voting/comments/accept granularity)
创建运行可执行文件并返回字符的 JNI 包装器 DLL。或者,将相关代码编译到静态库中,并将其链接到独立程序的 main() 函数,或链接到 JNI 存根以从 Java 调用。过程中总是更好。
Create a JNI wrapper DLL that runs the executable and returns the chars. Alternatively, compile the relevant code into a static library and link it either to a main() function for a standalone program, or to a JNI stub for calling from Java. In-process always better.
我相信正确的方法是首先将函数编译成独立的动态库(.dll / .so / .dylib)。
然后您可以:
I believe a proper way to do it would be first to compile your functions into an independent dynamic library (.dll / .so / .dylib).
Then you can :