【问题标题】:C++ Can't link Boost libraryC ++无法链接Boost库
【发布时间】:2011-08-22 22:20:05
【问题描述】:

我正在尝试从 boost 文档中编译这段代码: (http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/tutorial/filter_usage.html)

#include <boost/iostreams/device/file_descriptor.hpp>
#include <boost/iostreams/filtering_stream.hpp>

namespace io = boost::iostreams;

int main()
{   
    io::filtering_ostream out;
    out.push(compressor());
    out.push(base64_encoder());
    out.push(file_sink("my_file.txt"));
    // write to out using std::ostream interface
}

但它拒绝编译,我得到以下错误:

g++ -c -pipe -g -Wall -W -D_REENTRANT -DQT_GUI_LIB -DQT_CORE_LIB -DQT_SHARED -I/usr/share/qt4/mkspecs/linux-g++ -I../teste -I/usr/include/qt4 /QtCore -I/usr/include/qt4/QtGui -I/usr/include/qt4 -I. -I../teste -I. -o main.o ../teste/main.cpp

../teste/main.cpp:在函数'int main()'中:

../teste/main.cpp:9:25: 错误:“压缩器”未在此范围内声明

../teste/main.cpp:10:29: 错误:'base64_encoder' 未在此范围内声明

../teste/main.cpp:11:37: 错误:'file_sink' 未在此范围内声明

我知道我可能在做一些愚蠢的事情,但我就是看不到什么......

编辑:

顺便说一句,我已正确安装了所有 boost 库和 -dev 文件。我正在使用 QT-Creator,所以我的 .pro 文件如下所示:

SOURCES += \
    main.cpp

LIBS += \
    -lboost_filesystem \
    -lboost_iostreams 

【问题讨论】:

    标签: c++ linux boost linker qt-creator


    【解决方案1】:

    我假设您指的是示例

    http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/tutorial/filter_usage.html

    如果你仔细阅读,你会注意到教程页面声明

    如果你有合适的输出过滤器 压缩器和base64_encoder,你可以 这样做如下

    此示例页面上的代码不可编译。试试这个例子:

    http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/classes/zlib.html#examples

    ...但一定要添加另一个using namespace boost::iostreams 才能编译它,即:

    #include <fstream>
    #include <iostream>
    #include <boost/iostreams/filtering_streambuf.hpp>
    #include <boost/iostreams/copy.hpp>
    #include <boost/iostreams/filter/zlib.hpp>
    
    int main() 
    {
        using namespace std;
        using namespace boost::iostreams;
    
        ifstream file("hello.z", ios_base::in | ios_base::binary);
        filtering_streambuf<input> in;
        in.push(zlib_decompressor());
        in.push(file);
        boost::iostreams::copy(in, cout);
    }
    

    【讨论】:

    • 哇,现在我明白了!我认为compressor() 和base64_encoder() 是真正的函数:) 我说这可能是愚蠢的,谢谢你的关注。
    • 太棒了,浪费了 2 天时间以各种方式重新编译 boost,试图弄清楚为什么这个不可编译的示例代码无法编译。
    【解决方案2】:

    例子并不完整,只是展示了io::filtering_ostream的用法;但它无效,因为它没有声明或包含 compressor() 的必要代码; base64_encoder 和 file_sink 函数。

    【讨论】:

      猜你喜欢
      • 2017-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-31
      • 2020-07-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-22
      相关资源
      最近更新 更多