How to use Boost in Visual Studio 2010



339 down vote accepted

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 not across the system as a whole. Note you only have to do it once.

  1. Unarchive the latest version of boost (1.47.0 as of writing) into a directory of your choice (e.g. C:\boost_1_47_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.47.0 as of writing) into a directory of your choice (e.g. C:\boost_1_47_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 b2.exe (previously named bjam).
  4. Run b2: (Win32) b2 --toolset=msvc-10.0 --build-type=complete stage ; (x64) b2 --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_47_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.6 as of writing) source files into a directory of your choice (e.g. C:\bzip2-1.0.6).
  2. Follow the second set of instructions above to build boost, but add in the option -sBZIP2_SOURCE="C:\bzip2-1.0.6" when running b2 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 b2 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. To completely built the 32-bits version of the library requires 32-bits Python, and similarly for the 64-bits version. If you have multiple versions installed for such reason, you'll need to tell b2 where to find specific version and when to use which one. One way to do that would be to edit the file project-config.jam in the directory <BOOST_PATH> that resulted from running bootstrap. Add in the following two lines adjusting as appropriate for your Python installation paths & versions (note the space before the ';').

    using python : 2.6 : C:\\Python\\Python26\\python ;

    using python : 2.6 : C:\\Python\\Python26-x64\\python : : : <address-model>64 ;

    Do note that such explicit Python specification currently cause MPI build to fail. So you'll need to do some separate building with and without specification to build everything if you're building MPI as well.

  3. Follow the second set of instructions above to build boost.

Boost.Regex ICU support

  1. Unarchive the latest version of ICU4C library (4.8 as of writing) source file into a directory of your choice (e.g. C:\icu4c-4_8).
  2. Open the Visual Studio Solution in <ICU_PATH>\source\allinone.
  3. Build All for both debug & release configuration for the platform of your choice. There can be a problem building recent releases of ICU4C with Visual Studio 2010 when the output for both debug & release build are in the same directory (which is the default behaviour). A possible workaround is to do a Build All (of debug build say) and then do a Rebuild all in the 2nd configuration (e.g. release build).
  4. 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.
  5. Optionally remove the source directory when you're done.
  6. Follow the second set of instructions above to build boost, but add in the option -sICU_PATH="C:\icu4c-4_8" when running b2 in step 5.
share | improve this answer
 
 
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  Apr 24 '10 at 19:29
 
@Sorin: why would you ever need to change that? –   jalf  May 7 '10 at 0:59
2  
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 The Human  Aug 18 '10 at 0:41
7  
If you want both x64 & win32 side by side, add "--stagedir=lib/win32" and "--stagedir=lib/x64" to the respective builds. –   M. Tibbits  Sep 16 '10 at 19:13
2  
For a video tutorial how to build and configure boost for Visual Studio 2010, this might help:youtube.com/watch?v=5AmwIwedTCM –   toefel  Jan 22 '11 at 20:45
show 4 more comments
up vote 106 down vote

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\libbut 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: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\i386 install
    • For Visual Studio 2012, use toolset=msvc-11.0
    • For Visual Studio 2010, use toolset=msvc-10.0
  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\libbut 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: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\x64 architecture=x86 address-model=64 install
    • For Visual Studio 2012, use toolset=msvc-11.0
    • For Visual Studio 2010, use toolset=msvc-10.0
  5. Add C:\Boost\include\boost-(version) to your include path.
  6. Add C:\Boost\lib\x64 to your libs path.
share | improve this answer
 
7  
If you don't want to build them yourself, you can install pre-built binaries from sourceforge:sourceforge.net/projects/boost/files/boost-binaries For each version of boost, there are installers for each visual studio version (in both 32 and 64 bit). –   teeks99  Oct 21 '13 at 17:45
add comment
up vote 11 down vote

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 (in C++0x):

#include <memory> using std::shared_ptr;
share | improve this answer
 
5  
In VS2010, the TR1 libraries that are becoming part of C++0x have all been moved into the stdnamespace, 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  Apr 13 '10 at 13:04
add comment
up vote 10 down vote

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.

share | improve this answer
  add comment
up vote 8 down vote

Getting Started on Windows

share | improve this answer
 
1  
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  Apr 13 '10 at 14:59
add comment
up vote 7 down vote

I could recommend the following trick: Create a special boost.props file

  1. Open the property manager
  2. Right click on your project node, and select 'Add new project property sheet'.
  3. Select a location and name your property sheet (e.g. c:\mystuff\boost.props)
  4. Modify the additional Include and Lib folders to the search path.

This procedure has the value that boost is included only in projects where you want to explicitly include it. When you have a new project that uses boost, do:

  1. Open the property manager.
  2. Right click on the project node, and select 'Add existing property sheet'.
  3. Select the boost property sheet.

EDIT (following edit from @jim-fred):

The resulting boost.props file looks something like this...

<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup Label="UserMacros"> <BOOST_DIR>D:\boost_1_53_0\</BOOST_DIR> </PropertyGroup> <PropertyGroup> <IncludePath>$(BOOST_DIR);$(IncludePath)</IncludePath> <LibraryPath>$(BOOST_DIR)stage\lib\;$(LibraryPath)</LibraryPath> </PropertyGroup> </Project>

It contains a user macro for the location of the boost directory (in this case, D:\boost_1_53_0) and two other parameters: IncludePath and LibraryPath. A statement #include <boost/thread.hpp> would find thread.hpp in the appropriate directory (in this case, D:\boost_1_53_0\boost\thread.hpp). The 'stage\lib\' directory may change depending on the directory installed to.

This boost.props file could be located in the D:\boost_1_53_0\ directory.

share | improve this answer
  add comment
up vote 6 down vote

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 ;)

share | improve this answer
  add comment
up vote 2 down vote

Here is how I was able to use Boost:

  1. Download and extract the zip version of Boost libraries.
  2. Run bootstrap.bat file and then run bjam.exe.
  3. Wait for roughly 30 minutes or so.
  4. Create a new project in Visual Studio.
  5. Go to project-->properties-->Linker-->General-->Additional Library Directories and addboost/stage/lib directory to it.
  6. Go to project-->properties-->C/C++-->General-->Additional Include Directories and add boostdirectory to it.

You will be able to build your project without any errors !

share | improve this answer
  add comment
up vote 1 down vote

A small addition to KTC's very informative main answer:

If you are using the free Visual Studio c++ 2010 Express, and managed to get that one to compile 64-bits binaries, and now want to use that to use a 64-bits version of the Boost libaries, you may end up with 32-bits libraries (your mileage may vary of course, but on my machine this is the sad case).

I could fix this using the following: inbetween the steps described above as

  1. Start a 32-bit MSVC command prompt and change to the directory where Boost was unzipped.
  2. Run: bootstrap

I inserted a call to 'setenv' to set the environment. For a release build, the above steps become:

  1. Start a 32-bit MSVC command prompt and change to the directory where Boost was unzipped.
  2. Run: "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\setenv.cmd" /Release /x64
  3. Run: bootstrap

I found this info here: http://boost.2283326.n4.nabble.com/64-bit-with-VS-Express-again-td3044258.html

share | improve this answer
  add comment
up vote 1 down vote

Download boost from: http://www.boost.org/users/download/ e.g. by svn

  • Windows -> tortoise (the simplest way)

After that : cmd -> go to boost directory ("D:\boostTrunk" - where You checkout or download and extract package): command : bootstrap

we created bjam.exe in ("D:\boostTrunk") After that : command : bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static (It will take some time ~20min.)

After that: Open Visual studio 2010 -> create empty project -> go to project properties -> set:

How to use Boost in Visual Studio 2010_第1张图片

Paste this code and check if it is working?

#include <iostream> #include <boost/shared_ptr.hpp> #include <boost/regex.hpp> using namespace std; struct Hello { Hello(){ cout << "Hello constructor" << endl; } ~Hello(){ cout << "Hello destructor" << endl; cin.get(); } }; int main(int argc, char**argv) { //Boost regex, compiled library boost::regex regex("^(Hello|Bye) Boost$"); boost::cmatch helloMatches; boost::regex_search("Hello Boost", helloMatches, regex); cout << "The word between () is: " << helloMatches[1] << endl; //Boost shared pointer, header only library boost::shared_ptr<Hello> sharedHello(new Hello); return 0; }

Resources : https://www.youtube.com/watch?v=5AmwIwedTCM

share | improve this answer
  add comment
up vote 0 down vote

A minimalist example to get you started in Visual Studio:

1.Download and unzip Boost from here.

2.Create a Visual Studio empty project, using an example boost library that does not require separate compilation:

#include <iostream> #include <boost/format.hpp> using namespace std; using namespace boost; int main() { unsigned int arr[5] = { 0x05, 0x04, 0xAA, 0x0F, 0x0D }; cout << format("%02X-%02X-%02X-%02X-%02X") % arr[0] % arr[1] % arr[2] % arr[3] % arr[4] << endl; } 

3.In your Visual Studio project properties set the Additional Include Directories:

How to use Boost in Visual Studio 2010_第2张图片

For a very simple example:

How to Install the Boost Libraries in Visual Studio

If you don't want to use the entire boost library, just a subset:

Using a subset of the boost libraries in Windows

If you specifically want to now about the libraries that require compilation:

How to use the Boost compiled libraries in Windows

share | improve this answer
  add comment

Your Answer

 
339 down vote accepted

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 not across the system as a whole. Note you only have to do it once.

  1. Unarchive the latest version of boost (1.47.0 as of writing) into a directory of your choice (e.g. C:\boost_1_47_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.47.0 as of writing) into a directory of your choice (e.g. C:\boost_1_47_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 b2.exe (previously named bjam).
  4. Run b2: (Win32) b2 --toolset=msvc-10.0 --build-type=complete stage ; (x64) b2 --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_47_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.6 as of writing) source files into a directory of your choice (e.g. C:\bzip2-1.0.6).
  2. Follow the second set of instructions above to build boost, but add in the option -sBZIP2_SOURCE="C:\bzip2-1.0.6" when running b2 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 b2 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. To completely built the 32-bits version of the library requires 32-bits Python, and similarly for the 64-bits version. If you have multiple versions installed for such reason, you'll need to tell b2 where to find specific version and when to use which one. One way to do that would be to edit the file project-config.jam in the directory <BOOST_PATH> that resulted from running bootstrap. Add in the following two lines adjusting as appropriate for your Python installation paths & versions (note the space before the ';').

    using python : 2.6 : C:\\Python\\Python26\\python ;

    using python : 2.6 : C:\\Python\\Python26-x64\\python : : : <address-model>64 ;

    Do note that such explicit Python specification currently cause MPI build to fail. So you'll need to do some separate building with and without specification to build everything if you're building MPI as well.

  3. Follow the second set of instructions above to build boost.

Boost.Regex ICU support

  1. Unarchive the latest version of ICU4C library (4.8 as of writing) source file into a directory of your choice (e.g. C:\icu4c-4_8).
  2. Open the Visual Studio Solution in <ICU_PATH>\source\allinone.
  3. Build All for both debug & release configuration for the platform of your choice. There can be a problem building recent releases of ICU4C with Visual Studio 2010 when the output for both debug & release build are in the same directory (which is the default behaviour). A possible workaround is to do a Build All (of debug build say) and then do a Rebuild all in the 2nd configuration (e.g. release build).
  4. 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.
  5. Optionally remove the source directory when you're done.
  6. Follow the second set of instructions above to build boost, but add in the option -sICU_PATH="C:\icu4c-4_8" when running b2 in step 5.
share | improve this answer
 
    
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  Apr 24 '10 at 19:29
    
@Sorin: why would you ever need to change that? –   jalf  May 7 '10 at 0:59
2  
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 The Human  Aug 18 '10 at 0:41
7  
If you want both x64 & win32 side by side, add "--stagedir=lib/win32" and "--stagedir=lib/x64" to the respective builds. –   M. Tibbits  Sep 16 '10 at 19:13
2  
For a video tutorial how to build and configure boost for Visual Studio 2010, this might help:youtube.com/watch?v=5AmwIwedTCM –   toefel  Jan 22 '11 at 20:45
show 4 more comments
up vote 106 down vote

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\libbut 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: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\i386 install
    • For Visual Studio 2012, use toolset=msvc-11.0
    • For Visual Studio 2010, use toolset=msvc-10.0
  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\libbut 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: b2 toolset=msvc-12.0 --build-type=complete --libdir=C:\Boost\lib\x64 architecture=x86 address-model=64 install
    • For Visual Studio 2012, use toolset=msvc-11.0
    • For Visual Studio 2010, use toolset=msvc-10.0
  5. Add C:\Boost\include\boost-(version) to your include path.
  6. Add C:\Boost\lib\x64 to your libs path.
share | improve this answer
 
7  
If you don't want to build them yourself, you can install pre-built binaries from sourceforge:sourceforge.net/projects/boost/files/boost-binaries For each version of boost, there are installers for each visual studio version (in both 32 and 64 bit). –   teeks99  Oct 21 '13 at 17:45
add comment
up vote 11 down vote

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 (in C++0x):

#include <memory> using std::shared_ptr;
share | improve this answer
 
5  
In VS2010, the TR1 libraries that are becoming part of C++0x have all been moved into the stdnamespace, 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  Apr 13 '10 at 13:04
add comment
up vote 10 down vote

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.

share | improve this answer
  add comment
up vote 8 down vote

Getting Started on Windows

share | improve this answer
 
1  
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  Apr 13 '10 at 14:59
add comment
up vote 7 down vote

I could recommend the following trick: Create a special boost.props file

  1. Open the property manager
  2. Right click on your project node, and select 'Add new project property sheet'.
  3. Select a location and name your property sheet (e.g. c:\mystuff\boost.props)
  4. Modify the additional Include and Lib folders to the search path.

This procedure has the value that boost is included only in projects where you want to explicitly include it. When you have a new project that uses boost, do:

  1. Open the property manager.
  2. Right click on the project node, and select 'Add existing property sheet'.
  3. Select the boost property sheet.

EDIT (following edit from @jim-fred):

The resulting boost.props file looks something like this...

<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ImportGroup Label="PropertySheets" /> <PropertyGroup Label="UserMacros"> <BOOST_DIR>D:\boost_1_53_0\</BOOST_DIR> </PropertyGroup> <PropertyGroup> <IncludePath>$(BOOST_DIR);$(IncludePath)</IncludePath> <LibraryPath>$(BOOST_DIR)stage\lib\;$(LibraryPath)</LibraryPath> </PropertyGroup> </Project>

It contains a user macro for the location of the boost directory (in this case, D:\boost_1_53_0) and two other parameters: IncludePath and LibraryPath. A statement #include <boost/thread.hpp> would find thread.hpp in the appropriate directory (in this case, D:\boost_1_53_0\boost\thread.hpp). The 'stage\lib\' directory may change depending on the directory installed to.

This boost.props file could be located in the D:\boost_1_53_0\ directory.

share | improve this answer
  add comment
up vote 6 down vote

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 ;)

share | improve this answer
  add comment
up vote 2 down vote

Here is how I was able to use Boost:

  1. Download and extract the zip version of Boost libraries.
  2. Run bootstrap.bat file and then run bjam.exe.
  3. Wait for roughly 30 minutes or so.
  4. Create a new project in Visual Studio.
  5. Go to project-->properties-->Linker-->General-->Additional Library Directories and addboost/stage/lib directory to it.
  6. Go to project-->properties-->C/C++-->General-->Additional Include Directories and add boostdirectory to it.

You will be able to build your project without any errors !

share | improve this answer
  add comment
up vote 1 down vote

A small addition to KTC's very informative main answer:

If you are using the free Visual Studio c++ 2010 Express, and managed to get that one to compile 64-bits binaries, and now want to use that to use a 64-bits version of the Boost libaries, you may end up with 32-bits libraries (your mileage may vary of course, but on my machine this is the sad case).

I could fix this using the following: inbetween the steps described above as

  1. Start a 32-bit MSVC command prompt and change to the directory where Boost was unzipped.
  2. Run: bootstrap

I inserted a call to 'setenv' to set the environment. For a release build, the above steps become:

  1. Start a 32-bit MSVC command prompt and change to the directory where Boost was unzipped.
  2. Run: "C:\Program Files\Microsoft SDKs\Windows\v7.1\Bin\setenv.cmd" /Release /x64
  3. Run: bootstrap

I found this info here: http://boost.2283326.n4.nabble.com/64-bit-with-VS-Express-again-td3044258.html

share | improve this answer
  add comment
up vote 1 down vote

Download boost from: http://www.boost.org/users/download/ e.g. by svn

  • Windows -> tortoise (the simplest way)

After that : cmd -> go to boost directory ("D:\boostTrunk" - where You checkout or download and extract package): command : bootstrap

we created bjam.exe in ("D:\boostTrunk") After that : command : bjam toolset=msvc-10.0 variant=debug,release threading=multi link=static (It will take some time ~20min.)

After that: Open Visual studio 2010 -> create empty project -> go to project properties -> set:

How to use Boost in Visual Studio 2010_第3张图片

Paste this code and check if it is working?

#include <iostream> #include <boost/shared_ptr.hpp> #include <boost/regex.hpp> using namespace std; struct Hello { Hello(){ cout << "Hello constructor" << endl; } ~Hello(){ cout << "Hello destructor" << endl; cin.get(); } }; int main(int argc, char**argv) { //Boost regex, compiled library boost::regex regex("^(Hello|Bye) Boost$"); boost::cmatch helloMatches; boost::regex_search("Hello Boost", helloMatches, regex); cout << "The word between () is: " << helloMatches[1] << endl; //Boost shared pointer, header only library boost::shared_ptr<Hello> sharedHello(new Hello); return 0; }

Resources : https://www.youtube.com/watch?v=5AmwIwedTCM

share | improve this answer
  add comment
up vote 0 down vote

A minimalist example to get you started in Visual Studio:

1.Download and unzip Boost from here.

2.Create a Visual Studio empty project, using an example boost library that does not require separate compilation:

#include <iostream> #include <boost/format.hpp> using namespace std; using namespace boost; int main() { unsigned int arr[5] = { 0x05, 0x04, 0xAA, 0x0F, 0x0D }; cout << format("%02X-%02X-%02X-%02X-%02X") % arr[0] % arr[1] % arr[2] % arr[3] % arr[4] << endl; } 

3.In your Visual Studio project properties set the Additional Include Directories:

How to use Boost in Visual Studio 2010_第4张图片

For a very simple example:

How to Install the Boost Libraries in Visual Studio

If you don't want to use the entire boost library, just a subset:

Using a subset of the boost libraries in Windows

If you specifically want to now about the libraries that require compilation:

How to use the Boost compiled libraries in Windows

share | improve this answer
  add comment

Your Answer

 
0
0
 
 

你可能感兴趣的:(How to use Boost in Visual Studio 2010)