Boost Compiled Libraries in 3 Minutes

Boost is mostly made of just header files, as we saw last week, which means you include them in your source, add the correct namespace and you’re good to go.

However, there are a handful of compiled libraries too, so let’s take a very quick look at how we can use these. If you haven’t installed Boost, take a look at Boost in 3 Minutes to get you up and running.


1) Include the header of the library you are interested in using

I’m going to use the filesystem library:

#include <boost/filesystem.hpp>

Time taken: 30 seconds


2) Write some code using this library

Don’t forget to qualify the methods with a namespace (else it will not compile!)

#include <boost/filesystem.hpp>
#include <iostream>

int main()
    //don't forget this bit!
    using namespace boost::filesystem;

    path p("/var/log/dmesg");

    if (exists(p))
        std::cout << p << " size is " 
            << file_size(p) << std::endl;

    return 0;

Time taken: 2 minutes


3) Compile your code with the boost libraries

Now, instead of just compiling our file, we have to include the filesystem library and let the compiler know where it lives:

g++ boost.cpp -I/usr/include/boost -lboost_filesystem -lboost_system

To clarify, the line above is made up of the following things:


Firstly I’ve passed the source code file to the compiler.

Secondly I’ve added the path to boost using the -I option.

Thirdly I’ve added the libraries I want to link to using -l.


In this case there are two libraries that I need to include, as the filesystem binary depends on the system binary. You will get a linker error if there are any dependencies when you include your initial library. This will tell you what else you might need to add.

Time taken: 30 seconds



The output of the little program above should be something like:

/var/log/dmesg size is 61318

And there you have it – boost compiled libraries in under 3 minutes 🙂

Get Started With Boost in 3 Minutes

Boost is simply a collection of C++ libraries that provide lots of fun things to do in C++ without you having to write the code yourself.

For example, Boost contains libraries to help you with mathematical calculations, regular expressions, smart pointers and even python integration.

Parts of Boost have already been integrated into C++11, but if you (or your organisation) is not quite ready for C++11 yet, or you want to play with the other Boost libraries, you can get to grips with it really easily in just 3 minutes.

This guide covers the header only libraries (i.e. most of them). I’ll take a look at the compiled libraries next week.


1) Install Boost

As root/sudo type:

yum install boost-devel

You must install ‘boost-devel’, rather than just ‘boost’, or you will only get the compiled libraries and not the headers. Since boost is mostly header-only libraries, this won’t be much use!

Time taken: 30 seconds


2) Write yourself a program and Boost it!

In order to use the boost methods, you must include the namespace qualifier.

If you don’t, your compiler will tell you that the methods are undefined and you may end up on a wild goose chase trying to ensure that boost is installed correctly and that the header files are being found. Stop right there!

You can either include it in one line (as I have done here), or you can individually qualify each method you use, e.g. with boost::gregorian::.


//include the boost header file for the library you want to use
#include <boost/date_time/gregorian/gregorian.hpp>
#include <iostream>

int main()
    //don't miss this out or it won't compile
    using namespace boost::gregorian;

    //construct a date object
    date xmas(2011,Dec,25);

    //print the day of the week
    std::cout << "Christmas 2011 falls on ";
    std::cout  << xmas.day_of_week() << std::endl;

    //add a (leap) year
    date_duration leapyr(366);
    xmas += leapyr;

    //print day of the week next year
    std::cout << "Christmas 2012 falls on ";
    std::cout << xmas.day_of_week() << std::endl;

    return 0;

Time taken: 2 minutes


3) Compile and run your program

g++ boost.cpp

The output should be:

Christmas 2011 falls on Sun
Christmas 2012 falls on Tue

Then sit back and feel very pleased with yourself.

Time taken: 30 seconds


If you thought that was fab and want to learn what else you can do, full documentation is available here.

Can’t argue with that!