分类: C/C++
2008-09-15 16:04:03
The initiative is the next hottest thing after STL for the C++ community. While the documentation is quite good, it can be overwhelming to get a quick start. This simple article explains a method of building Boost libraries. I wrote it because many questions in the newsgroups are about linker errors, which happen if you try to link against some of the Boost libraries. Note that this article was originally written while I worked with the Boost version 1.33. Updated July, 2007: Boost version 1.34 seems to introduce a new syntax for Bjam.
While most of the Boost libraries are template-based, some of them -- e.g. date-time, regex, filesystem, signals -- come with CPP source files and thus require the actual code to be generated. It uses a build system called Bjam, which can be configured for a lot of platforms. Because I work only on Windows with Visual Studio 2003, I'll describe here a configuration for this combination.
After downloading the Boost libraries and unpacking them in a folder -- referenced in this article with
The Bjam system must be told that you want to build debug/release libraries, build with threading in mind, and link against various options of the STL or platforms. Because I only use Visual Studio with Plauger's STL, I have only two options:
This should correspond to the following command line for 1.33:
For Boost 1.34, different arguments are required:
This gives two batch files. Unzip them first, of course:
For Boost 1.34, they should be changed to:
Copy these batch files to the root of Boost,
Bjam will now try to build the libraries. After processing, it creates the executables in the
Because all of these files are built under their own library names, one can copy them all -- i.e. search on 'mt-gd-1_33 and mt-1_33 -- to one location and adjust Visual Studio to point to this library location.
We have built the shared libraries and therefore we have to instruct Visual Studio to link against them. Fortunately, Boost has incorporated an "autolink" feature. Thus, with the preprocessor one can link to the correct libraries. Define the following preprocessor statements:
BOOST_ALL_DYN_LINK
BOOST_LIB_DIAGNOSTIC
Even better, include a header file in your precompiled header before including a Boost header: