2017-04-01 50 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。感謝您指出了這一點! –