共享库编译,但有未定义的符号
我得到类似以下内容:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no viewerNativeDLL in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at viewer.SarBufferedImage.<clinit>(SarBufferedImage.java:356)
at viewer.viewerJPanel.<init>(viewerJPanel.java:34)
at viewer.ViewerView.initComponents(ViewerView.java:457)
at viewer.ViewerView.<init>(ViewerView.java:33)
at viewer.ViewerApp.main(ViewerApp.java:21)
即使我已将 LD_LIBRARY_PATH 设置为构建共享对象的路径,也是如此。
我构建的共享对象是错误的吗?这是我用来构建共享库的 makefile:
CPP = g++
CFLAGS = -O3 -Wall -fopenmp -fPIC
INC = -I ../../VC/readers/ -I../../VC/writers -I../../VC/common/ -I /home/dcole/software/xerces-c-3.1.1/src -I /home/dcole/software/java/jdk1.6.0_23/include -I /home/dcole/software/java/jdk1.6.0_23/include/linux
LIB2 = -L/usr/lib64/ -L../../lib/IDT.a -L../../lib/Linux/libxerces-c.a -L../../lib/Linux/libxerces-c-3.1.so -lcurl -lidn -ldl -lssl -L../../lib/Linux/libfftw3f.a -lpthread
all: ViewerNative.o
${CPP} ${CFLAGS} -shared -o libviewerNativeDLL.so ViewerNative.o ${LIB2}
ViewerNative.o: ViewerNative.cpp viewer_SarBufferedImage.h
${CPP} ${INC} ${CFLAGS} -c ViewerNative.cpp ${LIB2}
我创建viewerNativeDLL.so 的方式是否遗漏了一些东西?
如果我执行 ldd-d libviewerNativeDLL.so 我得到这个:
undefined symbol: _ZTIN11xercesc_3_117SAXParseExceptionE (./libviewerNativeDLL.so)
undefined symbol: _ZN11xercesc_3_117SAXParseExceptionD1Ev (./libviewerNativeDLL.so)
linux-vdso.so.1 => (0x00007fff41f21000)
libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x00007fc30bcd4000)
libidn.so.11 => /lib64/libidn.so.11 (0x00007fc30baa2000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fc30b89d000)
libssl.so.8 => /usr/lib64/libssl.so.8 (0x00007fc30b649000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc30b42d000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fc30b122000)
libm.so.6 => /lib64/libm.so.6 (0x00007fc30ae9f000)
libgomp.so.1 => /usr/lib64/libgomp.so.1 (0x00007fc30ac92000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fc30aa77000)
libc.so.6 => /lib64/libc.so.6 (0x00007fc30a709000)
libldap-2.4.so.2 => /usr/lib64/libldap-2.4.so.2 (0x00007fc30a4c4000)
librt.so.1 => /lib64/librt.so.1 (0x00007fc30a2bb000)
libgssapi_krb5.so.2 => /usr/lib64/libgssapi_krb5.so.2 (0x00007fc30a090000)
libkrb5.so.3 => /usr/lib64/libkrb5.so.3 (0x00007fc309df4000)
libk5crypto.so.3 => /usr/lib64/libk5crypto.so.3 (0x00007fc309bcd000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fc3099ca000)
libz.so.1 => /lib64/libz.so.1 (0x00007fc3097b5000)
libssl3.so => /lib64/libssl3.so (0x00007fc309581000)
libsmime3.so => /lib64/libsmime3.so (0x00007fc309357000)
libnss3.so => /lib64/libnss3.so (0x00007fc309024000)
libnssutil3.so => /lib64/libnssutil3.so (0x00007fc308e05000)
libplds4.so => /lib64/libplds4.so (0x00007fc308c02000)
libplc4.so => /lib64/libplc4.so (0x00007fc3089fe000)
libnspr4.so => /lib64/libnspr4.so (0x00007fc3087c1000)
libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x00007fc30859a000)
/lib64/ld-linux-x86-64.so.2 (0x0000003e34a00000)
libcrypto.so.8 => /usr/lib64/libcrypto.so.8 (0x00007fc308215000)
liblber-2.4.so.2 => /usr/lib64/liblber-2.4.so.2 (0x00007fc308005000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fc307dec000)
libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00007fc307bd1000)
libkrb5support.so.0 => /usr/lib64/libkrb5support.so.0 (0x00007fc3079c8000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fc3077c6000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fc30758e000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fc307370000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fc307113000)
所以我可以清楚地看到它们丢失了,但为什么我没有收到任何错误?解决办法是什么?我已经从 xerces edit 链接到静态库和共享库
:当我尝试运行它时,我现在实际上得到了这个:
$ java -Djava.library.path=/home/dcole/IM/work/ext/VC.viewer/viewerNativeDLL/ -jar viewer.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/dcole/IM/work/ext/VC.viewer/viewerNativeDLL/libviewerNativeDLL.so: /home/dcole/IM/work/ext/VC.viewer/viewerNativeDLL/libviewerNativeDLL.so: undefined symbol: _ZTIN11xercesc_3_117SAXParseExceptionE
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at viewer.SarBufferedImage.<clinit>(SarBufferedImage.java:356)
at viewer.viewerJPanel.<init>(viewerJPanel.java:34)
at viewer.ViewerView.initComponents(ViewerView.java:457)
at viewer.ViewerView.<init>(ViewerView.java:33)
at viewer.ViewerApp.main(ViewerApp.java:21)
edit2:
按照下面的建议,我更改了我链接到库的说法,这给了我这个命令和输出:
g++ -O3 -Wall -fopenmp -fPIC -shared -o libviewerNativeDLL.so ViewerNative.o -L/usr/lib64/ ../../lib/IDT.a ../../lib/Linux/libxerces-c.a ../../lib/Linux/libxerces-c-3.1.so -lcurl -lidn -ldl -lssl ../../lib/Linux/libfftw3f.a -lpthread
/usr/bin/ld: ../../lib/Linux/libxerces-c.a(PlatformUtils.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
../../lib/Linux/libxerces-c.a: could not read symbols: Bad value
我想我必须用 fPIC 重新编译 xerces?
I am getting something like the following:
Exception in thread "main" java.lang.UnsatisfiedLinkError: no viewerNativeDLL in java.library.path
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1734)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at viewer.SarBufferedImage.<clinit>(SarBufferedImage.java:356)
at viewer.viewerJPanel.<init>(viewerJPanel.java:34)
at viewer.ViewerView.initComponents(ViewerView.java:457)
at viewer.ViewerView.<init>(ViewerView.java:33)
at viewer.ViewerApp.main(ViewerApp.java:21)
This is even though I have set LD_LIBRARY_PATH to the path where I built my shared object.
Did I build that shared object wrong? This is the makefile I used to build the shared lib:
CPP = g++
CFLAGS = -O3 -Wall -fopenmp -fPIC
INC = -I ../../VC/readers/ -I../../VC/writers -I../../VC/common/ -I /home/dcole/software/xerces-c-3.1.1/src -I /home/dcole/software/java/jdk1.6.0_23/include -I /home/dcole/software/java/jdk1.6.0_23/include/linux
LIB2 = -L/usr/lib64/ -L../../lib/IDT.a -L../../lib/Linux/libxerces-c.a -L../../lib/Linux/libxerces-c-3.1.so -lcurl -lidn -ldl -lssl -L../../lib/Linux/libfftw3f.a -lpthread
all: ViewerNative.o
${CPP} ${CFLAGS} -shared -o libviewerNativeDLL.so ViewerNative.o ${LIB2}
ViewerNative.o: ViewerNative.cpp viewer_SarBufferedImage.h
${CPP} ${INC} ${CFLAGS} -c ViewerNative.cpp ${LIB2}
Am I missing something with the way I am creating viewerNativeDLL.so?
If I do a ldd-d libviewerNativeDLL.so I get this:
undefined symbol: _ZTIN11xercesc_3_117SAXParseExceptionE (./libviewerNativeDLL.so)
undefined symbol: _ZN11xercesc_3_117SAXParseExceptionD1Ev (./libviewerNativeDLL.so)
linux-vdso.so.1 => (0x00007fff41f21000)
libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x00007fc30bcd4000)
libidn.so.11 => /lib64/libidn.so.11 (0x00007fc30baa2000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007fc30b89d000)
libssl.so.8 => /usr/lib64/libssl.so.8 (0x00007fc30b649000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007fc30b42d000)
libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x00007fc30b122000)
libm.so.6 => /lib64/libm.so.6 (0x00007fc30ae9f000)
libgomp.so.1 => /usr/lib64/libgomp.so.1 (0x00007fc30ac92000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007fc30aa77000)
libc.so.6 => /lib64/libc.so.6 (0x00007fc30a709000)
libldap-2.4.so.2 => /usr/lib64/libldap-2.4.so.2 (0x00007fc30a4c4000)
librt.so.1 => /lib64/librt.so.1 (0x00007fc30a2bb000)
libgssapi_krb5.so.2 => /usr/lib64/libgssapi_krb5.so.2 (0x00007fc30a090000)
libkrb5.so.3 => /usr/lib64/libkrb5.so.3 (0x00007fc309df4000)
libk5crypto.so.3 => /usr/lib64/libk5crypto.so.3 (0x00007fc309bcd000)
libcom_err.so.2 => /lib64/libcom_err.so.2 (0x00007fc3099ca000)
libz.so.1 => /lib64/libz.so.1 (0x00007fc3097b5000)
libssl3.so => /lib64/libssl3.so (0x00007fc309581000)
libsmime3.so => /lib64/libsmime3.so (0x00007fc309357000)
libnss3.so => /lib64/libnss3.so (0x00007fc309024000)
libnssutil3.so => /lib64/libnssutil3.so (0x00007fc308e05000)
libplds4.so => /lib64/libplds4.so (0x00007fc308c02000)
libplc4.so => /lib64/libplc4.so (0x00007fc3089fe000)
libnspr4.so => /lib64/libnspr4.so (0x00007fc3087c1000)
libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x00007fc30859a000)
/lib64/ld-linux-x86-64.so.2 (0x0000003e34a00000)
libcrypto.so.8 => /usr/lib64/libcrypto.so.8 (0x00007fc308215000)
liblber-2.4.so.2 => /usr/lib64/liblber-2.4.so.2 (0x00007fc308005000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007fc307dec000)
libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x00007fc307bd1000)
libkrb5support.so.0 => /usr/lib64/libkrb5support.so.0 (0x00007fc3079c8000)
libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x00007fc3077c6000)
libcrypt.so.1 => /lib64/libcrypt.so.1 (0x00007fc30758e000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007fc307370000)
libfreebl3.so => /lib64/libfreebl3.so (0x00007fc307113000)
So I can clearly see they are missing, but how come I dont get any errors? What is the fix? I have linked in the static and shared libraries from xerces
edit: I am now actually getting this when I try to run it:
$ java -Djava.library.path=/home/dcole/IM/work/ext/VC.viewer/viewerNativeDLL/ -jar viewer.jar
Exception in thread "main" java.lang.UnsatisfiedLinkError: /home/dcole/IM/work/ext/VC.viewer/viewerNativeDLL/libviewerNativeDLL.so: /home/dcole/IM/work/ext/VC.viewer/viewerNativeDLL/libviewerNativeDLL.so: undefined symbol: _ZTIN11xercesc_3_117SAXParseExceptionE
at java.lang.ClassLoader$NativeLibrary.load(Native Method)
at java.lang.ClassLoader.loadLibrary0(ClassLoader.java:1803)
at java.lang.ClassLoader.loadLibrary(ClassLoader.java:1728)
at java.lang.Runtime.loadLibrary0(Runtime.java:823)
at java.lang.System.loadLibrary(System.java:1028)
at viewer.SarBufferedImage.<clinit>(SarBufferedImage.java:356)
at viewer.viewerJPanel.<init>(viewerJPanel.java:34)
at viewer.ViewerView.initComponents(ViewerView.java:457)
at viewer.ViewerView.<init>(ViewerView.java:33)
at viewer.ViewerApp.main(ViewerApp.java:21)
edit2:
following the suggestion below, I changed the say I was linking to the libraries, which gives me this command and output:
g++ -O3 -Wall -fopenmp -fPIC -shared -o libviewerNativeDLL.so ViewerNative.o -L/usr/lib64/ ../../lib/IDT.a ../../lib/Linux/libxerces-c.a ../../lib/Linux/libxerces-c-3.1.so -lcurl -lidn -ldl -lssl ../../lib/Linux/libfftw3f.a -lpthread
/usr/bin/ld: ../../lib/Linux/libxerces-c.a(PlatformUtils.o): relocation R_X86_64_32 against `.rodata.str1.8' can not be used when making a shared object; recompile with -fPIC
../../lib/Linux/libxerces-c.a: could not read symbols: Bad value
I guess I will have to recompile xerces with fPIC?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为原因是链接到库的方式:
-L
选项用于指定库的搜索目录,而库本身是使用-l
指定的选项。它可能应该是:
或者只是将库放在没有
-L
编辑:的
情况下,您可以使用
nm libxerces-c-3.1.so|grep ParseException
(与 libxerces-ca 相同)如果定义了这样的符号。还要确保用于编译的 libxerces(包括路径 -I)和用于链接的 libxerces 相同。链接库时传递
-Wl,-no-undefined
将帮助您在链接期间看到未定义的符号,而不是稍后尝试加载库。I think the reason is the way you link to the libraries:
-L
option is used to specify the search directories for the libraries, and the libraries themselves are specified using-l
option.It should probably be:
or just put the lib without
-L
edit:
You may check with
nm libxerces-c-3.1.so|grep ParseException
(same for libxerces-c.a) if there's such symbol defined. Make sure also that libxerces that you use for compilation (include paths -I) and the one you use for linking is the same.Passing
-Wl,-no-undefined
when linking your library will help you see undefined symbols during linking rather than trying to load the library later.您再次链接共享库,例如 -L../../lib/Linux/libxerces-c-3.1.so 除非包含所有这些,否则无法加载需要它们的共享库。
You are linking again shared libraries, e.g. -L../../lib/Linux/libxerces-c-3.1.so Unless all these are included, your shared library which needs them cannot load.