1. 程式人生 > >boost安裝(windows、linux)

boost安裝(windows、linux)

boost是一個功能強大、構造精巧、跨平臺、開源並且完全免費的C++程式庫。

boost安裝(vs2017+boost_1_64_0+win10)

1、到官網下載boost,http://www.boost.org/
2、解壓,解壓到d:\boost目錄下,這個解壓到自己認為合適的目錄就行。
3、環境配置
VS2017更加註重跨平臺性,安裝檔案較多,VC有三個版本,分別是arm、Hostx64、Hostx86,本文使用Hostx64。
預設安裝時,編譯器cl.exe並不在環境變數中,需要配置。
新增環境變數PATH: D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\bin\HostX64\x64

執行VS2017開發人員命令提示,英文名稱x64 Native Tools Command Prompt for VS 2017 執行Developer Command Prompt for VS2017也可。
4、輸入命令 cd d:\boost\boost_1_64_0
5、輸入bootstrap.bat執行
這裡寫圖片描述

6、在d:\boost\boost_1_64_0目錄中生成了b2.exe和bjam.exe
這裡寫圖片描述

7、在目錄C:\boost\boost_1_64_0下有一個專案配置檔案project-config.jam,用記事本或其他文字編輯工具開啟,做如下修改:
這裡寫圖片描述

其中第二行的目錄是你VS2017的安裝目錄中cl.exe的位置。
8、執行 b2.exe stage --toolset=msvc-14.0address-model=64 --stagedir="C:\boost\bin1.63.0\VC14.0"threading=multi --build-type=complete

具體介紹:
–toolset:設定編譯器,如果用VC,設msvc, 用MinGW就設gcc。

stage:可選install,選stage只生成庫(靜態庫和動態庫),install還包含include目錄,其實,可以直接用我們下載下來的BOOST包裡的boost目錄,這個目錄和install生成的include目錄內容基本一樣。所以也就不用了。

–build-dir=”[temporary folder name”:編譯的臨時檔案存放位置。

–stagedir=” stage folder name]”:存放編譯後庫檔案的路徑,預設是stage。

–build-type=complete:編譯所有版本

{

variant=debug|release 決定編譯什麼版本(Debug or Release?)

link=static|shared 決定使用靜態庫還是動態庫。

threading=single|multi 決定使用單執行緒還是多執行緒庫。

runtime-link=static|shared 決定是靜態還是動態連結C/C++標準庫。

}

link:是動態庫還是靜態庫,static | shared,一般預設靜態。

address-mode:address-model=64,如果沒有這個屬性的話,會預設生成32位的平臺庫,加入這個選項才能生成64位的DLL。如果執行在VS32位的命令列下需要新增” architecture=x86”,筆者使用x64 Native Tools Command Prompt for VS 2017沒有x86與x64之間的矛盾,所以未設定。
這裡寫圖片描述
9、過一段時間後在資料夾d:\boost\boost_1_64_0\bin\vc14\lib下生成.dll及.lib檔案。d:\boost\boost_1_64_0\bin.v2是編譯產生的臨時目錄,可刪除。

10、安裝完成。

驗證開發環境
讓我們來編寫一個簡單的boost應用程式來驗證開發環境
在編寫程式碼前要在專案->屬性->c/c++ ->常規 “附加包含目錄”連結器->常規 “附加包含目錄”中新增之前生成的boost lib目錄
這裡寫圖片描述
這裡寫圖片描述

測試程式碼如下:

#include "stdafx.h"
#include<iostream>
#include<stdio.h>

#include<boost/version.hpp>	//包含boost標頭檔案
#include<boost/config.hpp>

int main()
{
	using namespace std;
	cout << BOOST_VERSION << endl;
	cout << BOOST_LIB_VERSION << endl;
	cout << BOOST_PLATFORM << endl;
	cout << BOOST_COMPILER << endl;
	cout << BOOST_STDLIB << endl;

	system("pause");
    return 0;
}

執行結果:
這裡寫圖片描述

boost安裝成功。

Linux下安裝boost(Ubuntu16.04-LTS)

最簡單的方法,執行sudo apt-get install libboost-dev

安裝後可在/usr/include中檢視到boost目錄。也可在Boost官網下載原始碼安裝。

執行完後可用如下程式碼測試是否安裝成功。

測試程式碼:

#include <iostream>
#include<boost/version.hpp>
#include<boost/config.hpp>

using namespace std;

int main()
{
    cout << BOOST_VERSION << endl;
    cout << BOOST_LIB_VERSION << endl;
    cout << BOOST_PLATFORM << endl;
    cout << BOOST_COMPILER << endl;
    cout << BOOST_STDLIB << endl;

  return 0;
}

安裝成功後執行結果:

105800
1_58
linux
GNU C++ version 5.4.0 20160609
GNU libstdc++ version 20160609