2017-04-01 51 views
2

我试图从boost documentationgzip_decompressor在克利翁(CPP Boost库)

#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安装升压解压使用示例代码gzip文件未找到。 按照CLion documentation中的说明,我已经在CLion中包含了使用动态模板的增强库。 enter image description here

但是,这个简单的代码不起作用。 (编辑错误信息)

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不能被识别。

enter image description here

寻找一个解决方案!

编辑:

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)$使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 
+1

什么是CMakeLists.txt的_actual_内容。编程不是“驯服IDE的艺术”。如果有的话,这是驯服编译器的艺术,而IDE仅仅是一个工具[可以阻碍]。 – sehe

+1

你必须链接'-lboost_iostreams'库。请显示你的'cmake'文件。如果它有'find_package(Boost ...'字符串,然后添加'iostreams'。例如像这样︰'find_package(Boost REQUIRED COMPONENTS system iostreams)' – JustRufus

+0

另外,我怀疑该平台是OSX?尝试传递'cmake -DCMAKE_EXPORT_COMPILE_COMMANDS '并检查'compile_commands.json' – sehe

回答

2

我在一个类似的配置(MACOS +酿造安装升压),所以我进行了测试。答案中列出的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})

+0

哦,是的,我发现这个问题,所以我编辑了这个文件。但是,它给了'libC++ abi.dylib:以boost :: exception_detail :: clone_impl >为未捕获异常终止:gzip错误:unspecified iostream_category错误 '错误。顺便说一句,谢谢你指出这一点。我已更改错误消息。 –

+0

@HeeKyungYoon这是因为文件'example.txt.gz'不在当前工作目录(cwd)中。您可以在CLion的“运行配置”中设置cwd。 – Colliot

+0

哎呀。那就对了。当我在做Ctrl Z时,它意外地更改为原始示例代码,而不是example.txt.gz。感谢您指出了这一点! –