无法编译 JNI C 不兼容的指针类型
Can't compile JNI C incompatible pointer type
我正在编写一些本机代码以将 RPI 感应帽与我的 java 东西连接起来,但我无法让我的本机暗示。我在 java 中编写了存根,编译它然后使用 javah 提取头文件。我在 C 中创建了方法,将一个简单的 char 数组转换为一个字符串以供返回。我似乎无法编译它。
Java:
/**
* NativeTest - PACKAGE_NAME
* Created by matthew on 21/07/16.
*/
class SenseHat
{
static {
System.loadLibrary("SenseHat");
}
public native String getTemperature();
public native String getHumidity();
public native String getOrientation();
}
头文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SenseHat */
#ifndef _Included_SenseHat
#define _Included_SenseHat
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SenseHat
* Method: getTemperature
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature
(JNIEnv *, jobject);
/*
* Class: SenseHat
* Method: getHumidity
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity
(JNIEnv *, jobject);
/*
* Class: SenseHat
* Method: getOrientation
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
C文件:
#include <jni.h>
#include <stdio.h>
#include "SenseHat.h"
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature(JNIEnv *env, jobject thisObj) {
char done[] = "temperature";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity(JNIEnv *env, jobject thisObj) {
char done[9] = "humidity";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation(JNIEnv *env, jobject thisObj) {
char done[12] = "orientation";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
我正在使用以下命令编译它:
gcc -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/ -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/linux/ -shared -o libSenseHat.so SenseHat.c
你做不到
char done[] = "temperature";
/* ... */
answer = (*env)->NewStringUTF(env,&done);
/* --^-- & is redundant */
应该是
char done[] = "temperature";
/* ... */
answer = (*env)->NewStringUTF(env,done);
甚至
answer = (*env)->NewStringUTF(env,"temperature");
你也不应该 free(done)
。此内存未使用 malloc()
分配,因此释放会导致未定义的行为。
我看到的第一个问题:您声明局部数组变量并在其上使用 free()。使用 malloc()/free() 或在本地声明您的数组,但不要混合使用两者。
我正在编写一些本机代码以将 RPI 感应帽与我的 java 东西连接起来,但我无法让我的本机暗示。我在 java 中编写了存根,编译它然后使用 javah 提取头文件。我在 C 中创建了方法,将一个简单的 char 数组转换为一个字符串以供返回。我似乎无法编译它。 Java:
/**
* NativeTest - PACKAGE_NAME
* Created by matthew on 21/07/16.
*/
class SenseHat
{
static {
System.loadLibrary("SenseHat");
}
public native String getTemperature();
public native String getHumidity();
public native String getOrientation();
}
头文件:
/* DO NOT EDIT THIS FILE - it is machine generated */
#include <jni.h>
/* Header for class SenseHat */
#ifndef _Included_SenseHat
#define _Included_SenseHat
#ifdef __cplusplus
extern "C" {
#endif
/*
* Class: SenseHat
* Method: getTemperature
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature
(JNIEnv *, jobject);
/*
* Class: SenseHat
* Method: getHumidity
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity
(JNIEnv *, jobject);
/*
* Class: SenseHat
* Method: getOrientation
* Signature: ()Ljava/lang/String;
*/
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation
(JNIEnv *, jobject);
#ifdef __cplusplus
}
#endif
#endif
C文件:
#include <jni.h>
#include <stdio.h>
#include "SenseHat.h"
JNIEXPORT jstring JNICALL Java_SenseHat_getTemperature(JNIEnv *env, jobject thisObj) {
char done[] = "temperature";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
JNIEXPORT jstring JNICALL Java_SenseHat_getHumidity(JNIEnv *env, jobject thisObj) {
char done[9] = "humidity";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
JNIEXPORT jstring JNICALL Java_SenseHat_getOrientation(JNIEnv *env, jobject thisObj) {
char done[12] = "orientation";
jstring answer;
/* Make a new String based on done, then free done. */
answer = (*env)->NewStringUTF(env,&done);
free(done);
return answer;
}
我正在使用以下命令编译它:
gcc -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/ -I /usr/lib/jvm/jdk-8-oracle-arm32-vfp-hflt/include/linux/ -shared -o libSenseHat.so SenseHat.c
你做不到
char done[] = "temperature";
/* ... */
answer = (*env)->NewStringUTF(env,&done);
/* --^-- & is redundant */
应该是
char done[] = "temperature";
/* ... */
answer = (*env)->NewStringUTF(env,done);
甚至
answer = (*env)->NewStringUTF(env,"temperature");
你也不应该 free(done)
。此内存未使用 malloc()
分配,因此释放会导致未定义的行为。
我看到的第一个问题:您声明局部数组变量并在其上使用 free()。使用 malloc()/free() 或在本地声明您的数组,但不要混合使用两者。