0

まず第一に、これは私の最初の投稿なので、明確にするために最善を尽くします。

コンパイル/リンクの問題かどうかはわかりませんが、そうだと思います。. Javaコードからc関数を呼び出そうとしていますが、プログラムを実行しようとすると、このエラーが発生します(すべてのコンパイルとリンクは正常に機能します)。FFTWパッケージにもアクセスできるように、適切にリンクする方法がわからないためだと思います。私は、C コードが完全に機能していることを知っています。

私はkubuntoに取り組んでいます

詳細を説明する必要がある場合は教えてください。何が起こっているのかを理解するのに役立つことを願っています

ファイル名:Sample1.java、Sample1.c、Sample1.h
これらの行を使用して、Cファイルをコンパイル/リンクし、共有ライブラリを作成しました(私が理解しているように、Javaファイルのコンパイルは問題ではありません)

gcc -fPIC -c -lfftw3 -lm -I/usr/lib/jvm/java-7-openjdk-amd64/include -I/usr/lib/jvm/java-7-openjdk-amd64/linux Sample1.c

gcc -shared -lfftw3 -lm -o libSample1.so Sample1.o

javac Sample1.java

java Sample1 - here i get the error.

Sample1.java:

public class Sample1 {
  public native String stringMethod(String text);
  public static void main(String[] args) {
     System.loadLibrary("Sample1");
     Sample1 sample = new Sample1();
     String  text   = sample.stringMethod("JAVA");
     System.out.println("stringMethod: " + text);
  }
} 

Sample1.c:

#include "Sample1.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <fftw3.h>

void load_data(int inverse, void *data, char *path, int ncount) {
    double *dp;
    fftw_complex *cp;
    int k, eof;
    FILE *fp;
    double r, i, m;

    if (inverse)
            cp = (fftw_complex *)data;
    else
            dp = (double *)data;

    if ((fp = fopen(path, "r")) == NULL) {
            perror("cannot open input file");
            exit(-1);
    }

    for (eof = k = 0; k < ncount; k++) {
            if (inverse) {
                    if (eof ||
                        fscanf(fp, "%lf %lf %lf\n", &r, &i, &m) == EOF) {
                            eof = 1;
                            r = i = 0;
                    }
                    cp[k][0] = r;
                    cp[k][1] = i;
            } else {
                    if (eof || fscanf(fp, "%lf\n", &r) == EOF) {
                            eof = 1;
                            r = 0;
                    }
                    dp[k] = r;
            }
    }

    fclose(fp);
}

char* process_result(int inverse, int nx, int ny, void *data, char* path) {
    FILE *fp;
    fftw_complex *cp;
    double *dp, r, i, mag=0.0;
    int k, l, ik, il, max;
    int xshift,yshift;

    fp = fopen(path, "w");

    if (inverse) {
            dp = (double *)data;
            max = ny;
    } else {
            cp = (fftw_complex *)data;
            max = ny/2+1;
    }

    for (k = -(nx/2); k < (nx/2); k++) {
      for (l = -(ny/2); l < (ny/2); l++) {
        if (inverse) {
                ik=k;il=l;
                if(k<0)ik=k+nx;
                if(l<0)il=l+ny;
                r = dp[ny * ik + il] / (nx * ny);
                //periodic boundery condition
                if(mag<r){
                  mag = r; xshift=k;yshift=l;
                }
                fprintf(fp,"%i %i %lf\n", k, l, r);
        } else {
                r = cp[ny * k + l][0];
                i = cp[ny * k + l][1];
                mag = sqrt((r * r) + (i * i));
                fprintf(fp,"%lf %lf %lf\n", mag, r, i);
        }
      }
      fprintf(fp,"\n");
    }
    printf("Shift: ( %i, %i )\n",xshift, yshift);
    return xshift+" "+yshift;

}
 JNIEXPORT jstring JNICALL Java_Sample1_stringMethod
   (JNIEnv *env, jobject obj, jstring string) {

char *path1,*path2;
    int opt, inverse = 0;
    int ncount = 0, nx = 1024, ny = 1024, nyh = 0;
    double *dp1,*dp2,*res;
    fftw_complex *cp1,*cp2,*xc;

    int i, j;


    path1 = "1O.dat";
    path2 = "1R.dat";

    nyh = ( ny / 2 ) + 1;
    ncount = nx * ny;

    dp1 = (double *)malloc(sizeof (double) * nx * ny);
    dp2 = (double *)malloc(sizeof (double) * nx * ny);
    cp1 = (fftw_complex *)fftw_malloc(sizeof (fftw_complex) * nx * nyh);
    cp2 = (fftw_complex *)fftw_malloc(sizeof (fftw_complex) * nx * nyh);

    res = (double *)malloc(sizeof (double) * nx * ny);
    xc  = (fftw_complex *)fftw_malloc(sizeof (fftw_complex) * nx * nyh);

    memset(dp1, 0, sizeof (double) * nx * ny);
    memset(dp2, 0, sizeof (double) * nx * ny);
    memset(cp1, 0, sizeof (fftw_complex) * nx * nyh);
    memset(cp2, 0, sizeof (fftw_complex) * nx * nyh);
    memset(xc, 0, sizeof (fftw_complex) * nx * nyh);

    fftw_plan plan1 = fftw_plan_dft_r2c_2d( nx, ny, dp1, cp1, FFTW_ESTIMATE);
    fftw_plan plan2 = fftw_plan_dft_r2c_2d( nx, ny, dp2, cp2, FFTW_ESTIMATE);
    fftw_plan px    = fftw_plan_dft_c2r_2d( nx, ny,  xc, res, FFTW_ESTIMATE);

    load_data(0, dp1, path1, ncount );
    load_data(0, dp2, path2, ncount );

    fftw_execute(plan1);
    fftw_execute(plan2);

    for ( i = 0; i < (nx*nyh); i++){
       xc[i][0] = (cp1[i][0] * cp2[i][0] + cp1[i][1] * cp2[i][1]);
       xc[i][1] = (cp1[i][1] * cp2[i][0] - cp1[i][0] * cp2[i][1]);
    }

    fftw_execute(px);
    char* result = process_result(1, nx, ny, res,"xcorr.dat");

    fftw_destroy_plan(plan1);
    fftw_destroy_plan(plan2);
    fftw_destroy_plan(px);

    fftw_free(cp1);
    fftw_free(cp2);
    fftw_free(xc);

    free(dp1);
    free(dp2);
    free(res);

    fftw_cleanup();

    return result;


 const char *str = (*env)->GetStringUTFChars(env, string, 0);
 char cap[128];
 strcpy(cap, str);
 (*env)->ReleaseStringUTFChars(env, string, str);
 return (*env)->NewStringUTF(env, result);
}



 void main(){} 

どうもありがとう

4

1 に答える 1

0

これを呼び出す場所にヘッダーを含める必要があるだけです。未定義のシンボルも存在する場合、ビルドできません.ところで、このツールswigを使用できます

jni の開発を容易にするのに役立ちます。

さて、あなたの .mk ファイルはどうですか、忘れましたが、fftw_malloc() の .c ファイルを指定する必要がありました。

于 2013-10-17T14:03:13.270 に答える