2013-01-24 69 views
1

test.c的jni compile cpp和c有什么区别?

#include <string.h> 
#include <jni.h> 
#include <android/log.h> 
#include <stdio.h> 
#include <stdlib.h> 

jstring Java_com_test_b_hello_hellostr(JNIEnv* env,jobject thiz) 
{ 
    return (*env)->NewStringUTF(env, "Hello from JNI !"); 
} 

这编译OK。但是,当我更改为test.cpp时,编译出现错误。

libb/jtest.cpp:在函数 '_jstring * Java_com_test_b_hello_hellostr(JNIEnv的*,_jobject *)': jtest.cpp:108:错误:基地操作数 ' - >' 具有非指针类型 '_JNIEnv' make [1]:* [out /.../obj/ SHARED_LIBRARIES/libdrvb_intermediates/jtest.o] Error 1

这是为什么?它有不同的应用程序和C?

我检查系统jni.h文件:alps\dalvik\libnativehelper\include\nativehelper\jni.h

. 
    void  (*ReleaseStringChars)(JNIEnv*, jstring, const jchar*); 
    jstring  (*NewStringUTF)(JNIEnv*, const char*); 
    jsize  (*GetStringUTFLength)(JNIEnv*, jstring); 
... 
    jstring NewStringUTF(const char* bytes) 
    { return functions->NewStringUTF(this, bytes); } 
..... 

回答

2

JNI for C++是略有不同JNI为纯C.

在纯C是正确的使用方法:(*env)->SomeFunction(env, arg, arg, ...)

注意,您必须取消引用env,而且第一个参数功能始终为env

在C++中,它是不同的。您使用:env->SomeFunction(arg, arg, ...)

您无需解除引用env,并且您不会传递env作为第一个参数。

到Java的实际调用将是相同的。 Java并不关心你是否使用普通的C或C++来完成JNI的东西。

下面是使用C++进行JNI的快速入门。

http://journals.ecs.soton.ac.uk/java/tutorial/native1.1/implementing/cpp.html