2013-01-24 71 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