Java JNI编译小记

  一直都做Android开发,而且编译JNI代码都是用Google提供的NDK,今天做个J2SE平台的JNI,发现离开Android平台还多少不习惯了,编译so库都弄了半天,记下编译命令,以备后用。编译前提安装JDK和gcc/g++环境,我用的是gcc。步骤如下:


1.编译Java源代码
javac FifoTest.java

2. 生成*.H头文件
javah FifoTest.java

3.实现*.C文件,如FifoTest.c
# coding...

4. 编译*.c文件生成 *.o文件,这里注意JDK路径
gcc -c FifoTest.c -I./ -I/usr/lib/jvm/java-1.6.0-openjdk/include -I/usr/lib/jvm/java-1.6.0-openjdk/include/linux -o fifojni.o
(PS: -I optional with the jdk include file, you can find the include using this "locate include | grep java" to get the location of your jdk include)

#64位需要-fPIC选项
gcc -fPIC -c FifoTest.c -I./ -I/usr/lib/jvm/java-6-sun-1.6.0.26/include -I/usr/lib/jvm/java-6-sun-1.6.0.26/include/linux -o fifojni.o

5. 编译*.o 文件生成 *.so file
gcc -rdynamic -shared fifojni.o -o libfifo-jni.so



你可能感兴趣的:(其他)