2016-07-21 7 views
0

RPIのセンスハットとJavaのものをインターフェースするためにネイティブコードを使用しています。私はJavaでスタブを書いて、それをコンパイルしてからjavahを使ってヘッダファイルを抽出しました。単純なchar配列を返す文字列に変換するために、Cでメソッドを作成しました。私はそれをコンパイルすることはできないようです。 のJava:JNI Cの互換性のないポインタ型をコンパイルできません

/** 
* 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 
+1

コンパイラ出力を提供できますか? – Sergio

答えて

1

することができます」 do do

char done[] = "temperature"; 
    /* ... */ 
    answer = (*env)->NewStringUTF(env,&done); 
           /* --^-- & is redundant */ 

それはまた、あなたがfree(done)いけない

char done[] = "temperature"; 
    /* ... */ 
    answer = (*env)->NewStringUTF(env,done); 

あるいは

answer = (*env)->NewStringUTF(env,"temperature"); 

でなければなりません。このメモリはmalloc()で割り当てられていないため、解放すると未定義の動作につながります。

+0

うまくいった乾杯。 :D – MAWood

+0

*できません... 'NewStringUTF(env、&done);' *は間違っています、 'array'と'&array'は両方とも正しく、両方とも 'array'の最初の要素のアドレスに評価されます。 'printf("%p \ n%p \ n "、done、&done);'のような 'array'と'&array'の結果を出力するコードを記述します。 –

+0

@AndrewHenle同じメモリ位置を指しているにもかかわらず、それらはまったく異なった互換性のない型を持っています( 'done'は' char * 'と'&done'はです)。関数は次のプロトタイプ 'jstring NewStringUTF(JNIEnv * env、const char * bytes);を持っているので、'&done'は受け入れられない引数であり、コンパイラはそのことを伝えます。 – Sergio

1

私が見る最初の問題:ローカル配列変数を宣言し、free()を使用します。 malloc()/ free()を使用するか、配列をローカルで宣言しますが、両方を混在させないでください。

関連する問題