当前位置: 技术问答>linux和unix
gcc编译动态连接库一问
来源: 互联网 发布时间:2014-12-12
本文导语: 在Linux下面,Interface.java中声明了一个native方法在rrd_jinterface.c中实现, rrd_jinterface.c的头文件中包括了jni.h(在/usr/java/j2sdk1.4.0/include中)、jin_md.h(在/usr/java/j2sdk1.4.0/include/linux中)、rrd.h,rrd.h中声名了 rrd_create、rrd_upda...
在Linux下面,Interface.java中声明了一个native方法在rrd_jinterface.c中实现,
rrd_jinterface.c的头文件中包括了jni.h(在/usr/java/j2sdk1.4.0/include中)、jin_md.h(在/usr/java/j2sdk1.4.0/include/linux中)、rrd.h,rrd.h中声名了
rrd_create、rrd_update、rrd_fetch等多个函数,这些函数已被编译为librrd.a文件
放在/usr/local/rrdtool-1.0.33/lib下面,
问题1:
我用下面的命令编译rrd_jinterface.c文件:
gcc -I/usr/java/j2sdk1.4.0/include -I/usr/java/j2sdk1.4.0/include/linux -L/usr/local/rrdtool-1.0.33/lib -lrrd -shared rrd_jinterface.c -o libjrrd.so
这时没有任何出错信息,生成libjrrd.so文件,但是当使用java Interface执行程序时出错,显示:java.lang.UnsatisfiedLinkError: /home/Landy/study/libjrrd.so: /home/Landy/study/libjrrd.so: undefined symbol: rrd_create
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1480)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1404)
at java.lang.Runtime.loadLibrary0(Runtime.java:772)
at java.lang.System.loadLibrary(System.java:832)
at Interface.(Interface.java:151)
at Interface.init(Interface.java:118)
at Interface.reload(Interface.java:136)
at Interface.main(Interface.java:178)
这是不是我生成的动态连接库有问题?还是其他问题?
问题2
如果将问题1中-shared选项换成-static,则执行gcc就出错,显示:
usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function `_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18): undefined reference to `main'
/tmp/ccPnYhV2.o: In function `Java_Interface_launch':
/tmp/ccPnYhV2.o(.text+0x673): undefined reference to `rrd_create'
/tmp/ccPnYhV2.o(.text+0x74b): undefined reference to `rrd_update'
/tmp/ccPnYhV2.o(.text+0x838): undefined reference to `rrd_fetch'
/tmp/ccPnYhV2.o(.text+0xdaa): undefined reference to `rrd_set_error'
/tmp/ccPnYhV2.o(.text+0xe03): undefined reference to `rrd_test_error'
/tmp/ccPnYhV2.o(.text+0xe6c): undefined reference to `rrd_get_error'
/tmp/ccPnYhV2.o(.text+0xebb): undefined reference to `rrd_clear_error'
collect2: ld returned 1 exit status
这是什么问题?是不是路径设置不对?还是其它问题?应该如何修改?
在下刚刚接触gcc、jni,希望大家多多帮助。
rrd_jinterface.c的头文件中包括了jni.h(在/usr/java/j2sdk1.4.0/include中)、jin_md.h(在/usr/java/j2sdk1.4.0/include/linux中)、rrd.h,rrd.h中声名了
rrd_create、rrd_update、rrd_fetch等多个函数,这些函数已被编译为librrd.a文件
放在/usr/local/rrdtool-1.0.33/lib下面,
问题1:
我用下面的命令编译rrd_jinterface.c文件:
gcc -I/usr/java/j2sdk1.4.0/include -I/usr/java/j2sdk1.4.0/include/linux -L/usr/local/rrdtool-1.0.33/lib -lrrd -shared rrd_jinterface.c -o libjrrd.so
这时没有任何出错信息,生成libjrrd.so文件,但是当使用java Interface执行程序时出错,显示:java.lang.UnsatisfiedLinkError: /home/Landy/study/libjrrd.so: /home/Landy/study/libjrrd.so: undefined symbol: rrd_create
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1480)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1404)
at java.lang.Runtime.loadLibrary0(Runtime.java:772)
at java.lang.System.loadLibrary(System.java:832)
at Interface.(Interface.java:151)
at Interface.init(Interface.java:118)
at Interface.reload(Interface.java:136)
at Interface.main(Interface.java:178)
这是不是我生成的动态连接库有问题?还是其他问题?
问题2
如果将问题1中-shared选项换成-static,则执行gcc就出错,显示:
usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o: In function `_start':
/usr/lib/gcc-lib/i386-redhat-linux/2.96/../../../crt1.o(.text+0x18): undefined reference to `main'
/tmp/ccPnYhV2.o: In function `Java_Interface_launch':
/tmp/ccPnYhV2.o(.text+0x673): undefined reference to `rrd_create'
/tmp/ccPnYhV2.o(.text+0x74b): undefined reference to `rrd_update'
/tmp/ccPnYhV2.o(.text+0x838): undefined reference to `rrd_fetch'
/tmp/ccPnYhV2.o(.text+0xdaa): undefined reference to `rrd_set_error'
/tmp/ccPnYhV2.o(.text+0xe03): undefined reference to `rrd_test_error'
/tmp/ccPnYhV2.o(.text+0xe6c): undefined reference to `rrd_get_error'
/tmp/ccPnYhV2.o(.text+0xebb): undefined reference to `rrd_clear_error'
collect2: ld returned 1 exit status
这是什么问题?是不是路径设置不对?还是其它问题?应该如何修改?
在下刚刚接触gcc、jni,希望大家多多帮助。
|
1.
gcc -I/usr/java/j2sdk1.4.0/include -I/usr/java/j2sdk1.4.0/include/linux -L/usr/local/rrdtool-1.0.33/lib -lrrd -shared -Wl,-soname,libjrrd.so rrd_jinterface.c -o libjrrd.so.0.0.1 -ldl
试一试
2。加-c选项
gcc -I/usr/java/j2sdk1.4.0/include -I/usr/java/j2sdk1.4.0/include/linux -L/usr/local/rrdtool-1.0.33/lib -lrrd -shared -Wl,-soname,libjrrd.so rrd_jinterface.c -o libjrrd.so.0.0.1 -ldl
试一试
2。加-c选项