C++ Can't link Boost library
I'm trying to compile this little piece of code from the boost documentation: (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
}
But it refuses to compile, I get the following errors:
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: In fu开发者_JAVA百科nction ‘int main()’:
../teste/main.cpp:9:25: error: ‘compressor’ was not declared in this scope
../teste/main.cpp:10:29: error: ‘base64_encoder’ was not declared in this scope
../teste/main.cpp:11:37: error: ‘file_sink’ was not declared in this scope
I know I'm probably doing something stupid but I just can't see what...
edit:
BTW, I have all boost libraries and -dev files installed properly. and I'm using QT-Creator, so my .pro file looks like so:
SOURCES += \
main.cpp
LIBS += \
-lboost_filesystem \
-lboost_iostreams
I assume you are refering to the example at
http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/tutorial/filter_usage.html
If you read carefully, you will notice that the tutorial page states that
If you have appropriate OutputFilters compressor and base64_encoder, you can do this as follows
The code on this example page is not meant to be compilable. Try this example instead:
http://www.boost.org/doc/libs/1_46_1/libs/iostreams/doc/classes/zlib.html#examples
...but be sure to add another using namespace boost::iostreams
to be able to compile it, i.e.:
#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);
}
The example is not complete it just shows the usage of io::filtering_ostream out; but its not valid since its not declaring or including the necessary code for the compressor(); base64_encoder and file_sink functions.
精彩评论