[SVN r36494]
9.8 KiB
__ 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 and Install 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=%TEMP%\build-boost ^ More? --toolset=msvc stage
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.
Link From Within the Visual Studio IDE
Starting with the header-only example project we created earlier:
- Right-click example in the Solution Explorer pane and select Properties from the resulting pop-up menu
- In Configuration Properties > Linker >
Additional Library Directories, enter the path to the Boost
binaries, e.g.
\lib\
. - From the Build menu, select Build Solution.
__
Or, Link From the Command Prompt
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?”
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.↩︎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.↩︎
In this example, the caret character
^
is a way of continuing the command on multiple lines. The command prompt responds withMore?
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.↩︎