使用Android NDK和C ++ [英] Using Android NDK and C++
问题描述
我得到样例HelloJni项目工作,它使用C文件作为本地代码。我没有能够得到任何简单的例子使用C ++。采取以下JNI代码:
I got the sample HelloJni project working, which uses a C file for the native code. I have not been able to get any simple examples working with C++. Take the following JNI code:
#include <jni.h>
#include <string.h>
JNIEXPORT void JNICALL Java_com_test_testActivity_doSomething(JNIEnv * env, jobject obj)
{
}
如果我有一个.c文件中的代码,它工作正常。如果我将扩展名更改为.cpp,它编译正常,但运行时会弹起(强制关闭)。因为库和方法的签名是相同的,我不怀疑问题是在Java端。
If I have the code in a .c file, it works fine. If I change the extension to .cpp it compiles fine, but blows up when running (force closes). Since the library and method signature is the same either way, I don't suspect the problem being on the Java side.
推荐答案
您可能需要使用 externC
块封装您的代码:
You might need to surround your code with an extern "C"
block:
extern "C" {
JNIEXPORT ...
}
您应该能够通过在 #if
中包装extern块来创建一个能在C和C ++中工作的版本:
You should be able to make a version that will work in both C and C++ by wrapping the extern block in #if
:
#ifdef __cplusplus
extern "C" {
#endif
JNIEXPORT ...
#ifdef __cplusplus
}
#endif
这篇关于使用Android NDK和C ++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!