无法使用g ++与Xerces-c静态库链接

时间:2013-05-10 10:26:11

标签: c++ g++ xerces libtool xerces-c

我已下载并解压缩xerces-c-3.1.1-x86_64-linux-gcc-3.4.tar.gz, 所以我有

$ ls -1 /users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib
libxerces-c-3.1.so
libxerces-c.a
libxerces-c.la
libxerces-c.so
pkgconfig

然后我将Makefile作为

CC := gcc
CXX := g++
CXXFLAGS := -O3
LDFLAGS := -O3

CXXFLAGS += -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include
CXXFLAGS += -DXERCES_STATIC_LIBRARY
LDFLAGS += -L/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib
LDFLAGS += -Bstatic
LDFLAGS += -lxerces-c

ReadXML: DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o

clean:
    @rm -rvf *.o ReadXML

make命令工作正常。

$ make -B
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o ReadXML.o ReadXML.cpp
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o DOM.o DOM.cpp
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o MyDOMErrorHandler.o MyDOMErrorHandler.cpp
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o MyXMLString.o MyXMLString.cpp
gcc -O3 -L/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib -Bstatic -lxerces-c  ReadXML.o DOM.o MyDOMErrorHandler.o MyXMLString.o   -o ReadXML

但结果可执行文件ReadXML并不像我希望的那样“静态”。它打印

./ReadXML: error while loading shared libraries: libxerces-c-3.1.so: cannot open shared object file: No such file or directory

直到我setenv LD_LIBRARY_PATH /users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib。显然,xerces-c是动态链接的。

为什么-Bstatic无法帮助我创建静态链接可执行文件?


更新2013.05.13

跟着@​​n.m.建议并得到像

这样的错误
 gcc -O3 -L/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib -Wl,-Bstatic -lxerces-c  ReadXML.o DOM.o MyDOMErrorHandler.o MyXMLString.o   -o ReadXML
/usr/bin/ld: cannot find -lgcc_s
collect2: ld returned 1 exit status
make: *** [ReadXML] Error 1

使用LDFLAGS += -Wl,-Bstatic -lxerces-c -Wl,-Bdynamic,我得到了

g++ -O3 -L/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib -Wl,-Bstatic -lxerces-c -Wl,-Bdynamic -o ReadXML DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o
DOM.o: In function `parseXMLFile(char const*)':
DOM.cpp:(.text+0x3a6): undefined reference to `xercesc_3_1::DOMImplementationRegistry::getDOMImplementation(unsigned short const*)'
DOM.cpp:(.text+0x3b3): undefined reference to `xercesc_3_1::XMLPlatformUtils::fgMemoryManager'
DOM.cpp:(.text+0x3dc): undefined reference to `xercesc_3_1::XMLUni::fgXercesHandleMultipleImports'
DOM.cpp:(.text+0x3f0): undefined reference to `xercesc_3_1::XMLUni::fgXercesSchema'
DOM.cpp:(.text+0x404): undefined reference to `xercesc_3_1::XMLUni::fgXercesSchemaFullChecking'
DOM.cpp:(.text+0x42f): undefined reference to `xercesc_3_1::XMLUni::fgDOMErrorHandler'
... # Many other lines
ReadXML.cpp:(.text+0x1eb): undefined reference to `xercesc_3_1::XMLString::release(char**, xercesc_3_1::MemoryManager*)'
ReadXML.o:(.gcc_except_table+0x28): undefined reference to `typeinfo for xercesc_3_1::XMLException'
collect2: ld returned 1 exit status
make: *** [ReadXML] Error 1

由于

1 个答案:

答案 0 :(得分:3)

感谢@n.m.的帮助。我从libtool学习了一点后得到了答案。让我在这里发布答案。

最后我的工作Makefile看起来像

CC := gcc
CXX := g++
LD := g++
CXXFLAGS := -O3
LDFLAGS := -O3

CXXFLAGS += -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include
CXXFLAGS += -DXERCES_STATIC_LIBRARY
LDFLAGS += -L/users/jdyu/labs/xerces/xerces-c-3.1.1/lib

ReadXML: DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o
    $(LD) $(LDFLAGS) -o $@ $^ -Wl,-Bstatic -lxerces-c -Wl,-Bdynamic -lnsl -lpthread -lcurl
test: ReadXML
    ldd $(abspath $<)
    $(abspath $<) /users/jdyu/labs/xerces/xerces-c-3.1.1/samples/data/personal.xml

然后结果出现

$ make -B ReadXML
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o DOM.o DOM.cpp
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o MyDOMErrorHandler.o MyDOMErrorHandler.cpp
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o MyXMLString.o MyXMLString.cpp
g++ -O3 -I/users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/include -DXERCES_STATIC_LIBRARY   -c -o ReadXML.o ReadXML.cpp
g++ -O3 -L/users/jdyu/labs/xerces/xerces-c-3.1.1/lib -o ReadXML DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o -Wl,-Bstatic -lxerces-c -Wl,-Bdynamic -lnsl -lpthread -lcurl

$ make test
ldd /users/jdyu/labs/xerces/ReadXML/ReadXML
    linux-vdso.so.1 =>  (0x00007fff6fdd6000)
    libnsl.so.1 => /lib64/libnsl.so.1 (0x0000003b5e200000)
    libpthread.so.0 => /lib64/libpthread.so.0 (0x0000003b4ea00000)
    libcurl.so.4 => /usr/lib64/libcurl.so.4 (0x0000003b5f200000)
    libstdc++.so.6 => /usr/lib64/libstdc++.so.6 (0x0000003b54e00000)
    libm.so.6 => /lib64/libm.so.6 (0x0000003b4e200000)
    libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x0000003b54200000)
    libc.so.6 => /lib64/libc.so.6 (0x0000003b4de00000)
    /lib64/ld-linux-x86-64.so.2 (0x0000003b4d600000)
    libidn.so.11 => /lib64/libidn.so.11 (0x0000003b5d200000)
    libldap-2.4.so.2 => /usr/lib64/libldap-2.4.so.2 (0x0000003b60000000)
    librt.so.1 => /lib64/librt.so.1 (0x0000003b4f200000)
    libgssapi_krb5.so.2 => /lib64/libgssapi_krb5.so.2 (0x0000003b56a00000)
    libkrb5.so.3 => /lib64/libkrb5.so.3 (0x0000003b55e00000)
    libk5crypto.so.3 => /lib64/libk5crypto.so.3 (0x0000003b56600000)
    libcom_err.so.2 => /lib64/libcom_err.so.2 (0x0000003b55600000)
    libz.so.1 => /lib64/libz.so.1 (0x0000003b4ee00000)
    libssl3.so => /usr/lib64/libssl3.so (0x0000003b61000000)
    libsmime3.so => /usr/lib64/libsmime3.so (0x0000003b60800000)
    libnss3.so => /usr/lib64/libnss3.so (0x0000003b5ea00000)
    libnssutil3.so => /usr/lib64/libnssutil3.so (0x0000003b60400000)
    libplds4.so => /lib64/libplds4.so (0x0000003b5ca00000)
    libplc4.so => /lib64/libplc4.so (0x0000003b5c600000)
    libnspr4.so => /lib64/libnspr4.so (0x0000003b5e600000)
    libdl.so.2 => /lib64/libdl.so.2 (0x0000003b4e600000)
    libssh2.so.1 => /usr/lib64/libssh2.so.1 (0x0000003b5de00000)
    liblber-2.4.so.2 => /usr/lib64/liblber-2.4.so.2 (0x0000003b5fc00000)
    libresolv.so.2 => /lib64/libresolv.so.2 (0x0000003b4fa00000)
    libsasl2.so.2 => /usr/lib64/libsasl2.so.2 (0x0000003b5f800000)
    libssl.so.10 => /usr/lib64/libssl.so.10 (0x0000003b57a00000)
    libcrypto.so.10 => /usr/lib64/libcrypto.so.10 (0x0000003b55200000)
    libkrb5support.so.0 => /lib64/libkrb5support.so.0 (0x0000003b55a00000)
    libkeyutils.so.1 => /lib64/libkeyutils.so.1 (0x0000003b56200000)
    libcrypt.so.1 => /lib64/libcrypt.so.1 (0x0000003b5d600000)
    libselinux.so.1 => /lib64/libselinux.so.1 (0x0000003b4f600000)
    libfreebl3.so => /lib64/libfreebl3.so (0x0000003b5da00000)
/users/jdyu/labs/xerces/ReadXML/ReadXML /users/jdyu/labs/xerces/xerces-c-3.1.1/samples/data/personal.xml
Parsing XML file '/users/jdyu/labs/xerces/xerces-c-3.1.1/samples/data/personal.xml' ...
Parsing finished in 1 millisecond(s).
37 nodes found.
XML platform terminated successfully!

xerces-c静态链接。


libtool可以使它更加静态。使用

附加make文件
ReadXML_: DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o
    libtool --tag=CXX --mode=link $(LD) $(LDFLAGS) -o $@ $^ -lxerces-c -lnsl -lpthread -lcurl
test_: ReadXML_
    -ldd $(abspath $<)
    $(abspath $<) /users/jdyu/labs/xerces/xerces-c-3.1.1/samples/data/personal.xml

得到了结果

$ make ReadXML_
libtool --tag=CXX --mode=link g++ -O3 -L/users/jdyu/labs/xerces/xerces-c-3.1.1/lib -o ReadXML_ DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o -lxerces-c -lnsl -lpthread -lcurl
libtool: link: g++ -O3 -o .libs/ReadXML_ DOM.o MyDOMErrorHandler.o MyXMLString.o ReadXML.o  -L/users/jdyu/labs/xerces/xerces-c-3.1.1/lib /users/jdyu/labs/xerces/xerces-c-3.1.1/lib/libxerces-c.so -lnsl -lpthread -lcurl -Wl,-rpath -Wl,/usr/local/lib

$ make test_
ldd /users/jdyu/labs/xerces/ReadXML/ReadXML_
    not a dynamic executable
make: [test_] Error 1 (ignored)
/users/jdyu/labs/xerces/ReadXML/ReadXML_ /users/jdyu/labs/xerces/xerces-c-3.1.1/samples/data/personal.xml
Parsing XML file '/users/jdyu/labs/xerces/xerces-c-3.1.1/samples/data/personal.xml' ...
Parsing finished in 1 millisecond(s).
37 nodes found.
XML platform terminated successfully!

libtool还有更多内容可供我学习。我努力阅读libtool代码,但却未能弄清楚它是如何将临时动态.libs/ReadXML_转换为完全静态链接的最终ReadXML_。 无论如何,找到了这个问题的答案。

再次感谢,@ n.m。