我下载并解压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
遵循@nm 的建议并得到了类似的错误
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
谢谢