Java Native Access 不支持 C 语言,对吗?
我在网上找到了许多关于用于 C++ 库的 JNA 的参考(包括 stackoverflow 上的一些),但我在 JNA 文档中找不到任何内容表明这是可能的。特别是,似乎没有任何方法可以包装 C++ 类。
我需要本机访问才能使用 RTAudio,但 RTAudio 的所有函数都是 RTAudio 类的成员函数。所以只是想确认一下,JNA 不是正确的选择吗?
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.
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这个问题相当于问如何调用C++实例使用 JNA 的方法,这是可能的,但您必须做一些工作。特别是,您需要编写一个包装器,其中
extern "C"
是您实际需要调用的任何函数。对于任意
type* function()
定义,您可以使用 JNA 映射该方法作为返回com.sun.jna.Pointer
,但您将无法调用来自 JNA 的 C++ 对象上的方法。一个简单的解决方法是编写一个 C 接口库,它只需为您调用对象上的方法...因此,如果您有一些成员函数
foo()
您可以从你的 C++ 代码:显然这会为你增加一些工作......但我怀疑切换到 JNI< 的开销/a> 大致相同。
JNA 只关心方法在 DLL 中导出的方式 - 并且必须没有 C++ 修饰(因此是
extern "C"
),因此您可以在任何此类方法中执行您需要执行的任何操作,而无需公开您调用的方法。在我上面设计的示例中,这意味着
foo()
只要在 DLL 中定义,实际上就不必公开。由于它是一个 C++ 函数,JNA 无法直接调用它,但可以从 JNA 可以调用的函数内调用它,这就是我提出的解决方案有效的原因。所以,是的,您可以将对所有成员函数(创建、操作、销毁)的调用完全封装在单个函数中,而 JNA 不会关心。
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.For any arbitrary
type* function()
definition you can map the method using JNA as returning acom.sun.jna.Pointer
, but you won't be able to invoke methods on a C++ object from JNA.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:Obviously this will add some work for you...but I suspect the overhead for switching to JNI would be about the same.
JNA only cares about the way in which the method is exported in the DLL -- and that must be without C++ decorations (hence the
extern "C"
), so you can do whatever you need to within any such method without exposing methods that you call.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.So, yes, you can fully encapsulate calls to all the member functions (create, operate, destroy) in a single function and JNA won't care.
尝试 Swig。它将为您创建 C++ 类的包装器。
Try Swig. It will create wrappers for c++ classes for you.
BridJ 是 JNA 的精神之子,添加了一些有限的 C++ 支持(+来自 JNAerator)。如果您没有使用太多模板,它可能会起作用...
(免责声明:我是 BridJ 和 JNAerator 的作者)
BridJ is 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...
(disclaimer: I'm the author of BridJ & JNAerator)
你说得对,JNA 用于访问本机库。我认为你需要的是一个 Java - COM 桥。如果是这种情况,有一些免费的替代方案:
JCOM http://sourceforge.net/projects/jcom
Jacob http://sourceforge.net/projects/jacob-project
我用过雅各布在传球中取得了不错的成绩,但我认为它有点过时了。
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:
JCOM http://sourceforge.net/projects/jcom
Jacob 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.