我尝试使用boost documentation
中的示例代码解压缩gzip文件#include <fstream>
#include <iostream>
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
int main()
{
using namespace std;
ifstream file("example.txt.gz", ios_base::in | ios_base::binary);
boost::iostreams::filtering_streambuf<boost::iostreams::input> in;
in.push(boost::iostreams::gzip_decompressor());
in.push(file);
boost::iostreams::copy(in, cout);
}
我使用$brew install boost
安装了boost。
我按照CLion documentation中的说明在CLion中使用了Live Template包含了boost库。
然而,这个简单的代码并不起作用。 (编辑错误消息)
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::iostreams::gzip_error> >: gzip error: unspecified iostream_category error
我怀疑gzip_decompressor无法以某种方式识别出来。
寻找解决方案!
编辑:
1)这是CMakeLists.txt文件。
cmake_minimum_required(VERSION 3.7)
project(practice_cpp)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
find_package(Boost REQUIRED COMPONENTS system iostreams)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(practice_cpp ${SOURCE_FILES})
if(Boost_FOUND)
target_link_libraries(practice_cpp ${Boost_LIBRARIES})
endif()
2)另外,我在终端上尝试了这个命令。
c++ -I /usr/local/Cellar/boost/1.63.0 main.cpp -lboost_iostreams -o main
输出
$ ./main
libc++abi.dylib: terminating with uncaught exception of type boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::iostreams::gzip_error> >: gzip error: unspecified iostream_category error
Abort trap: 6
3)这是commands_compile.json。
[
{
"directory": "/path/to/practice_cpp",
"command": "/Library/Developer/CommandLineTools/usr/bin/c++ -I/usr/local/include -g -std=gnu++11 -o CMakeFiles/practice_cpp.dir/main.cpp.o -c /path/to/practice_cpp/main.cpp",
"file": "/path/to/practice_cpp/main.cpp"
}
]
4)$ make VERBOSE = ON
$ make VERBOSE=ON
/usr/local/Cellar/cmake/3.7.2/bin/cmake -H/path/to/practice_cpp -B/path/to/practice_cpp --check-build-system CMakeFiles/Makefile.cmake 0
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_progress_start /path/to/practice_cpp/CMakeFiles /path/to/practice_cpp/CMakeFiles/progress.marks
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/Makefile2 all
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/practice_cpp.dir/build.make CMakeFiles/practice_cpp.dir/depend
cd /path/to/practice_cpp && /usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_depends "Unix Makefiles" /path/to/practice_cpp /path/to/practice_cpp /path/to/practice_cpp /path/to/practice_cpp /path/to/practice_cpp/CMakeFiles/practice_cpp.dir/DependInfo.cmake --color=
/Library/Developer/CommandLineTools/usr/bin/make -f CMakeFiles/practice_cpp.dir/build.make CMakeFiles/practice_cpp.dir/build
make[2]: Nothing to be done for `CMakeFiles/practice_cpp.dir/build'.
[100%] Built target practice_cpp
/usr/local/Cellar/cmake/3.7.2/bin/cmake -E cmake_progress_start /path/to/practice_cpp/CMakeFiles 0
答案 0 :(得分:2)
我正在使用类似的配置(macOS + brew安装升级),所以我测试了它。答案中列出的cmake配置现在是好的。
cmake_minimum_required(VERSION 3.7)
project(practice_cpp)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
find_package(Boost REQUIRED COMPONENTS system iostreams)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(practice_cpp ${SOURCE_FILES})
if(Boost_FOUND)
target_link_libraries(practice_cpp ${Boost_LIBRARIES})
endif()
问题是,这似乎与问题中发布的原始文件不同:
cmake_minimum_required(VERSION 3.7)
project(practice_cpp)
set(CMAKE_CXX_STANDARD 11)
set(SOURCE_FILES main.cpp)
add_executable(practice_cpp ${SOURCE_FILES})
find_package(Boost REQUIRED COMPONENTS system iostreams)
if(Boost_FOUND)
message(STATUS "Boost_INCLUDE_DIRS: ${Boost_INCLUDE_DIRS}")
message(STATUS "Boost_LIBRARIES: ${Boost_LIBRARIES}")
message(STATUS "Boost_VERSION: ${Boost_VERSION}")
include_directories(${Boost_INCLUDE_DIRS})
endif()
add_executable(BoostTest main.cpp)
target_link_libraries(practice_cpp ${Boost_LIBRARIES})
当我第一次看到问题时,文件看起来像这样。我测试了它并得到了问题中的确切错误。
所以实际上BoostTest
是可执行文件,你应该添加target_link_libraries(BoostTest ${Boost_LIBRARIES})
。