Boost windows入門
基於版本1.69.0
1. 獲得Boost源代碼
獲取Boost副本最好的方法是下載boost_1_69_0.7z或boost_1_69_0.zip並解壓縮以安裝完整的Boost發行版。
2. Boost源代碼組織 The Boost Distribution
這是Boost目錄結構草圖:
boost_1_69_0 \ ........................ “boost根目錄”
index.htm ................ 這是www.boost.org的副本
boost \ ......................... 所有Boost 頭文件
lib \ .......................... 預編譯庫二進制文件
libs\ ................. 測試,.cpps,docs等,通過庫
index.html ..................... 庫文檔從這里開始
algorithm\
any \
array \
...更多庫...
status\ .........................Boost-wide測試套件
tools\ ........... 實用工具,例如Boost.Build,quickbook,bcp
more \ ................................. 政策文件等
doc \ ........................ 所有Boost庫文檔的子集
重要的是要注意以下幾點:
- boost根目錄(boost root directory)(通常是
C:\Program Files\boost\boost_1_69_0)有時在boost文檔和郵件列表中也稱為$BOOST_ROOT。 - 要在Boost中編譯任何內容,您需要在
include目錄中包含boost\子目錄。 在Microsoft Visual Studio中設置include路徑的具體步驟將在本文檔的后面部分中介紹; 如果您使用其他IDE,請參閱產品文檔以獲取相關說明。 - 由於所有Boost頭文件都具有
.hpp擴展名,並且位於boost根目錄下的boost\子目錄中,因此你的Boost#include指令將如下所示:
#include <boost/whatever.hpp>
或
#include "boost/whatever.hpp"
取決於您對使用尖括號的偏好包括。即使是Windows用戶也可以(並且出於可移植性的原因,可能)在#include指令中使用正斜杠(forward slashes); 你的編譯器不關心。
- 不要被
doc\子目錄分散注意力; 它只包含Boost文檔的子集。如果要查看完整的文件,從libs\index.html開始。
3. 僅用頭文件的庫 Header-Only Libraries
許多人想要知道的第一件事是,“我如何構建Boost?”好消息是,通常沒有什么可構建的。
必須單獨構建的Boost庫是:
- Boost.Chrono
- Boost.Context
- Boost.Filesystem
- Boost.GraphParallel
- Boost.IOStreams
- Boost.Locale
- Boost.Log (see build documentation)
- Boost.MPI
- Boost.ProgramOptions
- Boost.Python (see the Boost.Python build documentation before building and installing it)
- Boost.Regex
- Boost.Serialization
- Boost.Signals
- Boost.System
- Boost.Thread
- Boost.Timer
- Boost.Wave
一些庫具有可選的單獨編譯的二進制文件:
- Boost.DateTime有一個二進制組件,只有在使用其
to_string/from_string或序列化功能時才需要,或者如果你的目標是Visual C ++ 6.x或Borland。 - Boost.Graph 有一個二進制組件,只有在你打算
解析GraphViz文件時才需要它。 - Boost.Math具有TR1和C99 cmath函數的二進制組件。
- Boost.Random有一個二進制組件,只有在你使用
random_device時才需要它。 - Boost.Test可用於“僅標題”或“單獨編譯”模式,但建議單獨編譯以供嚴肅使用。
- Boost.Exception為32位
_MSC_VER == 1310和_MSC_VER == 1400提供了exception_ptr的非侵入式實現,這需要單獨編譯的二進制文件。這是由#define BOOST_ENABLE_NON_INTRUSIVE_EXCEPTION_PTR啟用的。
4. 使用Boost構建一個簡單的程序 Build a Simple Program Using Boost
為了簡單起見,我們首先使用僅限標頭的庫。以下程序從標准輸入讀取整數序列,使用Boost.Lambda將每個數字乘以3,並將它們寫入標准輸出:
#include <boost/lambda/lambda.hpp>
#include <iostream>
#include <iterator>
#include <algorithm>
int main()
{
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(
in(std::cin), in(), std::cout << (_1 * 3) << " " );
}
將該程序的文本復制到名為example.cpp的文件中。
4.1 使用Visual Studio IDE構建
- 從Visual Studio 2017的“文件”菜單中,選擇“新建” > “項目”...
- 在生成的“新建項目”對話框的左側窗格中,選擇“已安裝” > “Visual C++” > “Windows桌面”。
- 在右側窗格中,選擇
Win32控制台應用程序。 - 在名稱字段中,輸入
example_boost - 右鍵單擊“解決方案資源管理器”窗格中的
example_boost,然后從彈出的菜單中選擇“屬性” - 在 “配置屬性” > “C/C++” > “常規” > “其他包含目錄” 中,輸入Boost根目錄的路徑
E:\dev\VS2017\boost_1_69_0
- 在“配置屬性” > “C/C++” > “預編譯標題”中,將“使用預編譯標題(/Yu)”更改為“不使用預編譯標題”。
- 使用上面的示例代碼替換IDE生成的example.cpp的內容。
- 從Build菜單中,選擇Build Solution。
要測試您的應用程序,請按F5鍵並在結果窗口中鍵入以下內容,然后按Return鍵:
1 2 3
然后按住控制鍵並按“Z”,然后按Return鍵。
4.2 從命令提示符構建 Or, Build From the Command Prompt
從計算機的“開始”菜單中,如果您是Visual Studio 2017用戶,請選擇
. Visual Studio 2017 > 適用於 VS 2017 的 x64 本機工具命令提示
為Visual Studio編譯器調出一個特殊的命令提示符窗口。在該窗口中,將當前目錄設置為適合創建一些臨時文件的位置,然后鍵入以下命令,后跟Return鍵:
cl /EHsc /I E:\dev\VS2017\boost_1_69_0 D:\Users\yaoyu\source\repos\example_boost\example_boost\example_boost.cpp
要測試結果,請鍵入:
echo 1 2 3 | example_boost
4.3 錯誤和警告 Errors and Warnings
如果您看到源自Boost頭文件的編譯器警告,請不要驚慌。我們試圖消除它們,但這樣做並不總是實用的。錯誤是另一回事。如果您在本教程中看到編譯錯誤,請檢查以確保您已正確復制示例程序並且已正確識別Boost根目錄。
5 准備使用Boost Library Binary
如果要使用任何單獨編譯的Boost庫,則需要獲取庫二進制文件。
5.1 使用源代碼簡單構建
如果您希望使用Visual C++從源代碼構建,可以使用本節中描述的簡單構建過程。打開命令提示符並將當前目錄更改為Boost根目錄(Boost root directory)。然后,鍵入以下命令:
bootstrap
.\b2
第一個命令准備Boost.Build系統以供使用。第二個命令調用Boost.Build來構建單獨編譯的Boost庫。有關允許的選項列表,請參閱Boost.Build文檔。
5.2 從源構建二進制文件
如果您使用的是早期版本的Visual C++或其他供應商的編譯器,則需要使用Boost.Build來創建自己的二進制文件。
5.2.1 安裝Boost.Build
Boost.Build是一個基於文本的系統,用於開發,測試和安裝軟件。首先,您需要構建並安裝它。步驟如下:
- 轉到目錄
tools\build\。 - 運行
bootstrap.bat - 運行
b2 install --prefix=PREFIX, 其中PREFIX是您希望安裝Boost.Build的目錄 - 將
PREFIX\bin添加到PATH環境變量中。
5.2.2 確定工具集 Identify Your Toolset
首先,在下表中找到與編譯器對應的工具集(Boost.Build文檔中始終提供最新列表)。
| Toolset Name | Vendor | Notes |
|---|---|---|
| acc | Hewlett Packard | Only very recent versions are known to work well with Boost |
| borland | Borland | |
| como | Comeau Computing | Using this toolset may require configuring another toolset to act as its backend. |
| darwin | Apple Computer | Apple's version of the GCC toolchain with support for Darwin and MacOS X features such as frameworks. |
| gcc | The Gnu Project | Includes support for Cygwin and MinGW compilers. |
| hp_cxx | Hewlett Packard | Targeted at the Tru64 operating system. |
| intel | Intel | |
| msvc | Microsoft | |
| sun | Oracle | Only very recent versions are known to work well with Boost. Note that the Oracle/Sun compiler has a large number of options which effect binary compatibility: it is vital that the libraries are built with the same options that your appliction will use. In particular be aware that the default standard library may not work well with Boost, unless you are building for C++11. The particular compiler options you need can be injected with the b2 command line options cxxflags=``and ``linkflags=. For example to build with the Apache standard library in C++03 mode use b2 cxxflags=-library=stdcxx4 linkflags=-library=stdcxx4. |
| vacpp | IBM | The VisualAge C++ compiler. |
如果安裝了特定編譯器的多個版本,則可以將版本號附加到工具集名稱,后面加一個連字符,例如intel-9.0或borland-5.4.3。在Windows上,即使您只安裝了一個版本(除非您使用的是具有特殊版本檢測代碼的msvc或gcc工具集),否則auto-linking會失敗。
5.2.3 選擇構建目錄 Select a Build Directory
Boost.Build會將構建時生成的所有中間文件放入構建目錄(build directory)中。如果您的Boost根目錄是可寫的,則此步驟不是必需的:默認情況下,Boost.Build將在您當前的工作目錄中為此創建bin.v2/子目錄。
5.2.4 調用b2 Invoke b2
將當前目錄更改為Boost根目錄, 並按如下方式調用b2:
b2 --build-dir=build-directory toolset=toolset-name --build-type=complete stage
有關這些和其他調用選項的完整說明,請參閱Boost.Build文檔。
例如,您的會話可能如下所示:
C:\WINDOWS> cd /d C:\Program Files\boost\boost_1_69_0
C:\Program Files\boost\boost_1_69_0> b2 ^
More? --build-dir="C:\Documents and Settings\dave\build-boost" ^
More? --build-type=complete msvc stage
請務必閱讀關於^, More?和該行中的引號(“)的說明。
選項 --build-type=complete 會讓Boost.Build構建所有支持的庫版本。有關如何僅構建特定版本的說明,請在Boost.Build郵件列表中查詢。
構建特殊階段(stage)目標將Boost庫二進制文件放在Boost樹的stage\lib\子目錄中。要使用其他目錄,請將--stagedir=directory選項傳遞給b2。
有關在調用b2時可以傳遞的其他選項的說明 ,請鍵入:
b2 --help
特別是,為了限制建設所花費的時間,您可能會對以下內容感興趣:
- 使用
--show-libraries查看庫名列表 - 使用
--with-library-name或--without-library-name選項選擇要構建(或不構建)的庫 - 通過向命令行添加
release或debug來選擇特定的構建版本。
5.3 預期的構建輸出 Expected Build Output
在構建Boost庫的過程中,您可以期望在控制台上看到一些消息。這些可能包括
- 關於Boost庫配置的注意事項 - 例如,Regex庫在沒有Unicode支持的情況下生成有關ICU的消息,如果沒有安裝Python,可以跳過Python庫而不會出現錯誤(但需要注意)。
- 來自構建工具的消息,用於報告已構建或跳過的目標數。如果這些數字對你沒有任何意義,不要感到驚訝; 每個庫有很多目標。
- 構建描述工具正在做什么的動作消息,如下所示:
toolset-name.c++ long/path/to/file/being/built
- 編譯器警告。
5.4 在構建錯誤的情況下 In Case of Build Errors
在構建Boost時看到的唯一錯誤消息(如果有的話)應該與IOStreams庫對zip和bzip2格式的支持有關。如果需要這些特性,請安裝libz和libbz2的相關開發包。構建Boost庫時的其他錯誤也值得關注。
如果它看起來像編譯系統無法找到你的編譯器和/或連接器,考慮設置一個user-config.jam文件。如果這不是您的問題或user-config.jam文件不工作,請解決有關將編譯器的Boost配置到Boost.Build郵件列表的問題。
6 將您的程序鏈接到Boost Library
為了演示與Boost二進制庫的鏈接,我們將使用以下簡單程序從電子郵件中提取主題行。它使用Boost.Regex庫,它具有單獨編譯的二進制組件。
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
std::string line;
boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );
while (std::cin)
{
std::getline(std::cin, line);
boost::smatch matches;
if (boost::regex_match(line, matches, pat))
std::cout << matches[2] << std::endl;
}
}
鏈接有兩個主要挑戰:
- 工具配置,例如, 選擇命令行選項或IDE構建設置。
- 在所有構建版本中識別庫二進制文件,其編譯配置與項目的其余部分兼容。
6.1 從Visual Studio IDE中鏈接
從我們之前創建的僅頭文件example項目開始:
- 右鍵單擊“解決方案資源管理器”窗格中的
example,然后從彈出的菜單中選擇“屬性” - 在
配置屬性>鏈接器>附加庫目錄中,輸入Boost二進制文件的路徑,例如E:\dev\VS2017\boost_1_69_0\lib64-msvc-14.1。 - 從Build菜單中,選擇Build Solution。
6.2 從命令提示符鏈接
例如,假設您的Boost二進制文件位於E:\dev\VS2017\boost_1_69_0\lib64-msvc-14.1中,我們可以通過簡單地將下面的粗體文本添加到我們之前使用的命令行,從Visual C++命令行編譯和鏈接上述程序。:
cl /EHsc /I E:\dev\VS2017\boost_1_69_0 example_boost.cpp ^
/link /LIBPATH:E:\dev\VS2017\boost_1_69_0\lib64-msvc-14.1
6.3 庫命名 Library Naming
為了配置正確的二進制文件,您需要知道Boost二進制文件是如何命名的。每個庫文件名由一組共同的元素序列組成,這些元素描述了它的構建方式。例如,libboost_regex-vc71-mt-d-x86-1_34.lib可以分解為以下元素:
lib
boost_regex
-vc71
-mt
-d
| Key | Use this library when: | Boost.Build option |
|---|---|---|
| s | linking statically to the C++ standard library and compiler runtime support libraries. | runtime-link=static |
| g | using debug versions of the standard and runtime support libraries. | runtime-debugging=on |
| y | using a special debug build of Python. | python-debugging=on |
| d | building a debug version of your code.6 | variant=debug |
| p | using the STLPort standard library rather than the default one supplied with your compiler. | stdlib=stlport |
例如,如果構建代碼的調試版本以用於靜態運行時庫和STLPort標准庫的調試版本,則標記將為:-sgdp。如果以上都不適用,則省略ABI標記。
-x86
| Key | Architecture | Boost.Build option |
|---|---|---|
| x | x86-32, x86-64 | architecture=x86 |
| a | ARM | architecture=arm |
| i | IA-64 | architecture=ia64 |
| s | Sparc | architecture=sparc |
| m | MIPS/SGI | architecture=mips* |
| p | RS/6000 & PowerPC | architecture=power |
字母后面的兩位數字對地址模型進行編碼,如下所示:
| Key | Address model | Boost.Build option |
|---|---|---|
| 32 | 32 bit | address-model=32 |
| 64 | 64 bit | address-model=64 |
-1_34
.lib
6.4 測試 Test Your Program
為了測試我們的主題提取,我們將過濾以下文本文件。將其從瀏覽器中復制並保存為jayne.txt:
To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
現在,在命令提示符窗口中,鍵入:
path\to\compiled\example < path\to\jayne.txt
D:\Users\yaoyu\source\repos\example_boost\example_boost>example_boost.exe < jayne.txt
程序應輸出電子郵件主題, “Will Success Spoil Rock Hunter?”
7 結論和進一步的資源 Conclusion and Further Resources
最后介紹了Boost並將其與您的程序集成。當你開始認真地使用Boost時,肯定會有一些你希望我們覆蓋的額外點。有一天,我們可能會在“入門系列”中找到“第2冊”來解決這些問題。在此之前,我們建議您繼續使用以下資源。如果您無法找到所需內容,或者我們可以采取任何措施使此文檔更加清晰,請將其發布到Boost用戶的郵件列表中。
