将现有的C ++(.h和.cpp)文件转换为java for Android [英] Convert existing C++ (.h and .cpp) files to java for Android

查看:243
本文介绍了将现有的C ++(.h和.cpp)文件转换为java for Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Eclipse下开发Android。我有一些c ++(.h和.cpp)文件,我希望在我的Android应用程序中使用。我已阅读使用Java Native Interface在C / C ++中编程现在我知道如何使.so文件并将其导入到Android。但是主要的问题是:

I am developing for Android under Eclipse. I have some c++ (.h and .cpp) files that I wish to use in my Android application. I have read Programmming in C/C++ with the Java Native Interface and now I know how to make .so file and import it to Android. But the main problem is that:

有什么工具可以将c ++(.h和.cpp)文件转换为java风格的文件, .so库?我听说过JNI - javah和javac可以那工具帮助我,如果他们可以这样HOW?

已修改:

javah是一个用于从给定类创建C样式头文件的有用工具。生成的头文件用C语言描述类文件。虽然可以手动创建头文件,但这几乎总是一个坏主意。 javah知道Java类型和对象如何映射到C类型。例如,Java中的int映射到C中的长整型,而Java中的长整型映射到本地代码中的64位值_int64。如何使用这个javah?

任何人都明白如何使用在.so库中实现的函数在android应用程序中?如果有人有代码示例或有用的文章链接,请给。

Anybody have expiarance of how to use functions that are implemented in .so library in android application ? If anybody have code examples or useful articles links please give.

提前感谢!

推荐答案

不要将C ++文件转换为java风格。您需要在现有的C ++代码周围创建一个JNI包装器。这个JNI包装器实际上是可以被Java调用的C ++代码。

You don't "convert C++ file to java style". You need to create a JNI wrapper around your existing C++ code. This JNI wrapper is actually C++ code that can be called by Java.

通过包装我的意思是你不必修改现有的C ++代码库。这个包装器,或者更好地说这 binding 通常应该很薄。包装器代码仅用于公开现有功能,而不是实现它们。最好把实现放在(可移植的)C ++代码库中。

By wrapper I mean that you shouldn't have to modify your existing C++ code base. This wrapper, or better said this binding should generally be very thin. The wrapper code is only meant to expose existing functionalities, not to implement them. It is better to leave the implementation in the (portable) C++ code base.

如果代码库不是太大,那么我建议你写这个包装器手,如 JavaTM Native
Interface
中所述程序员指南和规范

If the code base isn't too large, then I recommend that you write this wrapper by hand, as explained in The JavaTM Native Interface Programmer's Guide and Specification

现在,如果您要绑定大型库,可能会有问题。因此,关于工具,我没有使用,但请查看 SWIG ,以及相关 SWIG Java文档

Now, if you are trying to bind a large library, it may be problematic. So, in regard to tools, I haven't used that, but have a look at SWIG, and the relevant SWIG Java documentation.

根据主页说明,这是您要求的:

According to the homepage description, it's what you're asking for:


SWIG通常用于解析C / C ++
接口并生成[Java,Python,PHP,
...]所需的'glue
代码'以调用C / C ++代码。

SWIG is typically used to parse C/C++ interfaces and generate the 'glue code' required for [Java, Python, PHP, ...] to call into the C/C++ code.

javah在某些情况下很有用,但它不是你要求的。它从Java类中发现的本地声明中提取JNI锅炉板代码。关于javac,它是Java编译器,这是不相关的。

javah can be useful in certain cases, but it's not what you ask for. It extracts JNI boiler plate code out of native declarations found in Java classes. Regarding javac, it's the Java compiler, that's irrelevant.

这篇关于将现有的C ++(.h和.cpp)文件转换为java for Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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