繁体   English   中英

Java:符号查找错误:未定义的符号:fftw_malloc

[英]java: symbol lookup error: undefined symbol: fftw_malloc

首先,这是我在这里的第一篇文章,所以请尽我所能。

我首先说不知道是不是编译/链接问题,但我认为是。 我试图从Java代码中调用c函数,而当我试图运行该程序时,出现此错误(所有编译和链接均正常运行)。 我认为是因为我不知道如何正确链接-因此FFTW软件包也将可以访问。 我知道一个事实,即C代码运行正常。

Iam在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(){} 

非常感谢

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

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

好吧,关于.mk文件,我忘了,应该指定fftw_malloc()的.c文件。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM