我在使用arguments构建的Windows上使用Mingw-w64 v7 和g++ 10.2 。我已经从这里下载并解压了bzip2 1.0.6:https ://sourceforge.net/projects/bzip2/ 。现在我在MSys2控制台中像这样构建boost 1.76.0 :--mode=gcc-10.2.0 --arch=x86_64 --buildroot=/c/mingw-builds/BuildRoot --update-sources --exceptions=seh --threads=posix --enable-languages=c++ --jobs=48 --rt-version=v7
./bootstrap.sh --with-libraries=iostreams --prefix=c:/Libraries/boost_1_76_0/InstallRelease
./b2 --layout=system variant=release link=shared address-model=64 threading=multi optimization=speed runtime-link=shared -s BZIP2_SOURCE=C:/Libraries/bzip2-1.0.6 install
我想使用Boost.Iostreams用bzip2压缩一些流。因此,我创建了一个包含以下内容的测试文件 main.cpp:
#include <iostream>
#include <fstream>
#include <boost/iostreams/filtering_stream.hpp>
#include <boost/iostreams/filter/bzip2.hpp>
int main()
{
{
std::ofstream file("hello.z", std::ios_base::out | std::ios_base::binary);
boost::iostreams::filtering_ostream out;
out.push(boost::iostreams::bzip2_compressor());
out.push(file);
out << "Hello World!" << std::endl;
}
{
std::ifstream file_in("hello.z", std::ios_base::in | std::ios_base::binary);
boost::iostreams::filtering_istream in;
in.push(boost::iostreams::bzip2_decompressor());
in.push(file_in);
std::string content(std::istreambuf_iterator<char>(in), {});
std::cout << content << std::endl;
}
return 0;
}
如果我编译它
g++ main.cpp -o main.exe --std=c++11 -lboost_iostreams -I/c/libraries/boost_1_76_0/InstallRelease/include -L/c/libraries/boost_1_76_0/InstallRelease/lib
在Msys2控制台上设置路径
export PATH=/c/Libraries/boost_1_76_0/InstallRelease/lib:/c/mingw-w64-10.2/mingw64/bin
运行它,我得到
$ ./main.exe
Hello World!
现在,如果我从目录中删除目录bin.v2
并再次构建boost,但这次是这样的:InstallRelease
boost_1_76_0
cflags=-flto
./b2 --layout=system variant=release link=shared address-model=64 threading=multi optimization=speed runtime-link=shared cflags="-flto" -s BZIP2_SOURCE=C:/Libraries/bzip2-1.0.6 install
再次运行main.exe
,我得到:
$ ./main.exe
Segmentation fault
如果我改用的话,情况并非如此boost::iostreams::zlib_(de)compressor
。如果我用这样的调试信息构建提升:
./b2 --layout=system variant=debug link=shared address-model=64 runtime-link=shared cflags="-flto" -s BZIP2_SOURCE=C:/Libraries/bzip2-1.0.6 install
并main.exe
在gdb中运行,输出如下:
Starting program: C:\...\main.exe #
[New Thread 7460.0x1dec]
[New Thread 7460.0x2508]
[New Thread 7460.0x2cb4]
Thread 1 received signal SIGSEGV, Segmentation fault.
0x0000000000000000 in ?? ()
(gdb)
尝试打印回溯是不成功的,即使main.exe
已使用参数编译-O0 -g
:
(gdb) bt
#0 0x0000000000000000 in ?? ()
Backtrace stopped: previous frame identical to this frame (corrupt stack?)
这意味着什么?链接时优化没有我想象的那么有害吗?错误在哪里?在编译器中?在Boost.Iostreams 中?我犯了错误吗?哪一个?或者我怎样才能弄清楚究竟是什么导致了分段错误?