从Java访问C ++共享库:JNI,JNA,CNI或SWIG? [英] Access C++ shared library from Java: JNI, JNA, CNI, or SWIG?

查看:1376
本文介绍了从Java访问C ++共享库:JNI,JNA,CNI或SWIG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您建议从Java访问C ++共享库时使用以下哪种方法(或其他方法)?

Which of the following (or other) method would you recommend for accessing a C++ shared library from Java and why?


  1. JNI:我听说这有一些陷阱,是相当的承诺?

  2. SWIG:显然这使得使用JNI更容易,但我听说它有一些问题吗?

  3. JNA:我可以编写一个C接口,然后使用JNA,这显然比JNI容易得多?

  4. CNI:显然这比JNI容易吗?

  5. 另一个图书馆:它不能商业化,我更喜欢那些仍然保持良好的东西(这将抛出JNIEasy,JNative和JACE - 我想)。

  1. JNI: I hear this has a number of pitfalls and is quite the undertaking?
  2. SWIG: Apparently this makes using JNI easier, but I've heard it has some problems too?
  3. JNA: I could write a C interface and then use JNA with it instead, which is apparently significantly easier than JNI?
  4. CNI: Apparently this is easier than JNI?
  5. Another library: it can't be commercial and I'd prefer something that is still well maintained (this would throw out JNIEasy, JNative, and JACE - I think).

我想尽量减少开发时间和问题。

I'm trying to go for minimal development time and problems down the road. Any info on pros/cons of each of these would be appreciated.

编辑:
最好,我能够在Java中使用我的C ++对象,但正如我在#3中提到的,如果绝对必要,我可以使用函数编写一个C接口。我想在Java中提供的C ++界面是:

Optimally, I'd be able to use my C++ objects in Java, but as I mentioned in #3, I could write a C interface using just functions if absolutely necessary. The C++ interface I'd like to have available in Java is something like:

class MyObj1
{
    MyObj1(std::string, bool);
    bool Func1();
    void Func2(bool);
}

class MyObj2
{
    MyObj2(MyObj1*);
    ssize_t Func(uint8_t *, size_t, MyObj1);
}



实际的接口有点复杂,但这演示了对象的类型和我将使用的方法。一些对象是派生的,但是Java接口不需要有任何多态行为。

The actual interface is a bit more complicated, but this demonstrates the type of objects and methods I'll be using. Some of the objects are derived, but the Java interface does not need to have any polymorphic behavior.

推荐答案

JNA是最简单的使用,但正如你所说,需要手写一个C接口到C ++ API。它也可以比JNI慢一个数量级。但是,我在一台计算机上测量了几百纳秒的单个调用,因此除非在瓶颈中,否则不太可能影响。

JNA is the easiest to use, but as you note, requires hand-writing a C interface to the C++ API. It can also be slower than JNI by an order of magnitude. However, I measured individual calls at a few hundred nanoseconds on one machine, so that's unlikely to matter except in a bottleneck.

JNA在Java中冗余地指定C函数签名。 JNI可以在C字符串中冗余地指定Java函数签名。

JNA redundantly specifies C function signatures, in Java. JNI can redundantly specify Java function signatures, in C strings. Discrepancies in either can result in unexpected runtime behavior.

我个人会使用JNA,除非接口是复杂的,使得手写的C接口繁琐你,或者个人电话超过几百纳秒。

I personally would use JNA unless the interface is complex enough to make hand-writing the C interface cumbersome for you, or the individual calls are more than a few hundred nano seconds.

本周我遇到过这样的异常 - 一个丰富的C ++界面,有许多类和方法。我开始玩SWIG,看起来很有前途。它是相当容易使用,并自动生成Java绑定和C实现。智能指针确实需要一些额外的工作 - 你必须指示SWIG实例化模板。

This week I've been faced with such an exception -- a rich C++ interface with many classes and methods. I've started playing with SWIG, and it's looking promising. It's been fairly easy to use, and automatically generates the Java bindings and C implementation. Smart pointers did take a little extra work -- you have to instruct SWIG to instantiate the templates.

EDIT(一年后)

SWIG非常强大。它也可以更复杂的设置。对于简单,薄的接口,我可能会考虑JNA或JNI第一。但是SWIG对于厚接口是方便的。

SWIG is amazingly powerful. It can also be more complex to set up. For simple, thin interfaces, I'd probably consider JNA or JNI first. But SWIG is handy for thick interfaces.

由于一些C ++头文件的复杂性,我对SWIG的工作感到有点惊讶。但是SWIG似乎没有什么困难。

I'm a little surprised that SWIG works, given the complexity of some C++ header files. But SWIG appears to have little difficulty.

我必须写一些SWIG类型和包含C ++ / JNI代码的宏。例如,通过引用传递std :: strings需要自定义类型。将抛出的C ++异常转换为抛出的Java异常需要一个typemap和一个宏。

I did have to write some SWIG typemaps and macros containing C++/JNI code. For example, passing std::strings by reference required a custom typemap. Transforming thrown C++ exceptions to thrown Java exceptions required a typemap and a macro.

除了之外,我们的头文件不需要更改SWIG完全实例化了智能指针模板,该模板已被一些类不满足其对默认构造函数的期望。解决方案:添加一些默认构造函数。

No changes were needed to our header files except that SWIG fully instantiated a smart-pointer template, which had been parameterized with some classes that did not satisfy its expectation of a default constructor. Solution: add a few default constructors.

这篇关于从Java访问C ++共享库:JNI,JNA,CNI或SWIG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆