我正在尝试构建一个使用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
答案 0 :(得分:0)
就像@some程序员花花公子的评论一样;所有需要做的就是改变顺序!
g++ -o demultiplexer.static demultiplexer.cpp -O3 -std=c++11 -static -lboost_iostreams -lz
这有效