采用Android NDK和C ++ [英] Using Android NDK and C++

查看:112
本文介绍了采用Android NDK和C ++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我拿到样品HelloJni项目的工作,它采用了原生code A C文件。我一直没能得到与C ++工作的简单的例子。看看下面的JNI code:

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文件的code,它工作正常。如果我改变扩展.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.

推荐答案

您可能需要围绕你的code与的externC块:

You might need to surround your code with an extern "C" block:

extern "C" {

    JNIEXPORT ...

}

您应该能够使一个版本将在C和C ++的工作在包裹的extern块#如果

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屋!

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