一、boost是一個准標准庫,相當於STL的延續和擴充,它的設計理念和STL比較接近,都是利用泛型讓復用達到最大化。不過對比STL,boost更加實用。STL集中在算法部分,而boost包含了不少工具類,可以完成比較具體的工作。考慮到boost的強大,為此特地里做了windows下移植編譯操作。
二、boost的移植
1.下載boost源碼boost_1_62_0.7z,下載地址:https://sourceforge.NET/projects/boost/
其實也可以下載boos編譯好的庫和頭文件,不過為了不必要的麻煩,建議手動編譯
2.編譯boost
1)解壓boost到d盤,目錄為boost_1_62
2)生成bjam工具:
進入D:\boost_1_62_0\boost_1_62_0\tools\build\src\engine目錄下,執行build.sh gcc,在當前目錄將會生成bin.ntx86文件夾,里面包含兩個exe文件b2.exe,bjam.exe
3)將bin.ntx86\bjam.exe拷貝到boost1.37的解壓目錄D:\boost_1_62_0\boost_1_62_0中
4)進入路徑D:\boost_1_62_0\boost_1_62_0,執行 bjam "toolset=gcc" install ,等待一段時間后,會在C盤根目錄下生成一個boost文件夾,里面放着生成的頭文件以及LIB和DLL文
5)將C:\Boost\include\boost-1_37目錄下的boost文件夾拷貝到C:\MinGW\include下面
6)將C:\Boost\lib下的lib文件拷貝到C:\MinGW\lib,將C:\Boost\lib下的dll文件拷貝到C:\MinGW\bin
三、boost的使用
程序代碼入下:
- #include <iostream>
- #include <boost/math/special_functions/acosh.hpp>
- #include <boost/math/special_functions/bessel.hpp>
- #include <string>
- #include <boost/filesystem.hpp>
- #include <boost/timer.hpp>
- using namespace boost::math;
- using namespace boost::math::detail;
- namespace fs = boost::filesystem;
- //測試boost貝塞爾函數
- void testBessel(){
- std::cout<<"Test Boost:"<<std::endl;
- std::cout<<acosh(2.5)<<std::endl;
- std::cout<<bessel_i0(3.2)<<std::endl;
- std::cout<<"Test Finished!"<<std::endl;
- }
- //測試boost文件系統庫
- void testFileSystem(){
- fs::path full_path("c:");
- fs::directory_iterator end_iter;
- for ( fs::directory_iterator dir_itr( full_path ); dir_itr != end_iter; ++dir_itr )
- {
- std::cout << dir_itr->path().filename() << std::endl;
- }
- }
- int main(int argc, char *argv[])
- {
- std::cout << "-----測試boost貝塞爾函數-------" << std::endl;
- testBessel();
- std::cout << "-----測試boost文件系統庫------" << std::endl;
- testFileSystem();
- return 0;
- }
在xxx_pro中添加,
LIBS += -LC:\Qt\mingw\lib -lboost_system -lboost_filesystem
運行效果如下,
- Starting D:\Documents\build-cplusplusboost-unknown-Debug\debug\cplusplusboost.exe...
- -----測試boost貝塞爾函數-------
- Test Boost:
- 1.5668
- 5.74721
- Test Finished!
- -----測試boost文件系統庫------
- "$RECYCLE.BIN"
- "Boost"
- "Boot"
- "bootmgr"
- "Documents and Settings"
- "PerfLogs"
- "Program Files"
- "Program Files (x86)"
- "ProgramData"
- "Qt"
- "RECYCLER"
- "System Volume Information"
- "Users"
- "Windows"
http://blog.csdn.net/xiaopangzi313/article/details/52800799
