【问题标题】:java: symbol lookup error: undefined symbol: fftw_mallocjava:符号查找错误:未定义的符号:fftw_malloc
【发布时间】:2013-10-17 13:52:25
【问题描述】:

首先,这是我在这里的第一篇文章,所以我尽量说清楚。

我首先说我不确定它是否是编译/链接问题 - 但我认为它是。 . 我试图从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(){} 

非常感谢

【问题讨论】:

    标签: java c java-native-interface shared-libraries fftw


    【解决方案1】:

    你只需要包含你调用它的头文件,如果还存在未定义的符号,你不能构建它。顺便说一句,你可以使用这个工具swig

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

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

    【讨论】:

    • 不确定我理解正确但是:我在 c 文件中包含了头文件。编辑了帖子,因此 c 文件也将包含在内。
    • 我的意思是你的 .mk 文件怎么样。你是否也包括了 .c 可能像这样,file.LOCAL_SRC_FILES := FFTW.cpp。
    • 我不知道这个命令“LOCAL_SRC_FILES”我应该在哪里写? FFTW.cpp 你的意思是原始的 FFTW 文件?不是我写的
    • 对不起,我不认为你有 .mk 文件。你能输入这样的命令吗?gcc -shared -lfftw3 -lm -o libSample1.so Sample1.o fftw3.o?只需将您的 fftw3.o 文件放入。
    猜你喜欢
    • 2012-06-19
    • 2017-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-02
    • 2017-05-04
    • 1970-01-01
    相关资源
    最近更新 更多