2013-10-17 59 views
0

首先,这是我第一次在这里发帖,所以生病的时候我尽力说清楚。java:符号查找错误:undefined symbol:fftw_malloc

我开始说iam不知道它是一个编译/链接问题 - 但我认为它是。 。 我想从java代码调用c函数,当iam试图运行程序时,我得到这个错误(所有的编译和链接工作正常)。我认为它是因为我不知道如何正确链接 - 以便FFTW包也可以访问。 我知道C代码工作正常。

荫工作kubunto

告诉我,如果我需要提供更多的细节,我希望你能帮助我了解什么在

文件名会:Sample1.java,Sample1.c,样本1。 ħ
我使用的那些行编译/链接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(){} 

非常感谢

回答

0

,你只需要包含在那里你调用这个头,如果没有定义符号也是存在的,你不能建立it.BTW ,您可以使用此工具swig

它可以帮助您轻松开发jni。

那么,你的.mk文件怎么样,我忘记了,你应该指定fftw_malloc()的.c文件。

+0

不知道我理解正确,但:我在头文件包含在c文件中。 编辑帖子,以便c文件将包括在内。 – nitayk

+0

我的意思是如何.mk file.did你也包括.c可能是这样的,file.LOCAL_SRC_FILES:= FFTW.cpp。 – venciallee

+0

我不知道这个通知“LOCAL_SRC_FILES” 我应该在哪里写它? 和FFTW.cpp u是指原始的FFTW文件?不是我写的 – nitayk