Android将“.c”后缀名改为“.cpp”时java调用native失败及“error: base operand of '>' has nonpoin[转]

现象“.c”后缀名改为“.cpp”时java调用native失败 。

解决 加入“ extern "C" ”。  

 

现象“error: base operand of '->' has non-pointer type '_JNIEnv'”错误。   

解决 将“(*env)->NewStringUTF(env, "HelloWorld from JNI !");”改为“env->NewStringUTF("HelloWorld from JNI !")”。

 

例子:

 

Cpp代码  收藏代码
  1. #include <stdio.h>  
  2. #include <string.h>  
  3. #include <android/log.h>  
  4. #include <jni.h>  
  5.   
  6. #ifdef __cplusplus  
  7. extern "C"  
  8. {  
  9. #endif  
  10.   
  11. jint Java_com_duicky_MainActivity_add(JNIEnv* env, jobject thiz, jint x, jint y)  
  12. {  
  13.     //该方法为打印的方法  
  14.     __android_log_print(ANDROID_LOG_INFO, "JNIMsg""Get Param:  x=%d y=%d ", x, y);  
  15.   
  16.     int iRet = x + y;  
  17.     return iRet;  
  18. }  
  19.   
  20. jstring Java_com_duicky_MainActivity_getString(JNIEnv* env, jobject thiz)  
  21. {  
  22.     jstring strRet = env->NewStringUTF("HelloWorld from JNI !");  
  23.     return strRet;  
  24. }  
  25.   
  26. #ifdef __cplusplus  
  27. }  
  28. #endif  

 

原文地址:https://www.cnblogs.com/afly/p/2459850.html