映射现有的 C++ JNI 函数

发布于 2024-10-11 10:16:05 字数 390 浏览 8 评论 0原文

我有一个库,正在编译并创建一个完全独立的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丑丑阿 2024-10-18 10:16:05

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.

沧笙踏歌 2024-10-18 10:16:05

我认为 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.)

黑凤梨 2024-10-18 10:16:05

如果加载不起作用,另一种方法是像现在一样在单独的进程中启动程序,但给它一个命令行选项,该选项与 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)

謌踐踏愛綪 2024-10-18 10:16:05

创建运行可执行文件并返回字符的 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.

白衬杉格子梦 2024-10-18 10:16:05

我相信正确的方法是首先将函数编译成独立的动态库(.dll / .so / .dylib)。
然后您可以:

  • 编写一个链接到您的共享库的 C++ 可执行文件
  • 编写一个绑定到您的 C++ 库的 Java 程序,这要归功于 BridJ,例如(或者 JNA,如果你坚持使用普通的旧 C)

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 :

  • write a C++ executable that links against your shared library
  • write a Java program that binds to your C++ library thanks to BridJ, for instance (or JNA, if you stick to plain old C)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文