Thursday, October 01, 2009

Compiling Boost Regex in Linux

Boost libraries provide many interesting C++ libraries with cool functionalities. I've been using for many years now boost shared pointers and boost regex library (for regular expression). In particular, I'm using them in my software GNU Source-Highlight. These libraries are usually available in LInux distributions, but if they are not, and you need to build them (compile them) from sources... well... here comes trouble :-)

The first time, it is actually not easy to build boost libraries from sources, even though you're used to build programs from sources following the usual "./configure && make && make install"; in particular, boost use their own build system based on bjam. Indeed, I've had many complaints from source-highlight users when it comes to build source-highlight and the boost libraries are not available in their system.

I hope this post sheds some light for building boost libraries from source. In particular, I'll concentrate on regex library (in fact, you're not required to build all the libraries: you can simply select the ones you're interested in). Note that some boost libraries come only in the shape of header files (like shared pointers) and others require also a lib file (like regex). Note, however, that all the header files will be installed anyway (they basically do not require any compilation), while library files require compilation, and compiling all the libraries in boost may require some time; thus, having the chance to select only some libraries is quite useful.

In this post I'll assume that you want to build only the regex library and to install all the built files into $HOME/opt/boost (so that you will not need root access rights).

Once you unpacked the sources into a directory, say ~boost

tar xjvf ~/Downloads/boost_1_39_0.tar.bz2
you get into that directory
cd ~boost
and run the booststrap command
./ \
--prefix=$HOME/opt/boost \
--libdir=$HOME/opt/boost/lib \
Hopefully, you will get no error, and then you're ready to build and install the boost headers and the regex library with the following command:
./bjam --build-dir=build --layout=system install
This will install the directories include and lib into $HOME/opt/boost.

You can now test your installation using the example program you find in the boost website, example.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;

Since you installed boost in a directory which is probably not in the search path of your compiler, you will need to specify the path for header inclusion and the path for library, as follows:
g++ -I$HOME/opt/boost/include example.cpp -o example \
-L$HOME/opt/boost/lib -lboost_regex
If everything went fine, you'll have your program to test.

If you want to run your program, remember that the boost regex library you built is a dynamic library, thus, again, since it is not installed in a system path where the loader searches for libraries to load at run-time, you'll need to specify that path with the environment variable LD_LIBRARY_PATH, e.g.,
LD_LIBRARY_PATH=$HOME/opt/boost/lib ./example
As I said before, you could have install the library and headers in system path, e.g., /usr; all you have to do is to specify a different bootstrap command
./ \
--prefix=/usr \
--libdir=/usr/lib \
and then run bjam as before (but with root privileges).

Hope this helps :-)


zerosith said...


Excellent post.

I was wondering if you knew something about building boost libraries under windows using MinGW msys and the eclipse CDT plugin.


betto said...

glad you liked that.

I'm working on the mingw version these days, and hope to be able to post something pretty soon :)

dc said...

Thank you! Your concise and meticulous post solved my problem. Kudos!

Jānis said...
This comment has been removed by the author.
Jānis said...

Could it be that the second ./ should go without '$HOME'?

betto said...

right! sorry about that, I'll modify the post.

you're right: if you want to install it in the system path then $HOME makes no sense :)

Anonymous said...

Thank you! Following these instructions I was able to build boost regex.