将现有的 C++(.h 和 .cpp)文件转换为适用于 Android 的 java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5949557/
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-30 13:37:08  来源:igfitidea点击:

Convert existing C++ (.h and .cpp) files to java for Android

javac++android

提问by Viktor Apoyan

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 Interfaceand now I know how to make .so file and import it to Android. But the main problem is that:

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

Is there any tool that converts c++ (.h and .cpp) file to java style files which I can compile and create .so library ? I have heard about JNI - javah and javac can that tools help me and if they can so HOW ?

是否有任何工具可以将 c++(.h 和 .cpp)文件转换为 java 样式文件,我可以编译和创建 .so 库?我听说过 JNI-javah 和 javac,这些工具可以帮助我吗?如果可以的话,如何?



Edited:

编辑:

javah is a useful tool that creates a C-style header file from a given class. The resulting header file describes the class file in C terms. Although it is possible to manually create the header file, this is almost always a bad idea. javah knows exactly how Java types and objects map into C types. For example, an int in Java maps to a long in C, and a long in Java maps to a 64-bit value, _int64, in native code. How to use this javah?

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

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.

有人知道如何在 android 应用程序中使用 .so 库中实现的函数吗?如果有人有代码示例或有用的文章链接,请提供。

Thanks in advance !

提前致谢 !

回答by olivierg

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++ 文件转换为 Java 风格”。您需要围绕现有的 C++ 代码创建一个 JNI 包装器。这个 JNI 包装器实际上是可以被 Java 调用的 C++ 代码。

By wrapper I mean that you shouldn't have to modify your existing C++ code base. This wrapper, or better said this bindingshould 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.

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

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

如果代码库不是太大,那么我建议您手动编写此包装器,如 The JavaTM Native Interface Programmer's Guide and Specification 中所述

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.

现在,如果您尝试绑定一个大型库,则可能会出现问题。所以,关于工具,我没有使用过,但请查看SWIG和相关的SWIG Java 文档

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

根据主页描述,这就是您要的:

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.

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

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.

javah 在某些情况下可能很有用,但这不是您所要求的。它从 Java 类中的本机声明中提取 JNI 样板代码。关于 javac,它是 Java 编译器,无关紧要。

回答by ildjarn

When developing for Android you are not limited to using the Java language. Why not use C++ directly? See e.g. the Android NDK.

在为 Android 开发时,您不仅限于使用 Java语言。为什么不直接使用C++?参见例如Android NDK