gzip_decompressor 未在 CLion 中找到(cpp boost 库)

gzip_decompressor not found in CLion (cpp boost library)

我尝试使用 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

我在类似的配置上(macOS + brew 安装了 boost)所以我测试了它。现在答案中列出的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}).