more/getting_started/windows.rst
2007-11-25 19:35:43 +00:00

11 KiB

Boost__ Getting Started on Windows

A 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

Get Boost

The easiest way to get a copy of Boost is to use the installer provided by Boost Consulting. We especially recommend this method if you use Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005, 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 .exe_ and run it to install a complete Boost distribution.1

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.

Command Prompt Basics

In Windows, a command-line tool is invoked by typing its name, optionally followed by arguments, into a Command Prompt window and pressing the Return (or Enter) key.

To open a generic Command Prompt, click the Start menu button, click Run, type “cmd”, and then click OK.

All commands are executed within the context of a current directory in the filesystem. To set the current directory, type:

cd path\to\some\directory

followed by Return. For example,

cd

Long commands can be continued across several lines by typing a caret (^) at the end of all but the last line. Some examples on this page use that technique to save horizontal space.

Build From the Visual Studio IDE

  • From Visual Studio's File menu, select New > Project…

  • In the left-hand pane of the resulting New Project dialog, select Visual C++ > Win32.

  • In the right-hand pane, select Win32 Console Application (VS8.0) or Win32 Console Project (VS7.1).

  • In the name field, enter “example”

  • Right-click example in the Solution Explorer pane and select Properties from the resulting pop-up menu

  • In Configuration Properties > C/C++ > General > Additional Include Directories, enter the path to the Boost root directory, for example

  • In Configuration Properties > C/C++ > Precompiled Headers, change Use Precompiled Header (/Yu) to Not Using Precompiled Headers.2

  • Replace the contents of the example.cpp generated by the IDE with the example code above.

  • From the Build menu, select Build Solution.

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.

__

Or, Build From the Command Prompt

From 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\example.cpp

To test the result, type:

echo 1 2 3 | example

Install Visual Studio (2005 or .NET 2003) Binaries

The installer supplied by Boost Consulting will download and install pre-compiled binaries into the lib\ subdirectory of the boost root, typically \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.

__

Or, Build Binaries From Source

If 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.

For example, your session might look like this:3

C:\WINDOWS> cd > 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.

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.

Starting with the header-only example project we created earlier:

  1. Right-click example in the Solution Explorer pane and select Properties from the resulting pop-up menu
  2. In Configuration Properties > Linker > Additional Library Directories, enter the path to the Boost binaries, e.g. \lib\.
  3. From the Build menu, select Build Solution.

__

For 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 \lib:

cl /EHsc /I example.cpp ^

/link /LIBPATH: \lib

Library Naming

Note

If, like Visual C++, your compiler supports auto-linking, you can probably __.

__ Test Your Program

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?”



  1. If you prefer not to download executable programs, download .zip_ and use an external tool to decompress it. We don't recommend using Windows' built-in decompression as it can be painfully slow for large archives.↩︎

  2. There's no problem using Boost with precompiled headers; these instructions merely avoid precompiled headers because it would require Visual Studio-specific changes to the source code used in the examples.↩︎

  3. In this example, the caret character ^ is a way of continuing the command on multiple lines, and must be the final character used on the line to be continued (i.e. do not follow it with spaces). The command prompt responds with More? to prompt for more input. Feel free to omit the carets and subsequent newlines; we used them so the example would fit on a page of reasonable width.

    The command prompt treats each bit of whitespace in the command as an argument separator. That means quotation marks (") are required to keep text together whenever a single command-line argument contains spaces, as in

    --build-dir="C:\Documents_and_Settings\dave\build-boost"

    Also, for example, you can't add spaces around the = sign as in

    --build-dir_=_"C:\Documents and Settings\dave\build-boost"

    ↩︎