VS2010安装Boost库

时间:2022-05-20 18:23:03

source URL: http://*.com/questions/2629421/how-to-use-boost-in-visual-studio-2010

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: 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\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: 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.

Test code

// boost_lib.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/regex.hpp>
#include <boost/thread/thread.hpp>
using namespace std;

void wait(int seconds)
{
  boost::this_thread::sleep(boost::posix_time::seconds(seconds));
} 

void my_thread()
{
  for (int i = 0; i < 5; ++i)
  {
    wait(1);
    std::cout << i << std::endl;
  }
} 

int main()
{
  boost::thread t(my_thread);
  t.join();
}