共享库编译,但具有未定义的符号

时间:2011-01-04 17:02:06

标签: java c++ c shared-libraries xerces

我得到的内容如下:

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

的静态和共享库中进行了链接

编辑:当我尝试运行它时,我现在实际上得到了这个:

    $ 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?

2 个答案:

答案 0 :(得分:3)

我认为原因在于您链接到库的方式:

-L../../lib/Linux/libxerces-c-3.1.so

-L选项用于指定库的搜索目录,库本身使用-l选项指定。

应该是:

-L../../lib/Linux/ -lxerces-c-3.1

或只是将lib放在没有-L

的情况下
../../lib/Linux/libxerces-c-3.1.so

修改

如果定义了这样的符号,您可以查看nm libxerces-c-3.1.so|grep ParseException(对于libxerces-c.a也是如此)。还要确保用于编译的libxerces(包括路径-I)和用于链接的libxerces是相同的。

链接库时传递-Wl,-no-undefined将有助于您在链接期间看到未定义的符号,而不是稍后尝试加载库。

答案 1 :(得分:0)

您再次链接共享库,例如-L ../../ lib / Linux / libxerces-c-3.1.so除非包含所有这些内容,否则无法加载需要它们的共享库。