Getting Started on WindowsA note to Cygwin and MinGW users If you plan to use your tools from the Windows command prompt, you're in the right place. If you plan to build from the Cygwin bash shell, you're actually running on a POSIX platform and should follow the instructions for getting started on Unix variants. Other command shells, such as MinGW's MSYS, are not supported—they may or may not work. Index 1 Get BoostThe easiest way to get a copy of Boost is to use an installer. The Boost website version of this Getting Started guide will have undated information on installers as they become available, or see Boost downloads or the installer provided by BoostPro Computing. We especially recommend using an installer if you use Microsoft Visual Studio, because the installer can download and install precompiled library binaries, saving you the trouble of building them yourself. To complete this tutorial, you'll need to at least install the Boost.Regex binaries when given the option. If you're using an earlier version of Visual Studio or some other compiler, or if you prefer to build everything yourself, you can download boost_1_36_0.7z or boost_1_36_0.zip and unpack it to install a complete Boost distribution.1 2 The Boost DistributionThis is a sketch of the resulting directory structure: boost_1_36_0\ .................The “boost root directory” index.htm .........A copy of www.boost.org starts here boost\ .........................All Boost Header files lib\ .....................precompiled library binaries libs\ ............Tests, .cpps, docs, etc., by library index.html ........Library documentation starts here algorithm\ any\ array\ …more libraries… status\ .........................Boost-wide test suite tools\ ...........Utilities, e.g. bjam, quickbook, bcp more\ ..........................Policy documents, etc. doc\ ...............A subset of all Boost library docs It's important to note the following:
3 Header-Only LibrariesThe first thing many people want to know is, “how do I build Boost?” The good news is that often, there's nothing to build. Nothing to Build? Most Boost libraries are header-only: they consist entirely of header files containing templates and inline functions, and require no separately-compiled library binaries or special treatment when linking. The only Boost libraries that must be built separately are:
A few libraries have optional separately-compiled binaries:
4 Build a Simple Program Using BoostTo keep things simple, let's start by using a header-only library. The following program reads a sequence of integers from standard input, uses Boost.Lambda to multiply each number by three, and writes them to standard output: #include <boost/lambda/lambda.hpp> #include <iostream> #include <iterator> #include <algorithm> int main() { using namespace boost::lambda; typedef std::istream_iterator<int> in; std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " ); } Copy the text of this program into a file called example.cpp. Note To build the examples in this guide, you can use an Integrated Development Environment (IDE) like Visual Studio, or you can issue commands from the command prompt. Since every IDE and compiler has different options and Microsoft's are by far the dominant compilers on Windows, we only give specific directions here for Visual Studio 2005 and .NET 2003 IDEs and their respective command prompt compilers (using the command prompt is a bit simpler). If you are using another compiler or IDE, it should be relatively easy to adapt these instructions to your environment. 4.1 Build From the Visual Studio IDE
To test your application, hit the F5 key and type the following into the resulting window, followed by the Return key: 1 2 3 Then hold down the control key and press "Z", followed by the Return key. 4.2 Or, Build From the Command PromptFrom your computer's Start menu, if you are a Visual Studio 2005 user, select All Programs > Microsoft Visual Studio 2005 > Visual Studio Tools > Visual Studio 2005 Command Prompt or, if you're a Visual Studio .NET 2003 user, select All Programs > Microsoft Visual Studio .NET 2003 > Visual Studio .NET Tools > Visual Studio .NET 2003 Command Prompt to bring up a special command prompt window set up for the Visual Studio compiler. In that window, set the current directory to a suitable location for creating some temporary files and type the following command followed by the Return key:
cl /EHsc /I path\to\boost_1_36_0 path\to\example.cpp
To test the result, type: echo 1 2 3 | example 4.3 Errors and WarningsDon't be alarmed if you see compiler warnings originating in Boost headers. We try to eliminate them, but doing so isn't always practical.5 Errors are another matter. If you're seeing compilation errors at this point in the tutorial, check to be sure you've copied the example program correctly and that you've correctly identified the Boost root directory. 5 Prepare to Use a Boost Library BinaryIf you want to use any of the separately-compiled Boost libraries, you'll need to acquire library binaries. 5.1 Install Visual Studio BinariesThe installers will download and install pre-compiled binaries into the lib\ subdirectory of the boost root, typically C:\Program Files\boost\boost_1_36_0\lib\. If you installed all variants of the Boost.Regex binary, you're done with this step. Otherwise, please run the installer again and install them now. 5.2 Or, Build Binaries From SourceIf you're using an earlier version of Visual C++, or a compiler from another vendor, you'll need to use Boost.Build to create your own binaries. Boost.Build is a text-based system for developing, testing, and installing software. To use it, you'll need an executable called bjam. 5.2.1 Get bjambjam is the command-line tool that drives the Boost Build system. To build Boost binaries, you'll invoke bjam from the Boost root. Boost provides pre-compiled bjam executables for a variety of platforms. Alternatively, you can build bjam yourself using these instructions. 5.2.2 Identify Your ToolsetFirst, find the toolset corresponding to your compiler in the following table. Note If you previously chose a toolset for the purposes of building bjam, you should assume it won't work and instead choose newly from the table below.
If you have multiple versions of a particular compiler installed, you can append the version number to the toolset name, preceded by a hyphen, e.g. intel-9.0 or borland-5.4.3. On Windows, append a version number even if you only have one version installed (unless you are using the msvc or gcc toolsets, which have special version detection code) or auto-linking will fail. 5.2.3 Select a Build DirectoryBoost.Build will place all intermediate files it generates while building into the build directory. If your Boost root directory is writable, this step isn't strictly necessary: by default Boost.Build will create a bin.v2/ subdirectory for that purpose in your current working directory. 5.2.4 Invoke bjamChange your current directory to the Boost root directory and invoke bjam as follows: bjam --build-dir=build-directory --toolset=toolset-name [--build-type=complete] stage For example, your session might look like this:4 C:\WINDOWS> cd C:\Program Files\boost\boost_1_36_0 C:\Program Files\boost\boost_1_36_0> bjam ^ More? --build-dir="C:\Documents and Settings\dave\build-boost" ^ More? --toolset=msvc stage Be sure to read this note about the appearance of ^, More? and quotation marks (") in that line. The above example session will build static and shared non-debug multi-threaded variations of the libraries. To build all variations: C:\WINDOWS> cd C:\Program Files\boost\boost_1_36_0 C:\Program Files\boost\boost_1_36_0> bjam ^ More? --build-dir="C:\Documents and Settings\dave\build-boost" ^ More? --toolset=msvc --build-type=complete stage Building the special stage target places Boost library binaries in the stage\ subdirectory of your build directory. Note bjam is case-sensitive; it is important that all the parts shown in bold type above be entirely lower-case. For a description of other options you can pass when invoking bjam, type: bjam --help In particular, to limit the amount of time spent building, you may be interested in:
5.3 Expected Build OutputDuring the process of building Boost libraries, you can expect to see some messages printed on the console. These may include
5.4 In Case of Build ErrorsThe only error messages you see when building Boost—if any—should be related to the IOStreams library's support of zip and bzip2 formats as described here. Install the relevant development packages for libz and libbz2 if you need those features. Other errors when building Boost libraries are cause for concern. If it seems like the build system can't find your compiler and/or linker, consider setting up a user-config.jam file as described in the Boost.Build documentation. If that isn't your problem or the user-config.jam file doesn't work for you, please address questions about configuring Boost for your compiler to the Boost.Build mailing list. 6 Link Your Program to a Boost LibraryTo demonstrate linking with a Boost binary library, we'll use the following simple program that extracts the subject lines from emails. It uses the Boost.Regex library, which has a separately-compiled binary component. #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; } } There are two main challenges associated with linking:
Auto-Linking Most Windows compilers and linkers have so-called “auto-linking support,” which eliminates the second challenge. Special code in Boost header files detects your compiler options and uses that information to encode the name of the correct library into your object files; the linker selects the library with that name from the directories you've told it to search. The GCC toolchains (Cygwin and MinGW) are notable exceptions; GCC users should refer to the linking instructions for Unix variant OSes for the appropriate command-line options to use. 6.1 Link From Within the Visual Studio IDEStarting with the header-only example project we created earlier:
6.2 Or, Link From the Command PromptFor example, we can compile and link the above program from the Visual C++ command-line by simply adding the bold text below to the command line we used earlier, assuming your Boost binaries are in C:\Program Files\boost\boost_1_36_0\lib:
cl /EHsc /I path\to\boost_1_36_0 example.cpp ^
/link /LIBPATH: C:\Program Files\boost\boost_1_36_0\lib
6.3 Library NamingNote If, like Visual C++, your compiler supports auto-linking, you can probably skip to the next step. In order to choose the right binary for your build configuration you need to know how Boost binaries are named. Each library filename is composed of a common sequence of elements that describe how it was built. For example, libboost_regex-vc71-mt-d-1_34.lib can be broken down into the following elements:
6.4 Test Your ProgramTo test our subject extraction, we'll filter the following text file. Copy it out of your browser and save it as jayne.txt: To: George Shmidlap From: Rita Marlowe Subject: Will Success Spoil Rock Hunter? --- See subject. Now, in a command prompt window, type: path\to\compiled\example < path\to\jayne.txt The program should respond with the email subject, “Will Success Spoil Rock Hunter?” 7 Conclusion and Further ResourcesThis concludes your introduction to Boost and to integrating it with your programs. As you start using Boost in earnest, there are surely a few additional points you'll wish we had covered. One day we may have a “Book 2 in the Getting Started series” that addresses them. Until then, we suggest you pursue the following resources. If you can't find what you need, or there's anything we can do to make this document clearer, please post it to the Boost Users' mailing list.
Onward
|