Search code examples
c++gccboostgnuzlib

Can't link static boost::iostreams library


I build the following code:

#include <boost/iostreams/filter/zlib.hpp>
#include <iostream>

int main(int argc, char* argv[])
{
  int a = boost::iostreams::zlib::default_compression;
  std::cout << a;
  return 0;
}

With the command:

g++ -Wall -ID:\boost_1_72_0 -c -o Source.o Source.cpp
g++ -Wall -ID:\boost_1_72_0 Source.o -LD:\boost_1_72_0\stage\lib -lboost_iostreams-mgw63-mt-x32-1_72 -o Source.exe

And it works, but only if boost_iostreams-mgw63-mt-x32-1_72 is shared library. If I try to use static library it'll give me the following error: D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x124): undefined reference to crc32'

I build static library with the following command: b2 -a -q -j8 address-model=32 link=static threading=multi toolset=gcc runtime-link=shared variant=release --with-iostreams -sZLIB_INCLUDE="C:\Program Files (x86)\GnuWin32\include" -sZLIB_LIBPATH="C:\Program Files (x86)\GnuWin32\lib" -sBZIP2_INCLUDE="C:\Program Files (x86)\GnuWin32\include" -sBZIP2_LIBPATH="C:\Program Files (x86)\GnuWin32\lib"

If I change link=staic to link=shared and then copy dll to the project's folder - everything will be ok. But I want program to work w/o dlls.

What's the problem? How do I run and build the program w/o shared libs?


Solution

  • Short: -Lpath_to_zlib -lz.

    Long: As you may know, to solve this problem on unix you just need to add -lz argument. The same works for windows users. You just (more likely) don't have zlib.a/lib location in your PATH environment variable. So to solve the problem you shall add 2 arguments: -Lpath_to_zlib -lz.