基于boost的程序的静态链接——zlib问题

Static linking for boost-based program -- zlib problem

本文关键字:问题 zlib 链接 静态 boost 程序 基于      更新时间:2023-10-16

我正在尝试构建一个使用Boost Iostreams库的程序,并创建一个静态链接的可执行文件。我在RHEL7和Ubuntu 18.04上都尝试过,但都出现了类似的错误。编译器是GCC。在Ubuntu上:

$ g++ -o demultiplexer.static demultiplexer.cpp -O3 -std=c++11 -static -lz -lboost_iostreams
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_iostreams.a(zlib.o): In function `boost::iostreams::detail::zlib_base::after(char const*&, char*&, bool)':
(.text+0xf8): undefined reference to `crc32'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_iostreams.a(zlib.o): In function `boost::iostreams::detail::zlib_base::reset(bool, bool)':
(.text+0x171): undefined reference to `deflateReset'
(.text+0x186): undefined reference to `inflateEnd'
(.text+0x199): undefined reference to `inflateReset'
(.text+0x1b1): undefined reference to `deflateEnd'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_iostreams.a(zlib.o): In function `boost::iostreams::detail::zlib_base::do_init(boost::iostreams::zlib_params const&, bool, void* (*)(void*, unsigned int, unsigned int), void (*)(void*, void*), void*)':
(.text+0x394): undefined reference to `inflateInit2_'
(.text+0x3e2): undefined reference to `deflateInit2_'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_iostreams.a(zlib.o): In function `boost::iostreams::detail::zlib_base::xdeflate(int)':
(.text+0x144): undefined reference to `deflate'
/usr/lib/gcc/x86_64-linux-gnu/7/../../../x86_64-linux-gnu/libboost_iostreams.a(zlib.o): In function `boost::iostreams::detail::zlib_base::xinflate(int)':
(.text+0x154): undefined reference to `inflate'
collect2: error: ld returned 1 exit status

怎么了,我该如何防止这些链接器错误?尝试的替代方案:

$ g++ -o demultiplexer.static demultiplexer.cpp -O3 -std=c++11 /usr/lib/x86_64-linux-gnu/libz.a  /usr/lib/x86_64-linux-gnu/libboost_iostreams.a 
(same as above)
$ g++ -o demultiplexer.static demultiplexer.cpp -O3 -std=c++11 -Wl,-Bstatic -lz -lboost_iostreams
/usr/bin/ld: cannot find -lgcc_s
/usr/bin/ld: cannot find -lgcc_s
collect2: error: ld returned 1 exit status

正如@some programmer dude的评论中所说;所需要的只是更改订单!

g++ -o demultiplexer.static demultiplexer.cpp -O3 -std=c++11 -static -lboost_iostreams -lz

这适用于