Boost的15个库(包括data_time,regex,program_options、test、thread等)必须编译成静态库或动态库后才能使用。对于官网下载的Boost库,需要编译成我们使用的版本才可以供vs调用。
这里以编译供vs2015使用的boost库为例:
b2 stage --toolset=msvc-14.0 --without-python --without-graph --without-graph_parallel --stagedir="D:\boost_1_70_0\vc14_32_release" link=static runtime-link=static threading=multi release
以上文的命令为例:
b2 stage --toolset=msvc-14.0 --without-python --without-graph --without-graph_parallel --stagedir="D:\boost_1_70_0\vc14_32_release" link=static runtime-link=static threading=multi release
生成vc14的32位的debug版本的
b2 stage --toolset=msvc-14.0 --without-python --without-graph --without-graph_parallel --stagedir="D:\boost_1_70_0\vc14_32_release" link=static runtime-link=static threading=multi debug
生成vc14的64位版本库
b2 stage --toolset=msvc-14.0 --without-python --without-graph --without-graph_parallel --stagedir="D:\boost_1_70_0\vc14_32_release" address-model=64 link=static runtime-link=static threading=multi release
很多网页上教程写的untime-link=shared runtime-link=static threading=multi debug release表示生成的库既可以用于msvc即为MT也可以用于MD,既可以用于debug也可以用于release
新建一个win32控制台工程。进行工程配置
C/C++ 常规 附加包含目录为:D:\boost_1_66_0
注:编译的时候要注意“附加库目录”和“附加库目录”分别是在“Release”模式下设置的还是“Debug”模式下设置的
链接器 常规 附加库目录为:D:\boost_1_66_0\vc14_32_release\lib
C/C++ 代码生成 运行库为:多线程MT(因为上述编译命令指定了runtime-link=static
)
链接器 常规 附加库目录为:D:\boost_1_66_0\vc14_32_release\lib
#include
#include
#include
using namespace std;
void mythread()
{
cout << " hello,thread! " << endl;
}
int main()
{
boost::function<void()> f(mythread);
boost::thread t(f);
t.join();
cout << " thread is over! " << endl;
getchar();
return 0;
}
注:因为我们编译的是vc14的32位的release版本
所以编译选项如下:
运行程序后得到:
跟之前一样,但是在编译时需要编译thread及date time库(thread链接时需要)
bjam --toolset=msvc-10.0 --with-date_time stage
bjam --toolset=msvc-10.0 --with-thread stage
stage参数:编译成功后在boost_1_57_0\stage目录下生成相应的lib文件
2.在vs2010中配置boost
(1)属性->VC++目录->包含目录:D:\boost_1_66_0
(2)属性->VC++目录->库目录:D:\boost_1_66_0\stage\lib
3.在vs2010中测试代码就ok
b2 stage --toolset=msvc-10.0 --without-python --without-graph --without-graph_parallel --stagedir="D:\boost_1_66_0\vc10_32_debug" link=static threading=multi debug