1. 从http://www.boostpro.com/download/下载最新的预编译好的boost版本,然后运行,安装是选择2005的static版本
2. 安装完成后,把C:\Program Files\boost\boost_1_51添加到头文件系统目录(tools->options->VC++ Directories->include files),把C:\Program Files\boost\boost_1_51\lib添加到库文件系统目录,如下图:
3. 新建一个console工程,输入如下代码:
#include <iostream> #include <boost/thread.hpp> #include <boost/date_time.hpp> void workerFunc() { boost::posix_time::seconds workTime(3); std::cout << "Worker: running" << std::endl; // Pretend to do something useful... boost::this_thread::sleep(workTime); std::cout << "Worker: finished" << std::endl; } int _tmain(int argc, _TCHAR* argv[]) { std::cout << "main: startup" << std::endl; boost::thread workerThread(workerFunc); std::cout << "main: waiting for thread" << std::endl; workerThread.join(); std::cout << "main: done" << std::endl; return 0; }
如果出现如下错误:
1>------ Build started: Project: boost_tester, Configuration: Debug Win32 ------
1>Compiling...
1>stdafx.cpp
1>Compiling...
1>boost_tester.cpp
1>Linking...
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: char const * __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::c_str(void)const " (?c_str@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBEPBDXZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::~basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(void)" (??1?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@XZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator+=(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??Y?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@ABV01@@Z) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator+=(char const *)" (??Y?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > & __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::operator=(char const *)" (??4?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAEAAV01@PBD@Z) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: bool __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::empty(void)const " (?empty@?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QBE_NXZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(char const *)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@PBD@Z) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: __thiscall std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >::basic_string<char,struct std::char_traits<char>,class std::allocator<char> >(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (??0?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAE@ABV01@@Z) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static unsigned int __cdecl std::char_traits<char>::length(char const *)" (?length@?$char_traits@D@std@@SAIPBD@Z) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static unsigned int __cdecl std::numeric_limits<unsigned int>::max(void)" (?max@?$numeric_limits@I@std@@SAIXZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static unsigned int __cdecl std::numeric_limits<unsigned int>::min(void)" (?min@?$numeric_limits@I@std@@SAIXZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static __int64 __cdecl std::numeric_limits<__int64>::max(void)" (?max@?$numeric_limits@_J@std@@SA_JXZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>msvcprtd.lib(MSVCP80D.dll) : error LNK2005: "public: static __int64 __cdecl std::numeric_limits<__int64>::min(void)" (?min@?$numeric_limits@_J@std@@SA_JXZ) already defined in libboost_thread-vc80-mt-sgd-1_51.lib(thread.obj)
1>LINK : fatal error LNK1104: cannot open file 'libboost_thread-vc80-mt-gd-1_51.lib'
则做如下改动(project->properties->c/c++ ->code generation, 将Multi-threaded Debug DLL (/MDd)改为Multi-threaded Debug (/MTd),编译即可通过。
如下图: