如何从 C++ 调用 Java使用SWIG?

发布于 2024-09-08 18:04:03 字数 395 浏览 1 评论 0原文

我可以使用 SWIG 从 Java 调用 C++ 代码,但我找不到任何有关如何执行相反操作(从 C++ 调用 Java)的文档。

官方 SWIG 文档说 (http://www.swig.org/Doc1.3 /Java.html#java_overview): “SWIG 使 Java 程序能够轻松地从 Java 调用 C/C++ 代码。历史上,SWIG 无法生成任何代码来从 C++ 调用 Java 代码。但是,SWIG 现在支持完整的跨语言多态性,并且生成代码来调用包装 C++ 虚拟方法时从 C++ 升级到 Java。”

但我找不到哪里说怎么做!非常感谢任何帮助。谢谢。

I am able to call C++ code from Java using SWIG but I can't find any documentation on how to do the reverse (call Java from C++).

The official SWIG documentation says (http://www.swig.org/Doc1.3/Java.html#java_overview):
"SWIG enables a Java program to easily call into C/C++ code from Java. Historically, SWIG was not able to generate any code to call into Java code from C++. However, SWIG now supports full cross language polymorphism and code is generated to call up from C++ to Java when wrapping C++ virtual methods."

But I can't find where it says how to do it! Any help is greatly appreciated. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

薄暮涼年 2024-09-15 18:04:03

实际上这是可能的,我已经基于从C++继承虚拟类到Java来实现了它。

您可以在“24.5 使用控制器的跨语言多态性”一章中找到解决方案SWIG 文档的 Java 部分。

Actually this is possible and I have implemented it based on inheriting a virtual class from C++ to Java.

You can find solution in chapter "24.5 Cross language polymorphism using directors" of Java part of SWIG document.

有深☉意 2024-09-15 18:04:03

有一个关于从 C++ 调用 Java 回调的小官方示例 。效果很好。

你的 C++ 回调类最好是非抽象的,否则你会失去 Java 中的默认构造函数。

There is a tiny official example on calling Java callbacks from C++. Works well.

Your C++ callback class better be non-abstract, you gonna loose default constructor in Java otherwise.

疯狂的代价 2024-09-15 18:04:03

根据声明中相当奇怪的措辞,我想说你想要的通常是不可能的。 “...包装 C++ 虚拟方法时从 C++ 调用到 Java。” “...调用...”让我相信您正在调用继承接口的受保护或公共成员,并且“...当包装 C++ 虚拟方法时”让我得出结论,您只能在以下情况下这样做:您正在重写继承的接口。所以听起来实际的用例非常狭窄。

但是,我只是基于您粘贴的文本的措辞。它可能会也可能不会帮助你。

Based on the rather strange wording of the statement I'd say what you want isn't generally possible. "...call up from C++ to Java when wrapping C++ virtual methods." The "...call up..." leads me to believe you're calling protected or public members of an inherited interface and, "...when wrapping C++ virtual methods," leads me to conclude you can only do so when you're overriding an inherited interface. So it sounds like the actual use case is very narrow.

But then, I'm just basing this on the wording of the text you've pasted. It may or may not help you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文