I downloaded and unpacked xerces-c-3.1.1-x86_64-linux-gcc-3.4.tar.gz
,
So I have
$ 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
Then I have my Makefile
as
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
The make
command works fine.
$ 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
But the result executable ReadXML
is not as "static" as I hope it to be. It prints
./ReadXML: error while loading shared libraries: libxerces-c-3.1.so: cannot open shared object file: No such file or directory
until I setenv LD_LIBRARY_PATH /users/jdyu/labs/xerces/xerces-c-3.1.1-x86_64-linux-gcc-3.4/lib
. Obviously, the xerces-c is linked dynamically.
Why the -Bstatic
doesn't help me to create a static linked executable?
Update 2013.05.13
Followed @n.m. 's advise and got error like
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
And with LDFLAGS += -Wl,-Bstatic -lxerces-c -Wl,-Bdynamic
, I got
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
Thanks
Thanks for @n.m. 's help. I got the answer after studying a little from libtool
. Let me post the answer here.
Finally my working Makefile
looks like
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
And then the result comes as
$ 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
and
$ 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!
The xerces-c
linked statically.
And libtool
can make it even more static. Append the make file with
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
and got the result
$ 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
and
$ 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!
There is far more in the libtool
to learn for me. I tried hard reading libtool
code, only to failed to figure out how it coverts the temporary dynamic .libs/ReadXML_
to the final ReadXML_
which is totally static linked.
Anyway, the answer of this question is found.
Thanks again, @n.m.