Wednesday, February 22, 2012

Building Boost with Visual C++ 2010 Express

Following my last blog about Building Boost with MinGW, here I'm going to show you step-by-step how to build Boost with Visual C++ 2010 Express. The latest Boost as of this writing is 1.48.0, so I'm going to use that version.

  1. Download Visual C++ 2010 Express from Microsoft website and install it.
  2. Download Windows SDK. This is optional, but it can be pretty useful if you plan to call any Windows API.
  3. Download Boost from Boost website.
  4. Extract Boost to any location of you preference.
  5. Modify the C:\Program Files\Microsoft Visual Studio 10.0\VC\bin\vcvars32.bat as shown below. You need to edit this file as an Administrator. Visual C++ 2010 Express seems to have a bug for not setting all these variables automatically. You need to uncomment the first 3 lines. Setting WindowsSdkDir is optional if you skipped step 2.
    :: @call :GetVSCommonToolsDir
    :: @if "%VS100COMNTOOLS%"=="" goto error_no_VS100COMNTOOLSDIR
    :: @call "%VS100COMNTOOLS%VCVarsQueryRegistry.bat" 32bit No64bit
    
    @SET WindowsSdkDir=C:\Program Files\Microsoft SDKs\Windows\v7.1\
    @SET VSINSTALLDIR=C:\Program Files\Microsoft Visual Studio 10.0\
    @SET VCINSTALLDIR=C:\Program Files\Microsoft Visual Studio 10.0\VC\
    @SET VS100COMNTOOLS=%VSINSTALLDIR%\Common7\IDE\
    @SET PATH=%PATH%;%VCINSTALLDIR%\bin;%VS100COMNTOOLS%
    @SET FrameworkDir32=C:\Windows\Microsoft.NET\Framework
    @SET FrameworkVersion32=v4.0.30319
    @SET Framework35Version=v3.5
    
    
  6. Open Visual Studio Command Prompt (2010) and go to your boost installation directory. 
  7. Build Boost build tool (b2).
    [boost_dir]\bootstrap.bat
    
    
  8. Build Boost (you can start making your coffee or do whatever you like since this process is gonna take a while). All the libraries will be created in the stage\lib folder.
  9. [boost_dir]\b2.exe toolset=msvc --build-type complete stage
  10. Open your Visual C++ 2010 Express and create a new Win32 Console project.
    
    
  11. Right click on the project --> Properties--> Configuration Properties --> C/C++ --> General and add [boost_dir] in the Additional Include Directories.
  12. In the same Properties window Configuration Properties --> Linker -->  General, add [boost_dir]\stage\lib in the Additional Library Directories.
  13. Let's now test our Boost libraries.
  14. #include "stdafx.h"
    #include <string>
    #include <iostream>
    #include <boost/shared_ptr.hpp>
    #include <boost/regex.hpp>
    using namespace std;
    
    int _tmain(int argc, _TCHAR* argv[])
    {
        boost::shared_ptr<string> ptr(new string("Hello World, Boost"));
        boost::smatch matches;
        boost::regex pattern("Hello World,\\s*(.*)");
        boost::regex_match(*ptr, matches, pattern);
        cout << matches[1] << endl;
    
        system("pause");
    
     return 0;
    }
    

3 comments:

  1. \b2.exe toolset=msvc ==build-type complete stage
    failed for unknown feature "<>"

    command should be:
    .\b2.exe toolset=msvc --build-type=complete stage

    ReplyDelete
  2. It was a typo. Fixed it. Thanks!

    ReplyDelete
  3. Thank you very much.its really work.finally i setup boost with vc++ express after 3 days.thank you again

    ReplyDelete