jni编译cpp跟c有区别
jni编译cpp和c有区别?test.c #include string.h#include jni.h#include android/log.h#include std
jni编译cpp和c有区别? test.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: In function '_jstring* Java_com_test_b_hello_hellostr(JNIEnv*, _jobject*)':
jtest.cpp:108: error: base operand of '->' has non-pointer type '_JNIEnv'
make[1]: *** [out/.../obj/SHARED_LIBRARIES/libdrvb_intermediates/jtest.o] Error 1
为什么呢? cpp和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); } ..... java
[解决办法] 你改成cpp后,对应的Android.mk里也要改成xx.cpp
[解决办法] 另外还要在方法上面加上
extern "C"
[解决办法] 函数的参数类型不一样的
[解决办法] 引用: 能具体说一下怎么不一样呢? jni.h中有这段声明
struct _JNIEnv;
struct _JavaVM;
typedef const struct JNINativeInterface* C_JNIEnv;
#if defined(__cplusplus)
typedef _JNIEnv JNIEnv;
typedef _JavaVM JavaVM;
#else
typedef const struct JNINativeInterface* JNIEnv;
typedef const struct JNIInvokeInterface* JavaVM;
#endif
c++是由struct _JNIEnv实现的,你可以去看下_JNIEnv的实现,就能明白了