Getting a Boost

March 11, 2011

Most of you that use C++ (and some of you that don’t) are sure to have heard of Boost C++. I won’t go into what it is, since if you want to know that just follow the previous link. I will however talk about the “installation” process. This is mainly for me, in case I ever feel like putting it on another computer, but I suppose some people might find this useful.

Step 1

Download Boost, I would recommend the latest version but it’s your call. You can find download links by going to the Boost website and doing a little “read-n-search”, aka using your brain. I will assume that you have downloaded the file (let’s call it FILE, and assume it is has extension bz2) to your desktop. Open a terminal and move to where you would like the Boost files/libraries/binaries to be located, call this $BOOST. In short you just run the following in the terminal:

cd $BOOST
tar --bzip2 -xf ~/Desktop/FILE

Step 2

At this point you are more or less done and are able to use quite a few of the provided libraries. However, there are some libraries that need to be built. So let’s go ahead and build them all, since you never know when you’ll need one, and you aren’t that strapped for space on your system.
First move to the folder that contains a file called bootstrap.sh, on my system that means moving to $BOOST/boost_1_46_0/. At this point I assume that you will be using the “default” GCC compiler.

(sudo) ./bootstrap.sh --prefix=$SOME_PATH --libdir=$OTHER_PATH

The sudo is optional depending on what you choose for $SOME_PATH and $OTHER_PATH. I chose $SOME_PATH=/usr/local and $OTHER_PATH=/usr/local/lib, in these cases I did need to include the sudo.
This should run without any errors.

Step 3

Finally you are ready to build and install the libraries. However, first there is a file called project-config.jam in your directory. Read it’s contents, at the bottom there should be a place that tells you what your prefix and libdir are set to. Make sure these are what you want, if not delete this file

rm -f project-config.jam

and go back to step 2.
So everything is in order. Now just run

(sudo) ./bjam --build-dir=build --layout=system install

You will only need to use sudo if it was required for the previous step. This command will the necessary files into $OTHER_PATH (you should go there to make sure it worked). This command will take a while to run since we are installing all the libraries (there is a way to specify which to install but I won’t bother with that). There will be a lot of text printed to the screen, just keep any eye out for anything that looks like an error message. Some of these can be ignored, like the ones that deal with Python integration, while others are actual problems you will need to fix.

Step 4

Hopefully, you have made it this far without any errors. So it is time to test that everything actually works.

//ASSUME THIS FILE IS CALLED test.cpp
#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;
   }
}

Use the following to compile

g++ -o test test.cpp -I $BOOST/boost_1_46_0 -L $OTHER_PATH -lboost_regex
./test

If there are any mistakes or if you have any suggestions let me know. I know this isn’t the best walk through but I think it’s better (for beginners) than the one presented at the Boost website.

Advertisements