首先,这是我在这里的第一篇文章,所以我要尽力明白。
我开始说我不确定它是否是编译/链接问题 - 但我认为是。 。 我试图从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(){}
非常感谢
答案 0 :(得分:0)
你只需要在你调用它的地方包含标题,如果还存在未定义的符号,你就无法构建它.BTW,你可以使用这个工具swig
它可以帮助您轻松开发jni。
好吧,你的.mk文件怎么样,我忘了,你应该指定fftw_malloc()的.c文件。