今日安裝一個PSI庫時,需要boost庫,在此認識一下boost庫,轉載:macOS 中Boost的安裝和使用
介紹
Boost是一個功能強大,構造精良,跨越平台,代碼開源,完全免費的C++程序庫。
共包含160余個庫/組件,涵蓋字符串與文本處理、容器、迭代器、算法、圖像處理、模板元編程、並發編程等多個領域。
由c++標准委員會成員發起倡議並建立boost社區,C++11標准庫中三分之二來自boost,並且將來還會有更多的庫進入c++標准庫,因此boost是一個c++ "准"標准庫。
支持現有的所有操作系統。
Boost庫的大多數組件不需要編譯鏈接,源碼里直接包含頭文件。(注意:包含頭文件的時候需要有boost目錄,即#include "boost/logic/tribool.hpp"
,而不能是#include "logic/tribool.hpp")
剩下的少量庫(如chrono,date_time,program_options,test,threa
等)必須編譯成靜態庫或動態庫,並在構建時指定鏈接選項才能使用。
Boost的獨特之處:它把C++類的聲明和實現放在了一個文件中,而不是分成兩個文件,即.h+.cpp,故文件的后綴是.hpp。
功能
含有的功能類有:
- 字符串和文本處理庫
- 容器庫
- 迭代器庫
- 算法庫
- 函數對象和高階編程庫
- 泛型編程庫
- 模板元編程
- 預處理元編程庫
- 並發編程庫
- 數學和數字庫
- 排錯和測試庫
- 數據結構庫
- 圖像處理庫
- 輸入輸出庫
- 跨語言混合編程庫
- 內存管理庫
- 解析庫
- 編程接口庫
- 綜合類庫
- 編譯器問題的變通方案庫
目錄結構
--boost:最重要的目錄,90%以上的Boost程序庫源碼都在這里
--doc:HTMI格式的文檔,也可以生成PDF格式的文檔
--libs:所有組件的示例、測試、編譯代碼和說明文檔
--more:庫作者的相關文檔
--status:可用於測試Boost庫的各個組件
--tools:用於編譯Boost的工具的源代碼等
安裝Boost
使用源碼安裝
(1)下載Boost源碼
(2)解壓放在任意目錄,例如/usr/local/boost_1_63_0
./bootstrap.sh
./b2 headers
./b2
./b2 install
安裝時間稍長,生成的靜態(同態)文件都在/usr/local/lib中,需要cp到/usr/lib中,可以在代碼中直接使用;頭文件在/usr/local/include中。
使用Homebrew安裝
(1)下載安裝HomeBrew
brew install boost
(2)留意運行日志會顯示頭文件目錄 /usr/local/Cellar/boost/1.60.0_2/include, lib目錄/usr/local/Cellar/boost/1.60.0_2/lib
使用MacPort安裝
下載安裝MacPort
sudo port install boost
在CLion中使用Boost
(1)新建一個C++項目
(2)在cmakelists中 增加頭文件目錄
include_directories(/Users/pam/Desktop/pam/boost_1_78_0/)
或者
find_package(Boost 1.70.0 REQUIRED)
if(Boost_FOUND)
set(Boost_LIBRARY_DIRS D:/ScanSource/download/Boost/vc141_64/lib)
message(Boost_INCLUDE_DIRS " ${Boost_INCLUDE_DIRS}")
message(Boost_LIBRARY_DIRS " ${Boost_LIBRARY_DIRS}")
endif()
include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})
(3)替換main.cpp中代碼,運行!輸入任意數字回車可看到結果。
在XCode項目中使用Boost
(1)新建一個Command Line Tool項目
(2)在Build Setings - Header Search Paths 增加頭文件目錄
(3)替換main.cpp中代碼,運行!輸入任意數字回車可看到結果。
#include <iostream>
#include <boost/lambda/lambda.hpp>
int main(int argc, const char * argv[]) {
printf("Please input any number:");
using namespace boost::lambda;
typedef std::istream_iterator<int> in;
std::for_each(in(std::cin), in(), std::cout << (_1 * 3) << " " );
return 0;
}
在XCode項目中使用Boost Lib庫
Boost的很多功能都是直接在hpp頭文件里實現的,比如上面的lambda例子不用導入任何lib就可以運行了。但也有一部分需要依賴指定lib庫才能使用。比如下面這個正則表達式的例子:
#include <iostream>
#include <boost/regex.hpp>
int main(int argc, const char * argv[]) {
std::string str = "2013-08-15";
boost::regex rex("(?<year>[0-9]{4}).*(?<month>[0-9]{2}).*(?<day>[0-9]{2})");
boost::smatch res;
std::string::const_iterator begin = str.begin();
std::string::const_iterator end = str.end();
if (boost::regex_search(begin, end, res, rex))
{
std::cout << "Day: " << res ["day"] << std::endl
<< "Month: " << res ["month"] << std::endl
<< "Year: " << res ["year"] << std::endl;
}
}
使用靜態庫
在Build Setings - Other linker flags
/usr/local/boost_1_63_0/stage/lib/libboost_regex.a
使用命令行編譯相當於
c++ -I /usr/local/boost_1_63_0 main.cpp -o main /usr/local/boost_1_63_0/stage/lib/libboost_regex.a
./main
如果這里直接使用lboost_regex, Xcode默認加載動態庫。實際運用中可以考慮將目錄中的動態庫刪除,只保留靜態庫,並在Build Setings - Library Search Paths 增加lib文件目錄。
使用動態庫
(1)在Build Setings - Library Search Paths 增加lib文件目錄
(2)將lib文件目錄中的libboost_regex.dylib文件拖入項目
(3)確保在Build Phases - Link Bindary With Libraries中已經有該庫
(4)在Build Phases - Copy Files, 復制libboost_regex.dylib到Products Directory
使用命令行編譯相當於
c++ -I /usr/local/boost_1_63_0 main.cpp -o main -L/usr/local/boost_1_63_0/stage/lib/ -lboost_regex
cp /usr/local/boost_1_63_0/stage/lib/libboost_regex.dylib ./
./main
例子:
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include "boost/version.hpp"
using namespace std;
void version()
{
cout << BOOST_LIB_VERSION << endl;
cout << BOOST_VERSION << endl;
}
int main()
{
using namespace std;
int i;
char c;
double d;
boost::tuples::tie(i,c,d);
boost::tuples::tie(i,c,d) = boost::tuples::tuple <int,char,double>(1,'A',0.68);
cout << d <<endl;
cout<<"BOOST的版本:";
version();
return 0;
}
參考
1、Boost(1):Boost庫簡介及安裝
2、Boost庫簡介
3、https://zhuanlan.zhihu.com/p/85806857
4、https://blog.csdn.net/weixin_39766005/article/details/120305393