vs2015 安装boost库
背景
CDNS目前关于vs编译boost库很多是使用较新的vs以及boost库,关于使用vs2015完整编译boost库的文章较少;如果你在编译过程中出现“toolset”的错误,以及vc141之类的版本错误;前者主要是boost库版本的问题;后者则是因为电脑安装多个vs版本造成的;
编译过程
(1)在官网链接 https://www.boost.org/users/history/version_1_70_0.html
上下载boost1.70.0版本,这里一定要选择boost版本 1.70,不要选择太高或太低的版本,并下载.zip格式文件不要去下载官网提供的.exe程序之类的
(2)在开始菜单栏中找到vs2015 x86 本机工具命令提示符;注意尽量选择X86格式;
(3)切换到boost目录下,可以看到bootstrap.bat文件,在vs2015 x86 本机工具命令提示符;输入bootstrap.bat vc14 注意:不要少了vc14 这个是针对vs2015的;具体版本对应如下:
输入成功后会出现如下图所示:
(4)然后按照提示,输入.\b2开始编译,编译工程有些漫长;编译成功后如下所示:
编译结束后,在文件中将会多出一个stage文件夹;打开stage文件夹后,如下所示
(5)再添加在属性/VC++中 包含目录和库目录:D:\boost_1_70_0\boost_1_70_0 和D:\boost_1_70_0\boost_1_70_0\stage\lib
测试boost库是否正常,测试代码如下:
#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/timer.hpp>
using namespace std;
using namespace boost;
void hello()
{
cout << "hello boost" << endl;
}
int main()
{
timer t;
boost::thread thrd(&hello);
thrd.join();
cout << t.elapsed_max() / 3600 << endl;//最大时间,单位:小时
cout << t.elapsed_min() << endl; //最小统计时间,单位:秒
cout << t.elapsed() << endl;//从建立对象开始,时间流失统计
return 0;
}
测试中如果出现错误(1):无法打开文件“libboost_system-vc141-mt-sgd-x32-1_66.lib”
解决方法,右键属性——C/C++——代码生成——运行库,改为多线程调试 DLL (/MDd)
出现链接错误(2)error "Incompatible build options如下:
这种错误主要是link.hpp中的连接错误,这种连接错误似乎解决的办法都是注释掉对应的代码部分。找到对应的代码
//#if defined(__MSVC_RUNTIME_CHECKS) && !defined(_DEBUG)
//# pragma message("Using the /RTC option without specifying a debug runtime will lead to linker errors")
//# pragma message("Hint: go to the code generation options and switch to one of the debugging runtimes")
//# error "Incompatible build options"
//#endif
//
// select toolset if not defined already:
//
注释后,正常运行的结果如下所示:
说明调用成功!