jna的简单测试
================>helloworld.h 文件:
================> helloworld.c 文件:
================> 编译方法1:生成libhello.so 文件
================>编译方法2:生成libhelloworld.so 文件
================>★★★★把生成的文件放到 /usr/lib64/ 下
================>java工程目录如下:
================> 测试代码如下:
================>测试结果如下:============================================

================>helloworld.h 文件:
#ifndef _HELLOWORLD_H #define _HELLOWORLD_H void print(); int add(int a, int b); #endif
================> helloworld.c 文件:
#include <stdio.h>
#include "helloworld.h"
int add(int first,int second){
return first+second;
}
void print(){
printf("helloworld....");
}
================> 编译方法1:生成libhello.so 文件
[root@localhost dev_src]# gcc -fpic -shared -o libhello.so helloworld.c
================>编译方法2:生成libhelloworld.so 文件
[root@localhost dev_src]# gcc -fPIC -c helloworld.c -o helloworld.o
[root@localhost dev_src]# gcc -fPIC -shared helloworld.o -o libhelloworld.so

================>★★★★把生成的文件放到 /usr/lib64/ 下

================>java工程目录如下:

================> 测试代码如下:
package com.wfg.test;
import com.sun.jna.Library;
import com.sun.jna.Native;
public class AlphaJna {
//继承Library,用于加载库文件
public interface Clibrary extends Library{
//加载libhello.so链接库
//Clibrary INSTANTCE = (Clibrary) Native.loadLibrary("hello", Clibrary.class);
//加载libhelloworld.so链接库
Clibrary INSTANTCE = (Clibrary) Native.loadLibrary("helloworld", Clibrary.class);
//此方法为链接库中的方法
void print();
int add(int a, int b);//此方法为链接库中的方法
}
public static void main(String[] args) {
//调用 c
Clibrary.INSTANTCE.print();
int x = Clibrary.INSTANTCE.add(3, 5);
System.out.println(x);
System.out.println("==============");
}
}
================>测试结果如下:
浙公网安备 33010602011771号