问题描述
我正在开发一个 Java 应用程序,我需要在其中调用一些 C++ 函数(来自 Google Talk 库 libjingle).目标是在 Google App Engine(仅支持 Python 或 Java)上运行它.
I am developing a Java application in which I need to call some C++ functions (from Google Talk library libjingle) . The objective is to run it all on Google App Engine (which only supports Python or Java).
我该怎么做?
推荐答案
您需要在您的 Java 代码中定义 native
方法,用于您想在 C++ 中实现的任何内容,并直接访问您的本机代码.然后你在你的代码上运行 javah
,它会为你生成 C 头文件,你需要提供 C++ 实现.
You need to define native
methods in your java code for whatever you want to be implemented in C++ and directly access your native code. Then you run javah
on your code and it will generate the C header files for you and you'll need to provide C++ implementations.
您可以像任何其他方法一样从 Java 代码调用本机方法,但它们的实现仍然是用 C++ 编写的,并直接与任何其他本机库对话.
The native methods you can call from your Java code like any other methods and still they'll have their implementation written in C++ and talking to whatever other native library directly.
然后您需要设置 java.library.path 系统属性以包含您需要的共享 C/C++ 库:在这种情况下需要 google 库和您自己的 JNI 实现库.
You then need to set the java.library.path system property to include the shared C/C++ libraries that you require: the google library and your own JNI implementation library would be required in this case.
这篇关于从 Java 调用 C++ 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!