一、以bsdiff.c和bspatch.c为例编译bsdiff.so
bsdiff.c和bspatch.c分别依赖bzip2
项目结构
步骤
使用BsDiff.java生成BsDiff.class文件
javac ivonhoe/spring/wrcenter/jni/BsDiff.java
生成BsDiff.h文件
javah ivonhoe.spring.wrcenter.jni.BsDiff
生成bspatch.o
gcc -fPIC -D_REENTRANT -IJAVA_INCLUDE1 -I/JAVA_INCLUDE2/ -I/Users/Ivonhoe/Downloads/VelocityDemo/src/main/java/jni/bzip2 -I/Users/Ivonhoe/Downloads/VelocityDemo/src/main/java/jni -c ./jni/bspatch.c
生成bsdiff.o
gcc -fPIC -D_REENTRANT -I/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include/darwin -I/Users/Ivonhoe/Downloads/VelocityDemo/src/main/java/jni/bzip2 -I/Users/Ivonhoe/Downloads/VelocityDemo/src/main/java/jni -c ./jni/bsdiff.c
将bzip2下的c文件生成相应的.o文件
gcc -fPIC -D_REENTRANT -I/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include -I/Library/Java/JavaVirtualMachines/jdk1.8.0_60.jdk/Contents/Home/include/darwin -I/Users/Ivonhoe/Downloads/VelocityDemo/src/main/java/jni/bzip2 -c ./jni/bzip2/*.c
生成so文件
*gcc .o -o bsdiff.so -shared -v
注:
JAVA_INCLUDE1:/usr/local/java/jdk1.7.0_40/include
JAVA_INCLUDE2:/usr/local/java/jdk1.7.0_40/include/linux
是 jdk 中的 jni.h 文件的所在目录,这两个目录需要大家替换为自己的对应路径BZIP2_INCLUDE:
参考文档:
http://www.cppblog.com/deane/articles/165216.html
http://blog.csdn.net/hongquan1991/article/details/12426615
http://codepub.cn/2015/05/19/How-to-load-dll-file-in-Java-Web-Project/