vs2015編譯boost庫並調用

1 編譯boost庫

在GitHub上下載的boost庫,需要自己編譯成我們使用的版本纔可以供vs調用。這裏以在官網上下載最新的boost-1.70.0下載地址爲例進行編譯成供vs2015使用的boost庫。下載完boost壓縮包後進行解壓,解壓後我們以管理員權限運行bootstrap.bat,會自動在當前目錄生成兩個文件b2.exe和bjam.exe,其實這兩個文件的功能基本一樣,新版本推薦使用b2進行編譯。

以管理員權限打開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

在這裏插入圖片描述
–toolset=msvc-14.0表示生成msvc14的庫,即爲供vs2015調用的;
–without-python --without-graph --without-graph_parallel 表示不生成這些庫;–stagedir="D:\boost_1_70_0\vc14_32_release"表示存放生成boost庫的目錄,這個目錄是由自己新建的;link=static代表生成靜態庫,boost一般都生成靜態庫,如果要生成共享動態庫,指定爲link=shared;runtime-link=static表示msvc運行時爲MT;threading=multi表示多線程;release爲生成release版本的

等待生成完成,會在D:\boost_1_70_0\vc14_32_release生成一個lib目錄,這個目錄下就是boost生成的靜態庫。
在這裏插入圖片描述

2 在vs2015中測試編譯的boost庫

接下來測試一下boost靜態庫是否可以使用,在vs2015中新建一個工程,工程的配置如下:
C/C++ 常規 附加包含目錄爲:D:\boost_1_70_0
在這裏插入圖片描述
鏈接器 常規 附加庫目錄爲:D:\boost_1_70_0\vc14_32_release\lib
在這裏插入圖片描述
C/C++ 代碼生成 運行庫爲:多線程MT
在這裏插入圖片描述
接下里就可以調用boost庫了,下面爲測試代碼:

#include<iostream>
#include<string>
#include<boost/thread.hpp>
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;
}

運行如下圖所示代表boost庫編譯成功。
在這裏插入圖片描述

3 編譯命令的補充說明

生成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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章