C-Programmierung

Boost Basics

Boost | | boost::asio

We closely follow the section “Getting Started with Boost”:

First we download boost_1_53_0.tar.gz (or a newer version) from sourceforge.

And extract the sources into /usr/local/:

cd /usr/local/
sudo tar zxvf /path/to/boost_1_53_0.tar.gz

Now for the following boost examples our path/to/boost is /usr/local/boost_1_53_0

To keep things simple, let’s start by using a header-only Boost library. This means we do not have to compile the library, because the headers do already contain everything we need.

As an example, the following program reads a sequence of integers from standard input, uses header-only Boost.Lambda to multiply each number by three, and writes them to standard output:

#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) << " " );
}

Copy the text of this program into a file called example.cpp.

Now, in the directory where you saved example.cpp, issue the following command:

c++ -I path/to/boost example.cpp -o example

To test the result, type:

echo 1 2 3 | ./example

Many other boost libraries like boost::asio are not header-conly. For those we need to compile Boost - in the path/to/boost:

./bootstrap.sh
./b2 link=static

This will take quite a while (about 15min on a MacBook Pro), so get yourself a coffee in the meantime, but when it is finished, we’ll get a C++ boost!

To link with the compiled boost libs, we add “-Lpath/to/boost/stage/lib” to the link line.

Boost | | boost::asio

Options: