Java Native Access 不支持 C++,对吧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2241685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 20:16:27  来源:igfitidea点击:

Java Native Access doesn't do C++, right?

javajava-native-interfacenativejna

提问by Yuvi Masory

I've found many references online (including some on stackoverflow) to JNA being used for C++ libraries, but nothing I can find in the JNA docs indicates that's possible. There doesn't seem to be any way to wrap a C++ class, in particular.

我在网上找到了许多关于用于 C++ 库的 JNA 的参考资料(包括一些在 stackoverflow 上的参考资料),但我在 JNA 文档中找不到任何内容表明这是可能的。似乎没有任何方法可以包装 C++ 类,特别是。

I need native access to use RTAudio, but all of RTAudio's functions are member functions of the RTAudio class. So just to confirm, JNA isn't the way to go right?

我需要本机访问权限才能使用 RTAudio,但 RTAudio 的所有函数都是 RTAudio 类的成员函数。所以只是为了确认,JNA 不是正确的方法吗?

回答by Mark Elliot

What this question amounts to is asking how to call C++ instance methods using JNA, and it's possible, but you're going to have to do some work. In particular, you'll need to write a wrapper which extern "C"s any functions you actually need to invoke.

这个问题相当于询问如何使用 JNA 调用 C++ 实例方法,这是可能的,但您将不得不做一些工作。特别是,您需要编写一个包装器,它extern "C"是您实际需要调用的任何函数。

For any arbitrary type* function()definition you can map the method using JNA as returning a com.sun.jna.Pointer, but you won't be able to invoke methods on a C++ object from JNA.

对于任意type* function()定义,您可以使用 JNA 映射方法作为返回 a com.sun.jna.Pointer,但您将无法从 JNA 调用 C++ 对象上的方法。

A simple workaround for this would be to write a C interface library that simply invokes the method on the objects for you...so if you have some member function foo()you could export a C method from your C++ code:

一个简单的解决方法是编写一个 C 接口库,它只是为您调用对象上的方法……因此,如果您有一些成员函数,foo()您可以从 C++ 代码中导出 C 方法:

extern "C" void bar(type* var){
   var->foo();
}

Obviously this will add some work for you...but I suspect the overhead for switching to JNIwould be about the same.

显然,这将为您增加一些工作……但我怀疑切换到JNI的开销大致相同。

JNA only cares about the way in which the method is exported in the DLL -- and that must be withoutC++ decorations (hence the extern "C"), so you can do whatever you need to within any such method without exposing methods that you call.

JNA 只关心在 DLL 中导出方法的方式——并且必须没有C++ 修饰(因此是extern "C"),因此您可以在任何此类方法中执行任何您需要的操作,而无需公开您调用的方法。

In my contrived example above, this means that foo(), as long as it is defined within the DLL does not in fact have to even be exposed. Since it's a C++ function, JNA cannot call it directly, but it can be called from within a function that JNA can call, which is why my proposed solution works.

在我上面的人为示例中,这意味着foo(),只要它在 DLL 中定义,实际上甚至不必公开。由于它是一个 C++ 函数,JNA 不能直接调用它,但可以从 JNA 可以调用的函数中调用它,这就是我提出的解决方案有效的原因。

So, yes, you can fully encapsulate calls to all the member functions (create, operate, destroy) in a single function and JNA won't care.

所以,是的,您可以将所有成员函数(创建、操作、销毁)的调用完全封装在单个函数中,而 JNA 不会在意。

回答by zOlive

BridJis a spiritual child of JNA that adds some limited C++ support (+ full support from JNAerator). If you're not using too many templates it might just work...

BridJ是 JNA 的精神产物,它增加了一些有限的 C++ 支持(+ JNAerator 的完全支持)。如果您没有使用太多模板,它可能会起作用...

(disclaimer: I'm the author of BridJ & JNAerator)

(免责声明:我是 BridJ 和 JNAerator 的作者)

回答by Denis Tulskiy

Try Swig. It will create wrappers for c++ classes for you.

试试Swig。它将为您创建 C++ 类的包装器。

回答by Alonso

Youre right JNA is for accesing native libraries. I think what you need is a Java - COM Bridge. If this is the case there are a few free alternatives:

您说得对,JNA 用于访问本机库。我认为您需要的是 Java - COM 桥接器。如果是这种情况,有一些免费的替代方案:

JCOM http://sourceforge.net/projects/jcom

JCOM http://sourceforge.net/projects/jcom

Jacob http://sourceforge.net/projects/jacob-project

雅各布http://sourceforge.net/projects/jacob-project

I've used Jacob in the pass with good results, but I think it's a little bit outdated.

我在传球中使用了 Jacob 并取得了不错的效果,但我认为它有点过时了。