views:

7512

answers:

7

What is a good step by step explanation on how to use BOOST in an empty project in Visual Studio 2010.

+5  A: 

Getting Started on Windows

skwllsp
I wish the guide was a little less verbose. It could really be consdensed down to about 5 lines... unzip, run bootstrap, run bjam, set include directories in vs, set library directories in vs.
Inverse
+3  A: 

What parts of Boost do you need? A lot of stuff is part of TR1 which is shipped with Visual Studio, so you could simply say, for example:

#include <tr1/memory>

using std::tr1::shared_ptr;

According to James, this should also work:

#include <memory>

using std::shared_ptr;
FredOverflow
In VS2010, the TR1 libraries that are becoming part of C++0x have all been moved into the `std` namespace, as they are in the C++0x standard. (I think they are probably in the `std::tr1` namespace as well, for backwards compatibility).
James McNellis
@James Thanks, I extended my post accordingly.
FredOverflow
+11  A: 

While the instructions on the Boost web site are helpful, here is a condensed version that also builds x64 libraries.

  • You only need to do this if you are using one of the libraries mentioned in section 3 of the instructions page. (E.g., to use Boost.Filesystem requires compilation.) If you are not using any of those, just unzip and go.

Build the 32-bit libraries

This installs the Boost header files under C:\Boost\include\boost-(version), and the 32-bit libraries under C:\Boost\lib\i386. Note that the default location for the libraries is C:\Boost\lib but you’ll want to put them under an i386 directory if you plan to build for multiple architectures.

  1. Unzip Boost into a new directory.
  2. Start a 32-bit MSVC command prompt and change to the directory where Boost was unzipped.
  3. Run: bootstrap
  4. Run: .\bjam.exe toolset=msvc --build-type=complete --libdir=C:\Boost\lib\i386 install
  5. Add C:\Boost\include\boost-(version) to your include path.
  6. Add C:\Boost\lib\i386 to your libs path.

Build the 64-bit libraries

This installs the Boost header files under C:\Boost\include\boost-(version), and the 64-bit libraries under C:\Boost\lib\x64. Note that the default location for the libraries is C:\Boost\lib but you’ll want to put them under an x64 directory if you plan to build for multiple architectures.

  1. Unzip Boost into a new directory.
  2. Start a 64-bit MSVC command prompt and change to the directory where Boost was unzipped.
  3. Run: bootstrap
  4. Run: .\bjam.exe toolset=msvc --build-type=complete --libdir=C:\Boost\lib\x64 architecture=x86 address-model=64 install
  5. Add C:\Boost\include\boost-(version) to your include path.
  6. Add C:\Boost\lib\x64 to your libs path.
Nate
+46  A: 

While Nate's answer is pretty good already, I'm going to expand on it more specifically for Visual Studio 2010 as requested, and include information on compiling in the various optional components which requires external libraries.

If you are using headers only libraries, then all you need to do is to unarchive the boost download and set up the environment variables. The instruction below set the environment variables for Visual Studio only, and riables. The instruction below set the environment variables for Visual Studio only, and not across the system as a whole. Note you only have to do it once.

  1. Unarchive the latest version of boost (1.44.0 as of writing) into a directory of your choice (e.g. C:\boost_1_44_0).
  2. Create a new empty project in Visual Studio.
  3. Open the Property Manager and expand one of the configuration for the platform of your choice.
  4. Select & right click Microsoft.Cpp.<Platform>.user, and select Properties to open the Property Page for edit.
  5. Select VC++ Directories on the left.
  6. Edit the Include Directories section to include the path to your boost source files.
  7. Repeat steps 3 - 6 for different platform of your choice if needed.

If you want to use the part of boost that require building, but none of the features that requires external dependencies, then building it is fairly simple.

  1. Unarchive the latest version of boost (1.44.0 as of writing) into a directory of your choice (e.g. C:\boost_1_44_0).
  2. Start the Visual Studio Command Prompt for the platform of your choice and navigate to where boost is.
  3. Run: bootstrap.bat to build bjam.
  4. Run bjam: (Win32) bjam --toolset=msvc-10.0 --build-type=complete stage ; (x64) bjam --toolset=msvc-10.0 --build-type=complete architecture=x86 address-model=64 stage. Go for a walk / watch a movie or 2 / ....
  5. Go through steps 2 - 6 from the set of instruction above to set the environment variables.
  6. Edit the Library Directories section to include the path to your boost libraries output. (The default for the example and instructions above would be C:\boost_1_44_0\stage\lib. Rename and move the directory first if you want to have x86 & x64 side by side (such as to <BOOST_PATH>\lib\x86 & <BOOST_PATH>\lib\x64).
  7. Repeat steps 2 - 6 for different platform of your choice if needed.

If you want the optional components, then you have more work to do. These are:

  • Boost.IOStreams Bzip2 filters
  • Boost.IOStreams Zlib filters
  • Boost.MPI
  • Boost.Python
  • Boost.Regex ICU support

Boost.IOStreams Bzip2 filters:

  1. Unarchive the latest version of bzip2 library (1.0.5 as of writing) source files into a directory of your choice (e.g. C:\bzip2-1.0.5).
  2. Follow the second set of instructions above to build boost, but add in the option -sBZIP2_SOURCE="C:\bzip2-1.0.5" when running bjam in step 5.

Boost.IOStreams Zlib filters

  1. Unarchive the latest version of zlib library (1.2.5 as of writing) source files into a directory of your choice (e.g. C:\zlib-1.2.5).
  2. Follow the second set of instructions above to build boost, but add in the option -sZLIB_SOURCE="C:\zlib-1.2.5" when running bjam in step 5.

Boost.MPI

  1. Install a MPI distribution such as Microsoft Compute Cluster Pack.
  2. Follow steps 1 - 3 from the second set of instructions above to build boost.
  3. Edit the file project-config.jam in the directory <BOOST_PATH> that resulted from running bootstrap. Add in a line that read using mpi ; (note the space before the ';').
  4. Follow the rest of the steps from the second set of instructions above to build boost. If auto-detection of the MPI installation fail, then you'll need to look for and modify the appropriate build file to look for MPI in the right place.

Boost.Python

  1. Install a Python distribution such as ActiveState's ActivePython. Make sure the Python installation is in your PATH.
  2. Follow the second set of instructions above to build boost.

Boost.Regex ICU support

  1. Unarchive the latest version of ICU4C library (4.4.1 as of writing) source file into a directory of your choice (e.g. C:\icu4c-4_4_1).
  2. Open the Visual Studio Solution in <ICU_PATH>\source\allinone and accept the question to convert the solution and projects to Visual Studio 2010 formats.
  3. Build All for both debug & release configuration for the platform of your choice. (You can happily ignore all the TargetName, TargetPath, OutputFile mismatch warnings.) You'll need to do some directories renaming and move for both the bin & lib directories if you want to have x86 & x64 side by side. If building for x64, you'll need to be running x64 OS as there's post build steps that involves running some of the 64-bits application that it's building.
  4. Optionally remove the source directory when you're done.
  5. Follow the second set of instructions above to build boost, but add in the option -sICU_PATH="C:\icu4c-4_4_1" when running bjam in step 5.
KTC
+1 Very helpful to me. Thank you.
Jon
Thanks KTC, The only things missing here is specifying how to deal with calling conventions: meaning how to change calling conventions for boost (changing for your own msvc project it is easy).
Sorin Sbarnea
@Sorin: why would you ever need to change that?
jalf
yay thank for the fix for property_tree
MiniScalope
It should be noted that you must choose Tools > Settings > Expert Mode in order to even see the property sheets. Took me some googling to finally find this out...
Zack Mulgrew
@Zack Mulgrew: Huh? I don't even have that menu option...
KTC
@KTC: It's actually "Expert Settings", available from the Tools > Settings menu. I'm running VSC++ 2010 Express Edition and without activating "Expert Settings" you can't see the property sheets.
Zack Mulgrew
M. Tibbits
+2  A: 

Also a little note: If you want to reduce the compilation-time, you can add the flag

-j2

to run two parallel builds at the same time. This might reduce it to viewing one movie ;)

GaMer13
+2  A: 

You can also try -j%NUMBER_OF_PROCESSORS% as an argument it will use all your cores. Makes things super fast on my quad core.

Aaron Stainback
A: 

what about the dll version of boost and how to set path for loading dll-s froom boost/stage/lib instead of windows/...

lazar