1. Get & Build & Install Boost
download boost from http://www.boost.org/
进入boost目录,使用命令:
./bootstrap.sh --prefix=path/to/installation
./b2 install
如此之后:
leave Boost binaries in the lib/ subdirectory of your installation prefix. You will also find a copy of the Boost headers in theinclude/ subdirectory of the installation prefix, so you can henceforth use that directory as an #include path in place of the Boost root directory.
2. Use Boost
1) Header-Only Libraries
Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking.
比如下面这个例子,使用的就是header-only的library.
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
编译:
c++ -I path/to/boost_1_47_0 example.cpp -o example
then:
echo 1 2 3 | ./example
2) Separately-Compiled Binary
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
编译:
g++ -I /home/bin.jinb/usr/local/boost/include/ test.cc -o test \
-L /home/bin.jinb/usr/local/boost/lib/ -lboost_regex
或者:
g++ -I /home/bin.jinb/usr/local/boost/include/ test.cc -o test \
/home/bin.jinb/usr/local/boost/lib/libboost_regex.a