在UBuntu 14.01中配置Boost庫(及CodeBlocks)

在UBuntu 14.01中配置Boost庫
前言:以下配置的步驟參考Boost官方文檔以及其他一些CSDN上的博文。配置的編譯環境是CodeBlocks v13,有關命令行的編譯方法CSDN上介紹了很多,此處不在贅述。

步驟1:
首先下載Boost庫。
Linux下推薦下面的鏈接。
http://www.boost.org/users/history/version_1_56_0.html


步驟2:配置Boost庫
正如Boost庫文檔中說明的,Boost的大部分庫以hpp文件提供,即類的聲明和實現都放在同一文件中並且以源碼提供,在使用時不需要做特別處理。因此,下載壓縮包之後,將壓縮包解壓到合適路徑下就可以使用它們了。在UBuntu中,推薦放置在/home或者/usr/local中的一處。例如,Boost的版本是1.56.0,那麼可以將庫展開到/usr/local/boost_1_56_0這個目錄中。
之後,可以啓動CodeBlocks,配置全局變量。具體操作爲
菜單Settings->Global Variable,配置信息如下。

Global Variable Config - Boost


然後新建控制檯工程(如下圖)。



接下來,在新建的工程中,選擇“Source”資源項新建main.cpp,將Boost的測試代碼(取自Boost官網的Getting Started)粘貼一下。下圖示例中給出了示例代碼和編譯後的輸出結果。

圖片
注意:編譯之前要選擇工程的“Build Options”然後配置一下Boost庫的查找路徑。
之後,按Ctrl+F9執行編譯,就可以知道是否配置完畢。
 

Building Option Console Project



確認執行結果


從源碼可以知道例子是測試Lamda表達式,功能是計算從標準控制檯(即cin輸入流)中輸入的3個數字,每個數字各乘以3。因此,運行示例程序,結果如下圖所示。

Test result of Sample code of Boost Lib


步驟3:編譯二進制Boost庫

由於Boost的配置在持續改進,使用Boost官網起步教程中的快速安裝步驟,只需要3不就可以編譯二進制庫。


1)準備編譯工具 b2;請注意,Boost庫從1.50版開始,使用新的b2編譯程序而不是bjam,其編譯速度已大幅提升。

 在開始前,可以用./bootstrap.sh --help命令查看一下幫助,主要的參數執行如下(注意$是命令行提示符,不用輸入)

$ cd path/to/boost_1_56_0

$ ./bootstrap.sh --help


此處,先按缺省配置操作。直接指定安裝路徑即可,注意下面紅色的參數配置,其中YOUR_BOOST_LIB_DIR是你希望的Boost二進制庫安裝位置,譬如,你可以使用“binlib”作爲目錄名稱,我的例子中是按照“stage”目錄設置的。我的硬盤空間很充足,也
爲了省事 ,是按照編譯全部庫進行操作(可以使用--with-libraries=lib1,lib2,...,的格式制定部分庫進行編譯。有關如何指定編譯部分庫文件的操作在CSDN上已有博文,此處不再贅述)。

以下爲執行./bootstrap.sh的輸出結果:

$ sudo sh ./bootstrap.sh --prefix=YOUR_BOOST_LIB_DIR

 

Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2

Detecting Python version... 2.7

Detecting Python root... /usr

Unicode/ICU support for Boost.Regex?... not found.

Generating Boost.Build configuration in project-config.jam...

Bootstrapping is done. To build, run:

    ./b2

   

To adjust configuration, edit 'project-config.jam'.

Further information:

   - Command line help:

     ./b2 --help

    

   - Getting started guide:

     http://www.boost.org/more/getting_started/unix-variants.html

    

   - Boost.Build  documentation:

     http://www.boost.org/boost-build2/doc/html/index.html

 


由於部分庫編譯時需要系統中有其他庫文件支持,請執行以下操作,確認所有的依賴文件包都已安裝。以下安裝包中python2.6-dev和libbz2-dev特別重要,缺少這些庫文件的支持,編譯過程中python庫和bzip庫會無法編譯。

$ sudo apt-get install python2.6-dev

$ sudo apt-get install libicu-dev

$ sudo apt-get install libbz2-dev


 

此步驟完成之後,就可以啓動b2程序進行編譯。爲了確認編譯的過程沒有出錯,建議將編譯結果的輸出重定向(可以使用">> build.log")到日誌文件。因此,執行以下命令。(實際上,由於bjam已經過時,你即便啓動bjam,也是調用b2程序。)


$ ./b2 install >build.log


如果輸出到屏幕,控制檯中的信息會由於內容過多,後輸出的結果覆蓋掉之前的輸出結果。以下是編譯開始時的信息摘錄。
 

Building the Boost C++ Libraries.

Performing configuration checks

    - 32-bit                   : no  (cached)
    - 64-bit                   : yes (cached)
    - arm                      : no  (cached)
    - mips1                    : no  (cached)
    - power                    : no  (cached)
    - sparc                    : no  (cached)
    - x86                      : yes (cached)
    - lockfree boost::atomic_flag : yes (cached)
    - has_icu builds           : no  (cached)
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam
    - zlib                     : yes (cached)
    - iconv (libc)             : yes (cached)
    - icu                      : no  (cached)
    - icu (lib64)              : no  (cached)
    - compiler-supports-ssse3  : yes (cached)
    - compiler-supports-avx2   : yes (cached)
    - gcc visibility           : yes (cached)
    - long double support      : yes (cached)
warning: skipping optional Message Passing Interface (MPI) library.
note: to enable MPI support, add "using mpi ;" to user-config.jam.
note: to suppress this message, pass "--without-mpi" to bjam.
note: otherwise, you can safely ignore this message.
    - zlib                     : yes (cached)

Component configuration:

    - atomic                   : building
    - chrono                   : building
    - container                : building
    - context                  : building
    - coroutine                : building
    - date_time                : building
    - exception                : building
    - filesystem               : building
    - graph                    : building
    - graph_parallel           : building
    - iostreams                : building
    - locale                   : building
    - log                      : building
    - math                     : building
    - mpi                      : building
    - program_options          : building
    - python                   : building
    - random                   : building
    - regex                    : building
    - serialization            : building
    - signals                  : building
    - system                   : building
    - test                     : building
    - thread                   : building
    - timer                    : building
    - wave                     : building

...patience...
...patience...
...patience...
...patience...
...patience...
...found 11605 targets...
...updating 70 targets...


在所有庫(如前述,根據你的配置需要庫的多少有變化,缺省是編譯所有需要編譯的庫)都成功編譯後,最後幾行會輸出如下結果。
 

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

    /usr/local/boost_1_56_0

The following directory should be added to linker library paths:

    /usr/local/boost_1_56_0/stage/lib


 看到這些提示,恭喜你大功告成! 注意編譯程序給出的路徑提示,即你在使用時如何配置的路徑,分別對應頭文件路徑和二進制庫文件的路徑。

步驟4:確認編譯結果

確認編譯結果同樣可以使用官方網站上提供的例子程序,


Test sample for Boost binary lib


此時配置搜索路徑(仍然是選擇Build options在對話框中選擇Search directories)就需要增加一個選項,如下圖。


Console Project build option with binary lib

同時,還要配置鏈接庫,如下圖。

Linik config for Boost with binary lib

此時,編譯例子程序遍可以成功完成。

在編譯的可執行程序目錄下新建一個文本文件sample.txt,粘貼以下測試信息並保存退出。

To: George Shmidlap
From: Rita Marlowe
Subject: Will Success Spoil Rock Hunter?
---
See subject.
 
然後執行例子程序測試,結果如下圖。

Reult for sampel code of testing Boost binary lib

可以看到例子給的是使用正則表達式的程序,功能是在輸入流中檢索Subject字符串之後的內容(即主題的內容),因此輸出結果是符合期望的。


最後要說的幾句話:
1)如果親的硬盤空間有限,在所有任務結束後,可以切換到剛纔編譯時執行的目錄下,使用以下命令清理編譯中間結果。中間結果存放在“bin.v2”目錄中。注意:如果清理了編譯中間結果,以後Boost庫更新的時候,所有編譯都必須從頭來過。
 
$ ./b2 --clean
 
2)整個安裝過程在不費周章的情況下,大約需要1小時。編譯過程所耗費的時間大致是半小時,我用的硬件配置就是Lenovo Y470,i3+6GB 內存,不算高。

3)本文中的所有步驟我都已經實際測試過,如果試驗過程遇到其他問題可以留言。最後請大家尊重我的勞動成果,轉載請註明出處。多謝! 


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