linux下so动态库的编译以及使用

1.生成jni文件
javah Test(java文件)

2.首先编译.c文件生成.o中间文件

gcc -fPIC -D_REENTRANT jdk路径/include -I jdk路径/include/linux -c test.c

3.用.o文件编译so库 gcc -shared -o libtest.so test.o 如果有依赖其他.o 在test.o后加依赖的.o文件

4.编写java调用程序

public class Test{
static {
System.load("/home/lance/main.so"); //以绝对路径加载so文件
}
public native static int add(int x,int y); //java 代码中声明 native 方法
public static void main(String[] args){
System.out.println(add(1,1));
}
}

你可能感兴趣的:(linux,c)