Thursday, May 20, 2010

Boost.Build and bjam

Boost.Build and bjam

Just to document the essentials.
I had some difficulties in the past using bjam in combination with Boost, and had them again with each new non-Linux platform. The Boost SVN sandbox shows that many Boost authors encounter the same problems, resulting in a variety of creative solutions.

The canonical way to build a project using boost header-only libraries, making with bjam (Boost.Build) is:
  • add a clause "<dependency>/boost//headers" into the jamfile (either Jamfile.v2, or Jamfile.jam, or Jamroot.jam)
  • add a clause "use-project boost : c:/software/boost_1_43_0 ;" (adapt path if necessary) into the file "user-config.jam" which lives in c:\software\boost_1_43_0\tools\build\v2\user-config.jam
  • bjam.exe should be in your path
  • how does it find Boost.Build? There are two ways to do this:
    • either add a file named 'boost-build.jam' in your directory tree, either at the level of 'Jamroot', or a level higher
    • or set the environment variable BOOST_BUILD_PATH e.g. using export BOOST_BUILD_PATH='c:\software\boost_1_43_0\tools\build\v2' (note that 1) setting environment variables might be inconvenient and 2) if both are set, the boost-build.jam file setting is preferred)
Then your project should compile without errors. For example, this is your source-file "hello.cpp":
#include <iostream>
#include <boost/algorithm/string.hpp>

int main()
        << boost::to_upper_copy(std::string("Hello boost!"))
        << std::endl;
    return 0;

And this is your jamfile "jamroot.jam":
exe hello
    : hello.cpp
    : <dependency>/boost//headers

And this is your file "boost-build.jam":
boost-build c:/software/boost_1_43_0/tools/build/v2 ;

And this is at the bottom of your "user-config.jam":
use-project boost : c:/software/boost_1_43_0 ;

This works in at least MSVC (command prompt) and MinGW using GCC