开发者

C/C++ header to java

开发者 https://www.devze.com 2023-01-17 08:03 出处:网络
Is there any good tool to generate java (+JNI support if needed) from a header file so that a C or C++ library 开发者_开发技巧can be used as-is. Kind of a reverse of javah. The real functionality woul

Is there any good tool to generate java (+JNI support if needed) from a header file so that a C or C++ library 开发者_开发技巧can be used as-is. Kind of a reverse of javah. The real functionality would be in the C/C++, the Java would be only a shim on top for certain users.

I'm no expert on JNI but as far as I can see Javah forces you to do this back to front. It forces you to have JNI-isms penetrating unecessarily into the C code unless you write a conversion layer yourself. And writing a conversion layer by hand is basically a waste of time, since all the information is there in the header file to begin with.


For C, you can use JNA. You do have to declare function signatures redundantly in Java, but do not have to write any glue code. JNA is very easy to use.

For C or C++, you can use SWIG. SWIG is a little more complex to use, but automatically generates Java wrappers for C++ classes. I'm enjoying it.


JNAerator does exactly that : it reads C/C++/ObjectiveC headers and outputs Java bindings that rely on BridJ (C/C++), JNA (C only) or Rococoa (ObjectiveC, uses JNA).


Looks like SWIG works with Java: http://www.swig.org/Doc2.0/Java.html

Maybe this isn't exactly what you're looking for, though, since you do have to add SWIG directives…

0

精彩评论

暂无评论...
验证码 换一张
取 消