<menu id="w8yyk"><menu id="w8yyk"></menu></menu>
  • <dd id="w8yyk"><nav id="w8yyk"></nav></dd>
    <menu id="w8yyk"></menu>
    <menu id="w8yyk"><code id="w8yyk"></code></menu>
    <menu id="w8yyk"></menu>
    <xmp id="w8yyk">
    <xmp id="w8yyk"><nav id="w8yyk"></nav>
  • 網站首頁 > 物聯資訊 > 技術分享

    boost 1.56.0 編譯及使用

    2016-09-28 00:00:00 廣州睿豐德信息科技有限公司 閱讀
    睿豐德科技 專注RFID識別技術和條碼識別技術與管理軟件的集成項目。質量追溯系統、MES系統、金蝶與條碼系統對接、用友與條碼系統對接

    boost的編譯和使用,經過搜集資料和總結,記錄成文。感謝文后所列參考資料的作者。

    1 下載

    地址:http://sourceforge.net/projects/boost/files/boost/1.56.0/

    可以選擇 boost_1_56_0.7z 下載。

    2 編譯

    2.1 生成boost的自用的編譯工具bjam.exe

    解壓后,使用VS2013編譯。首先打開“VS2013 開發人員命令提示”,cd 到boost解壓后的根目錄:E:\XXX\boost_1_56_0,執行bootstrap.bat。會在boost根目錄生成 b2.exe 、bjam.exe 、project-config.jam 、bootstrap.log四個文件。

    其中,b2.exe 、bjam.exe 這兩個exe作用是一樣的,bjam.exe 是老版本,b2是bjam的升級版本。

    2.2 使用bjam(或b2)來編譯boost

    1. bjam命令參數分析

    我們以文章【1】中的命令來分析一下各個參數的作用(原作者解壓后的boost根目錄為E:\SDK\boost)。

    bjam stage --toolset=msvc-9.0 --without-python --stagedir="E:\SDK\boost\bin\vc9" link=static runtime-link=shared runtime-link=static threading=multi debug release

    (1)stage/install:

    stage表示只生成庫(dll和lib),install還會生成包含頭文件的include目錄。本人推薦使用stage,因為install生成的這個include目錄實際就是boost安裝包解壓縮后的boost目錄(E:\SDK\boost\boost,只比include目錄多幾個非hpp文件,都很小),所以可以直接使用,而且不同的IDE都可以使用同一套頭文件,這樣既節省編譯時間,也節省硬盤空間。

    (2)toolset:

    指定編譯器,可選的如borland、gcc、msvc(VC6)、msvc-9.0(VS2008)等。

    (3)without/with:

    選擇不編譯/編譯哪些庫。因為python、mpi等庫我都用不著,所以排除之。還有wave、graph、math、regex、test、program_options、serialization、signals這幾個庫編出的靜態lib都非常大,所以不需要的也可以without掉。這可以根據各人需要進行選擇,默認是全部編譯。但是需要注意,如果選擇編譯python的話,是需要python語言支持的,應該到python官方主頁http://www.python.org/下載安裝。

    查看boost包含庫的命令是bjam --show-libraries

    (4)stagedir/prefix:

    stage時使用stagedir,install時使用prefix,表示編譯生成文件的路徑。推薦給不同的IDE指定不同的目錄,如VS2008對應的是E:\SDK\boost\bin\vc9,VC6對應的是E:\SDK\boost\bin\vc6,否則都生成到一個目錄下面,難以管理。如果使用了install參數,那么還將生成頭文件目錄,vc9對應的就是E:\SDK\boost\bin\vc9\include\boost-1_46\boost,vc6類似(光這路徑都這樣累贅,還是使用stage好)。

    (5)build-dir:

    編譯生成的中間文件的路徑。這個本人這里沒用到,默認就在根目錄(E:\SDK\boost)下,目錄名為bin.v2,等編譯完成后可將這個目錄全部刪除(沒用了),所以不需要去設置。

    (6)link:

    生成動態鏈接庫/靜態鏈接庫。生成動態鏈接庫需使用shared方式,生成靜態鏈接庫需使用static方式。一般boost庫可能都是以static方式編譯,因為最終發布程序帶著boost的dll感覺會比較累贅。

    (7)runtime-link:

    動態/靜態鏈接C/C++運行時庫。同樣有shared和static兩種方式,這樣runtime-link和link一共可以產生4種組合方式,各人可以根據自己的需要選擇編譯。

    (8)threading:

    單/多線程編譯。一般都寫多線程程序,當然要指定multi方式了;如果需要編寫單線程程序,那么還需要編譯單線程庫,可以使用single方式。

    (9)debug/release:

    編譯debug/release版本。一般都是程序的debug版本對應庫的debug版本,所以兩個都編譯。

    2. 編譯boost

    編譯boost的命令比較復雜,尤其是 link, runtime-link 這兩個選項的功能分不太清楚,他們共有4種相互組合,這些相互組合各有什么含義呢?

    所以首先做個實驗,僅編譯date_time庫,觀察一下這兩個選項的作用。

    分別使用下面的命令行編譯, 

    b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=static runtime-link=static threading=multi debug release
    b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=static runtime-link=shared threading=multi debug release
    b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=shared runtime-link=shared threading=multi debug release
    b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12" link=shared runtime-link=static threading=multi debug release
    b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12_2" (為避免將前面的結果覆蓋,配置另一目錄vc12_2存放)
    b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12_2" --build-type=complete(為避免將前面的結果覆蓋,配置另一目錄vc12_3存放)
    
    
    

    所得到的結果如下表所示:

    序號 link runtime-link 生成物 備注 1 static static

    libboost_date_time-vc120-mt-sgd-1_56.lib

    libboost_date_time-vc120-mt-s-1_56.lib

      2 static shared

    libboost_date_time-vc120-mt-gd-1_56.lib

    libboost_date_time-vc120-mt-1_56.lib

    與5結果相同 3 shared shared

    boost_date_time-vc120-mt-gd-1_56.dll

    boost_date_time-vc120-mt-gd-1_56.lib

    boost_date_time-vc120-mt-1_56.dll

    boost_date_time-vc120-mt-1_56.lib

      4 shared static 報錯,無法編譯   5 使用缺省 使用缺省

    libboost_date_time-vc120-mt-gd-1_56.lib

    libboost_date_time-vc120-mt-1_56.lib

    與2結果相同

    并且在省略debug release時,debug release版本都編譯

    6 使用--build-type=complete

    boost_date_time-vc120-mt-gd-1_56.dll

    boost_date_time-vc120-mt-gd-1_56.lib

    boost_date_time-vc120-mt-1_56.dll

    boost_date_time-vc120-mt-1_56.lib

     

    libboost_date_time-vc120-mt-sgd-1_56.lib

    libboost_date_time-vc120-mt-s-1_56.lib

     

    libboost_date_time-vc120-mt-gd-1_56.lib

    libboost_date_time-vc120-mt-1_56.lib

     

    libboost_date_time-vc120-s-1_56.lib

    libboost_date_time-vc120-sgd-1_56.lib

     --build-type=complete時,可以看到link,runtime-link的

    3種組合下debug, release的多線程版本都生成出來了,

    除此之外,還生成了link=static,runtime-link=static的debug, release的單線程版本

    從上面的結果可以看到,link和runtime-link的缺省配置是 link=static runtime-link=shared,所以我們可以使用 (b2 stage --toolset=msvc-12.0 --with-date_time --stagedir="E:\eCode\boost_1_56_0\bin\vc12_2")命令行來編譯boost。

    另外,我們還可以分析一下 boost 庫的命名特點:【2】

    (1)以“lib”開頭的是“link=static”版本(靜態鏈接庫版本,沒有dll),而直接以“boost”開頭的是“link=shared”版本(動態鏈接庫版本,包含lib和dll)。

    (2)所有的庫都含有"boost"前綴。

    (3)緊隨其后的是boost庫名稱(比如date_time庫)。

    (4)然后是編譯器的版本,與庫名稱之間以"-"而不是下劃線"_"分隔(比如 -vc120)。

    (5)有“mt”的為“threading=multi”版本,沒有的則是“threading=single”版本。

    (6)有“s”的為“runtime-link=static”版本,沒有的則是“runtime-link=shared”版本。

    (7)有“gd”的為debug版本,沒有的則是release版本。

    (8)所有的庫都含有boost庫的版本號結尾(比如1_56,其中的"."以下劃線"_"代替)

    3. link, runtime-link 組合分析

    文章【2】給出了link,runtime-link的具體作用分析。

    假設一個庫A依賴于庫B,我們自己的程序client依賴于庫A,即:

    RFID設備管理軟件

    那么,link指的是client->A,runtime-link指的是A -> B

    配置

    鏈接過程

    運行時需要的文件

    link=static

    runtime-link=static

    client通過A.a (A.lib)靜態包含A;

    A通過B.a (B.lib)靜態包含B;

    不關 .so .dll的事

    client

    link=static

    runtime-link=shared

    client通過A.a (A.lib)靜態包含A;

    在運行時,client要動態調用B.so (B.dll)

    client

    B.so (B.dll)

    link=shared

    runtime-link=shared

    client會包含A.a (A.lib);

    A會包含 B.a (B.lib);

    但都只保存動態庫的真正實現的stub,運行時通過stub去動態加載 A.so (A.dll), B.so (B.dll) 中的實現

    client

    A.so (A.dll)

    B.so (B.dll)

    link=shared

    runtime-link=static

    client會包含A.a (A.lib),但只包含真正實現的stub;

    A通過B.a (B.lib)靜態包含B;

    運行時,client會動態調用A.so (A.dll)

    client

    A.so (A.dll)

     

    3. 配置

    包含頭文件的Include路徑:E:\eCode\boost_1_56_0

    包含庫文件的鏈接路徑:E:\eCode\boost_1_56_0\bin\vc12\lib

    (1)可以設置為僅用于當前project:

    選中當前project->Properties->Configuration Properties->C/C++->General: Additional Include Directories: 設置 E:\eCode\boost_1_56_0

    選中當前project->Properties->Configuration Properties->Linker->General: Additional LibraryDirectories: 設置 E:\eCode\boost_1_56_0\bin\vc12\lib

    (2)可設置為僅用于當前Solution:

    選中當前project->Properties->Configuration Properties->VC++ Directories:

    Include Directories: 設置 E:\eCode\boost_1_56_0

    LibraryDirectories: 設置 E:\eCode\boost_1_56_0\bin\vc12\lib

    (3)可設置為OS當前用戶下的VC++環境(當前用戶下VC++所創建的所有Solution)

    在某個已打開的工程下,切換到Property Manager 選項卡,然后然后展開當前工程的properties配置,打開Microsoft.Cpp.Win32.User

    選擇Common Properties->VC++ Directories:

    Include Directories: 設置 E:\eCode\boost_1_56_0

    LibraryDirectories: 設置 E:\eCode\boost_1_56_0\bin\vc12\lib

    這樣設置的僅在Win32編譯選項下起作用,x64編譯選項需要另外配置x64的properties sheet。

    (4)可設置為OS所有用戶下的VC++環境

    可以編輯 Microsoft.Cpp.Default.props 、Microsoft.Cpp.props 。這里就不介紹了。

    4. 測試

    使用文章【3】中date_time計時函數。創建一個Win32 console 工程,然后copy下面代碼

    復制代碼
    //#define BOOST_DATE_TIME_SOURCE
    #include <iostream>
    #include <boost/date_time/gregorian/gregorian.hpp>
    #include <boost/date_time/posix_time/posix_time.hpp>
    using namespace std;
    using namespace boost::gregorian;
    using namespace boost::posix_time;
    
    /************************************************************************
    創建微秒級的計時器
    ************************************************************************/
    
    template <class T = microsec_clock>
    class MyTimer
    {
    private:
        ptime m_startTime;
    
    public:
        MyTimer()
        {
            Restart();
        }
    
        void Restart()
        {
            m_startTime = T::local_time();
        }
    
    
        void Elapsed()
        {
            cout << T::local_time() - m_startTime << endl;
        }
    };
    
    
    int main()
    {
        MyTimer<microsec_clock> t;
        for(int i = 0; i < 100; ++i)
        {
            cout << "hello" << endl;
        }
        t.Elapsed();
    }
    復制代碼

    注意開頭的宏 “#define BOOST_DATE_TIME_SOURCE” 注掉了。若啟用這個宏定義,則默認由編譯器重新編譯嵌入的頭文件;若不啟用這個宏定義,則表示使用系統已編譯好的date_time庫。

    (1)禁用#define BOOST_DATE_TIME_SOURCE 宏,然后將 libboost_date_time-vc120-mt-gd-1_56.lib 從 E:\eCode\boost_1_56_0\bin\vc12\lib 中移除,編譯debug版的程序時,提示連接錯誤,缺少libboost_date_time-vc120-mt-gd-1_56.lib。

    (2)啟用#define BOOST_DATE_TIME_SOURCE 宏,編譯debug版的程序時,可發現即使在缺少 libboost_date_time-vc120-mt-gd-1_56.lib的情況下,也能成功編譯。

    References

    【1】Boost下載安裝編譯配置使用指南(含Windows、Linux以及ARM Linux)(http://www.cnblogs.com/wondering/archive/2009/05/21/boost_setup.html

    【2】link 和 runtime-link,搭配shared 和 static(http://blog.csdn.net/yasi_xi/article/details/8660549

    【3】計時函數(二)(http://www.cnblogs.com/jerry19880126/archive/2013/02/20/2919718.html

    【4】官方文檔Getting Started on Windows(http://www.boost.org/doc/libs/1_56_0/more/getting_started/windows.html)

    【5】bjam使用(http://blog.chinaunix.net/uid-22301538-id-3158997.html

     

     

     

    我常用的編譯命令是:bjam stage --stagedir="E:\private\boost\boost_1_56_0\bin\vc12" --toolset=msvc-12.0 link=static runtime-link=shared threading=multi debug release --with-system --with-thread --with-random --with-program_options  --with-log  --with-date_time

    RFID管理系統集成商 RFID中間件 條碼系統中間層 物聯網軟件集成
    最近免费观看高清韩国日本大全