c++ - Java JNI with C function using gsl. -
i trying use jni in order call function defined in c. weird because when run java program, 1 mistake , other times different one. here are:
82 [main] java (5556) c:\windows\system32\java.exe: *** fatal error - cygheap base mismatch detected - 0x612708f0/0x17ff08f0. problem due using incompatible versions of cygwin dll. search cygwin1.dll using windows start->find/search facility , delete recent version. recent version *should* reside in x:\cygwin\bin, 'x' drive on have installed cygwin distribution. rebooting suggested if unable find cygwin dll. java.lang.unsatisfiedlinkerror: c:\cygwin\home\juli▒n\pruebas_jni\caracterizacion.dll: se realiz▒ un acceso no v▒lido la ubicaci▒n de memoria @ java.lang.classloader$nativelibrary.load(native method) @ java.lang.classloader.loadlibrary1(unknown source) @ java.lang.classloader.loadlibrary0(unknown source) @ java.lang.classloader.loadlibrary(unknown source) @ java.lang.runtime.loadlibrary0(unknown source) @ java.lang.system.loadlibrary(unknown source) @ main.<clinit>(main.java:6) exception in thread "main"
and
exception in thread "main" java.lang.unsatisfiedlinkerror: main.caracterizar()[f @ main.caracterizar(native method) @ main.main(main.java:10)
i think have done right, thought seems haven't.
here c function
/* * file: main.c * author: ocardonam * created on 14 de enero de 2013, 11:46 */ #include <stdio.h> #include <math.h> #include <string.h> #include <jni.h> #include <gsl/gsl_vector.h> #include <gsl/gsl_statistics.h> #include <gsl/gsl_blas.h> #include <gsl/gsl_linalg.h> #include "sizedata.h" #include "elementary_math_vec.h" #include "contar.h" #include "media.h" #include "main.h" jniexport jfloatarray jnicall java_main_caracterizar (jnienv *env, jclass class) { //variables int n_fil, n1, n2, i; char *filename; //features names double m,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11;//,f1,f2,f3,f4,f5,f6,f7,f8,f9,f10,f11; //load signal , define size filename = "reg_test.txt"; tamano(filename, &n1, &n2); n_fil = n1-2; gsl_vector * v = gsl_vector_alloc (n_fil); { file * f = fopen (filename, "r"); gsl_vector_fscanf (f, v); fclose (f); } // statistics features in time domain double data[n_fil],tmp[n_fil]; (i = 0; < n_fil; i++) { data[i] = gsl_vector_get(v,i); } //centered data m = gsl_stats_mean(data, 1, n_fil); (i = 0; < n_fil; i++) { data[i] = data[i]-m; gsl_vector_set(v,i,data[i]); } t1 = gsl_stats_mean(data, 1, n_fil); //mean t2 = gsl_stats_sd_m(data, 1, n_fil, t1); //standard deviation t3 = gsl_blas_dnrm2(v)/sqrt(n_fil);//rms t4 = gsl_stats_skew(data, 1, n_fil); //skewness t5 = gsl_stats_kurtosis(data, 1, n_fil) + 3; //kurtosis t6 = ch_t6(data,n_fil); t7 = ch_t7(data,n_fil); //absolute maximum t8 = t7/t3; //crest factor t9 = t7/t6; t10 = ch_t10(data,n_fil,t3);//shape factor t11 = ch_t11(data,n_fil,t7); //print vector jfloat res[1000]; jfloatarray jres; (i = 0; < n1-2; i++) { res[i] = (jfloat) gsl_vector_get(v, i); } gsl_vector_free (v); (*env)->setfloatarrayregion(env, jres, 0, n1-2, res); return jres; }
and here java code use invoke it:
public class main { public native float[] caracterizar(); static { system.loadlibrary("caracterizacion"); } public static void main(string args[]) { float ans[] = (new main()).caracterizar(); (int i=0 ; i<ans.length ; i++) system.out.println(ans[i]); } }
also, here i've used compile , link c program:
gcc -d__int64="long long" -c -i/cygdrive/c/program\ files/java/jdk1.7.0_11/include/ -i/cygdrive/c/program\ files/java/jdk1.7.0_11/include/win32/ -o main.o main.c
link:
gcc -shared -l/usr/local/lib -o caracterizacion.dll main.o -lgsl -lgslcblas -lm
i not sure mistake is. i've tested other jni programs , work fine, think problem way i'm including gsl library.
i hope can me. thank you
do not use cygwin gcc, unless want have program linked cygwin libraries. if need it, have package cygwin dlls package well.
if want running on windows, use mingw compiler (there package cygwin, create cygwin independent binary.) or msvc project. unless java cygwin enabled, not understand cygwin paths, native code will. become confusing. unless know need it, use cygwin scripting, use mingw-gcc compiler make binaries or use min lin's studio project.
Comments
Post a Comment