如何构建静态boost库?

How to build a static boost library?

更具体地说,静态 boost::iostreams 支持 zlib(又名 libz)。

问题是,当我尝试 link 静态库时,它会抛出未解析的符号。同时,当我尝试 link 动态库时一切正常...

我找了好几天的答案,还没找到。

我用来构建共享库的命令:b2 -a -q -j8 address-model=32 toolset=gcc --with-iostreams link=shared runtime-link=shared -sZLIB_INCLUDE="path" -sZLIB_LIBPATH="path" -sBZIP2_INCLUDE="path" -sBZIP2_LIBPATH="path"

我用来构建静态库的命令:b2 -a -q -j8 address-model=32 toolset=gcc --with-iostreams link=static runtime-link=static -sZLIB_INCLUDE="path" -sZLIB_LIBPATH="path" -sBZIP2_INCLUDE="path" -sBZIP2_LIBPATH="path"

程序示例:

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

int main(int argc, char* argv[]) {

  int a = boost::iostreams::zlib::default_compression;
  std::cout << a << std::endl;

  return 0;
}

我得到的异常:

D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x124): undefined reference to `crc32'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x181): undefined reference to `deflate'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1a1): undefined reference to `inflate'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1cc): undefined reference to `deflateReset'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x1e5): undefined reference to `inflateEnd'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x201): undefined reference to `inflateReset'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x214): undefined reference to `deflateEnd'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x3c9): undefined reference to `inflateInit2_'
D:\boost_1_72_0\stage\lib/libboost_iostreams-mgw63-mt-s-x32-1_72.a(zlib.o):zlib.cpp:(.text+0x469): undefined reference to `deflateInit2_'
collect2.exe: error: ld returned 1 exit status
makefile:15: recipe for target 'Source.exe' failed
mingw32-make: *** [Source.exe] Error 1
The terminal process terminated with exit code: 1

更新: 另外,如果问题可能与 zlib 或 bz2 有关: 我在这里得到了 bz2 库:http://gnuwin32.sourceforge.net/packages/bzip2.htm 和 zlib 在这里:http://gnuwin32.sourceforge.net/packages/zlib.htm 我尝试用他们的 win32 makefile 自己构建 zlib。我得到了 libz.a 并尝试使用它进行构建,但没有任何改变。

你可能知道,要在 unix 上解决这个问题你只需要添加 -lz 参数。

同样适用于 windows 用户。您只是(更有可能)在 PATH 环境变量中没有 zlib.a/lib 位置。所以要解决这个问题,你应该添加 2 个参数:-Lpath_to_zlib -lz。如此简单的解决方案,我花了几个小时试图修复它。