loop.c
#include<stdio.h> #include<stdlib.h> #include<math.h> #include"loop.h" int loop2(double* v1, double* v2,int n){ double tmp=0; int i; for(i=0;i<n;i++){ tmp=tmp+v1[i]*v2[i]; } return tmp; }
loop.h
#ifndef loop #define loop #ifdef _cplusplus extern "C" { #endif int loop2(double* v1, double* v2, int n); #ifdef _cplusplus } #endif #endif
c_test3.scala
import com.sun.jna.Library; import com.sun.jna.Native; import com.sun.jna.NativeLibrary; object c_test3{ def main(args: Array[String]): Unit={ val lib=NativeLibrary.getInstance("loop.dll"); val func=lib.getFunction("loop2"); val k=func.invokeDouble(Array(Array(1.0,2.0,3.0),Array(2.0,3.0,4.0),3.asInstanceOf[java.lang.Integer])); println(k); } }
scalac -cp jna.jar c_test3.scala
出力
20.0