Static linking for boost-based program -- zlib problem

2582 views c++
4

I am trying to build a program which uses the Boost Iostreams library and create a statically linked executable. I have tried this on both RHEL7 and Ubuntu 18.04, and get similar errors. The compiler is GCC. On 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

What's wrong, how can I prevent these linker errors? Alternatives attempted:

$ 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

answered question

Order matters. If library A depends on library B, then A must come before B on the command line.

1 Answer

3

As in the comment by @some programmer dude; all that's needed is to change the order!

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

This works

posted this

Have an answer?

JD

Please login first before posting an answer.