1,到官网下载boost,www.boost.org 这里我下载的1-69版本.
2,安装,解压后运行bootstrap.bat文件。稍等一小会就OK。
3,编译boost库。注意一定要使用VS2017的x86本机工具命令提示,这个可以在VS2017的安装菜单里面找到。进入命令行提示,输入下面的内容:
bjam -j4 --debug-symbols=on --build-type=complete toolset=msvc-14.1 threading=multi runtime-link=shared address-model=32
注意这里指定的运行库类型是动态链接库:
runtime-link=shared
当然也可以选择静态库,这样指定即可:
runtime-link=static
根据电脑配置,太低可能要30分钟到一小时。然后等待编译完毕。
编译完后,屏幕会有下面的提示:
...updated 2376 targets... The Boost C++ Libraries were successfully built! The following directory should be added to compiler include paths: E:\boost_1_69_0\boost_1_69_0 The following directory should be added to linker library paths: E:\boost_1_69_0\boost_1_69_0\stage\lib
4,在VS2017中配置boost环境
项目属性 > 配置属性,然后看到下面的选择项:
常规 > 平台工具集,选择 Visual Studio 2017 (v141).
下面的两个操作,需要你将上面boost编译的时候告诉你的目录替换到下面说的有关目录信息里面去。
看到 "C\C++" 常规 > 附加包含目录,增加"E:\boost_1_69_0\boost_1_69_0"
最后,看到“链接器”常规 > 附加库目录,增加"E:\boost_1_69_0\boost_1_69_0\stage\lib"
注意:一定要进行这样正确的设置,否则编译使用boost的程序总是会提示有问题。
5,使用boost:
#include "stdafx.h" #include <iostream> #include <boost/thread/thread.hpp> void hello() { std::cout << "Hello world, I'm a thread!" << std::endl; } int main() { boost::thread thrd(&hello); thrd.join(); }
出错:
错误 LNK1104 无法打开文件“libboost_thread-vc140-mt-gd-1_63.lib”
解决办法:
因为上面选择的是以动态链接库的形式编译的boost库,所以这里要选择 多线程调试 DLL(/MDd)。
再去运行一下。就OK了。