學習使用BOOST庫總結(應用環境 Windows XP + VC6.0)

來源:互聯網
上載者:User

1、下載

從www.boost.org下載boost_1_38_0.zip 和 boost-jam-3.1.17-1-ntx86.zip。
  
2、安裝構建工具bjam

解壓boost-jam得到bjam.exe,拷貝到path路徑中(例如“windows目錄”),
就是說在cmd命令環境中執行bjam命令時系統應該能找到bjam.exe可執行檔。

 

3、安裝boost庫

解壓boost_1_38_0.zip到你準備安裝boost的路徑下,建議選擇空間相對充足的分區,解壓後將近200M;
例如,我準備安裝到c區,於是在c區建立Boost檔案夾,並將壓縮包其解壓到其中,開啟C:/Boost/boost_1_38_0
便可以看到一堆檔案夾和檔案。

 

4、增加環境變數

我的電腦->屬性->進階->環境變數,在使用者變數中增加環境變數:

BOOST_BUILD_PATH 值為 C:/Boost/boost_1_38_0/tools/build/v2/

 

5、配置編譯環境

C:/Boost/boost_1_38_0/tools/build/v2中的user-config.jam檔案,最後增加一行:

using msvc : 6.0 : D:/Program Files/Microsoft Visual Studio/VC98/Bin/cl ;

注意上行的格式,冒號兩邊一定有空格,最後分號一定有空格(因為c++代碼中並沒有
如此苛刻的格式限制,所以一開始沒太注意,總也解決不了錯誤,最後試著才找到這個原因)。

 

6、建立Hello World!

到這裡就可以使用bjam來建立應用程式了,寫個hello world,找點成就感吧。
建立檔案D:/Test/Boost/hello/hello.cpp(路徑可隨意,我喜歡把所有練習程式放在一個Test檔案夾中,
然後把VC練習放在D:/Test/VC/檔案中,GCC練習放在D:/Test/GCC/檔案夾中,Boost練習當然要再建一個D:/Test/Boost),hello.cpp內容如下:
//<
#include <iostream>
using namespace std;
int main()
{
 std::cout<<"Hello, World!";
 return 0;
}
//>

在檔案夾D:/Test/Boost/hello/中建立名為jamroot的文字檔,內容(下面三行):
# jamfile for project hello

exe hello : hello.cpp ;

install dist : hello : <location>. ;

進入cmd命令列,並進入項目路徑:D:/Test/Boost/hello/>

執行編譯命令:D:/Test/Boost/hello/>bjam hello;
會在給檔案夾下產生bin/msvc-6.0/debug/threading-multi檔案夾,並在其中編譯產生hello.exe檔案

執行安裝命令:D:/Test/Boost/hello/>bjam dist;
會將產生的hello.exe檔案拷貝到D:/Test/Boost/hello/下

執行Hello World:D:/Test/Boost/hello/>hello;
會看到可愛的Hello, World!

 

7、使用boost庫

修改hello.cpp檔案如下:

#include <iostream>
#include <string>
#include <boost/lexical_cast.hpp>
using namespace std;

int main()
{
 //std::cout<<"hello, world!";

 string str = "520";

 try
 {
  cout << boost::lexical_cast<int>(str) << endl;
 }
 catch (boost::bad_lexical_cast& e)
 {
  cerr<<"Bad cast: "<< e.what() << endl;
 }

 return 0;
}

如果就這樣編譯的話會提示找不到boost/lexical_cast.hpp檔案,
需要修改jamroot檔案,增加包含boost路徑:

# jamfile for project hello

exe hello : hello.cpp : <include>C:/Boost/boost_1_38_0/ ;

install dist : hello : <location>. ;

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.