boost庫 - Windows vs2005 安裝嚮導

轉載:http://blog.sina.com.cn/s/blog_76fbd24d01018mag.html

1. 下載文件包:
boost_1_44_0.zip

2. 解壓縮至自定義根目錄:
D:\boost\boost_1_44_0

3. 安裝配置VS2005:
【開始】->【所有程序】->Microsoft Visual Studio 2005】->Visual Studio Tools】->Visual Studio 2005 命令提示】以確保Visual Studio中C++目錄下的環境vcvarsall.bat配置腳本得到運行,以配置VS2005的編譯器環境變量。

4. 編譯獲取bjam:
執行D:\boost\boost_1_44_0\tools\jam\src\build.bat,將會生成D:\boost\boost_1_44_0\tools\jam\src\bin.ntx86\bjam.exe,將其複製到D:\boost\boost_1_44_0根目錄,確保與boost-build.jam同一目錄。

5. 設定編譯環境:
修改D:\boost\boost_1_44_0\tools\build\v2\user-config.jam,在
# -------------------
# MSVC configuration.
# -------------------

# Configure msvc (default version, searched for in standard locations and PATH).
# using msvc ;

# Configure specific msvc version (searched for in standard locations and PATH).
# using msvc : 8.0 ;
下方添加:
using msvc : 8.0 : : <compileflags>/wd4819 <compileflags>/D_CRT_SECURE_NO_DEPRECATE <compileflags>/D_SCL_SECURE_NO_DEPRECATE <compileflags>/D_SECURE_SCL=0 ;

注意:如果是VS2008或VS2010則,分別要對應修改成:

using msvc : 9.0 : : <compileflags>/wd4819 <compileflags>/D_CRT_SECURE_NO_DEPRECATE <compileflags>/D_SCL_SECURE_NO_DEPRECATE <compileflags>/D_SECURE_SCL=0 ;

using msvc : 10.0 : : <compileflags>/wd4819 <compileflags>/D_CRT_SECURE_NO_DEPRECATE <compileflags>/D_SCL_SECURE_NO_DEPRECATE <compileflags>/D_SECURE_SCL=0 ;

其中,/wd 是Visual C++ 編譯器選項,用於指定編譯器如何爲給定編譯生成警告。
/wd n 表示禁用指定的編譯器警告,其中 n 是編譯器警告編號。例如,/wd4819 禁用編譯器警告 C4819。
在系統中編譯 ANSI 源文件時,如果有一個代碼頁不能表示該文件中的所有字符,則會出現 C4819 警告。
/D(預處理器定義)用於定義源文件的預處理符號。_CRT_SECURE_NO_DEPRECATE、_SCL_SECURE_NO_DEPRECATE、_SECURE_SCL等預處理宏定義分別能屏蔽一部分warning警告信息,詳細參見MSDN。
因此,上述添加內容中只是添加了幾個編譯選項,用於屏蔽一些非必要警告信息。

6. 編譯boost:
在D:\boost\boost_1_44_0根目錄下執行:
bjam --without-python --toolset=msvc-8.0 --build-type=complete --prefix="D:\boost\boost_1_44_0" install
注意:編譯需要時間較長,通常爲1小時左右。
如果是VS2008或VS2010,分別對應的命令爲:
bjam --without-python --toolset=msvc-9.0 --build-type=complete --prefix="D:\boost\boost_1_44_0" install
bjam --without-python --toolset=msvc-10.0 --build-type=complete --prefix="D:\boost\boost_1_44_0" install

7. 參數說明:
stage/install:stage表示只生成庫(dll和lib),install還會生成包含頭文件的include目錄。
toolset:指定編譯器,可選的如borland、gcc、msvc(VC6)、msvc-8.0(VS2005)等。
without/with:選擇不編譯/編譯哪些庫。因爲python庫用不着,所以排除之。
stagedir/prefix:stage時使用stagedir,install時使用prefix,表示編譯生成庫文件的路徑。
build-type:編譯類型,complete表示生成所有的版本(debug,release等)。

8. 配置VS2005開發環境:
【工具】->【選項】->【項目和解決方案】->【VC++ 目錄】分別添加:
包含文件目錄:D:\boost\boost_1_44_0\include\boost-1_44
庫文件目錄:D:\boost\boost_1_44_0\lib

9. 編寫程序測試:
#include <iostream>

#include <boost/lexical_cast.hpp>

int main()
{
    int a = boost::lexical_cast<int>("123");
    double b = boost::lexical_cast<double>("123.0123456789");
    std::string s0 = boost::lexical_cast<std::string>(a);
    std::string s1 = boost::lexical_cast<std::string>(b);
    std::cout << "number is: " << a << " " << b << std::endl;
    std::cout << "string is: " << s0 << " " << s1 << std::endl;

    int c = 0;
    try
    {
        c = boost::lexical_cast<int>("abcd");
    }
    catch (boost::bad_lexical_cast & e)
    {
        std::cout << e.what() << std::endl;
        return -1;
    }

    return 0;
}

10. 執行結果:
number is: 123 123.012
string is: 123 123.0123456789
bad lexical cast: source type value could not be interpreted as target
請按任意鍵繼續. . .

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章