ubuntu12.04+boost_1_54_0

來源:互聯網
上載者:User

http://blog.csdn.net/glt3953/article/details/9960179

手動下載
http://www.bzip.org/1.0.5/bzip2-1.0.5.tar.gz

解壓:

tar --bzip2 -xf ./boost_1_54_0.tar.bz2

開始編譯,全部編譯耗時太多,所以我僅選擇我需要的庫:

先用下面的命令查看有多少庫可以編譯:

./bootstrap.sh --show-libraries
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2

The following Boost libraries have portions that require a separate build
and installation step. Any library not listed here can be used by including
the headers only.

The Boost libraries requiring separate building and installation are:
    - atomic
    - chrono
    - context
    - coroutine
    - date_time
    - exception
    - filesystem
    - graph
    - graph_parallel
    - iostreams
    - locale
    - log
    - math
    - mpi
    - program_options
    - python
    - random
    - regex
    - serialization
    - signals
    - system
    - test
    - thread
    - timer
    - wave

然後就編譯我要的庫:

./bootstrap.sh --with-libraries=system,filesystem,log,thread
Building Boost.Build engine with toolset gcc... tools/build/v2/engine/bin.linuxx86_64/b2
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

然後運行下面的命令完成編譯。

./b2

耐心等待。不過因為不是編譯所有庫。時間會少很多。以後需要再來編譯。很快看到結果:

The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

/usr/src/boost_1_54_0

The following directory should be added to linker library paths:

/usr/src/boost_1_54_0/stage/lib

忘記安裝了。再運行./b2 install 命令,預設安裝在

/usr/local/lib目錄下

標頭檔在

/usr/local/include/boost目錄下

***********************************************************************************************

前期準備:boost中,用到了別的函數庫,所以為了使用boost中相應的功能,需要先安裝系統中可能缺失的庫 


apt-get install mpi-default-dev  #安裝mpi庫

apt-get install libicu-dev     #支援Regex的UNICODE字元集 

apt-get install python-dev     #需要python的話

apt-get install libbz2-dev     #如果編譯出現錯誤:bzlib.h: No such file or directory

上述函數庫裝好之後,就可以編譯boost庫了。解壓boost_1_49_0.tar.bz2,得到/boost_1_49_0,將當前工作目錄切換到此檔案夾下。


./bootstrap.sh

產生bjam,上述命令可以帶有各種選項,具體可參考說明文檔: ./bootstrap.sh --help。其中--prefix參數,可以指定安裝路徑,如果不帶--prefix參數的話(推薦),預設路徑是 /usr/local/include 和 /usr/local/lib,分別存放標頭檔和各種庫。執行完成後,會產生bjam,已經存在的指令碼將會被自動備份。注意,boost 1.49會在目前的目錄下,產生兩個檔案bjam和b2,這兩個是一樣的,所以接下來的步驟,可以用這兩個中的任意一個來執行。


usingmpi ;  #如果需要MPI功能,需要在 /tools/build/v2/user-config.jam 檔案的末尾添加

 接下來就是利用產生的bjam指令碼編譯原始碼了


./b2 -a -sHAVE_ICU=1  #-a參數,代表重新編譯,-sHAVE_ICU=1代表支援Unicode/ICU

注意,這裡是全部編譯。當然也可以選擇只編譯一部分,選項 --with-<library>  只編譯指定的庫,如輸入--with-regex就只編譯regex庫了。boost1.49 的完全編譯,在筆者Intel Core2 Duo CPU T5750 @ 2.00GHz × 2 ,2G DDR2記憶體的老機子上,使用上述選項,半個小時就差不多了。這個時間是可以承受的。全部編譯安裝,心理上感覺也舒服些。^_^

bjam的一些常用的參數,列表如下:

--build-dir=<builddir> 編譯的臨時檔案會放在builddir裡(這樣比較好管理,編譯完就可以把它刪除了)
--stagedir=<stagedir> 存放編譯後庫檔案的路徑,預設是stage
--build-type=complete

編譯所有版本,不然只會編譯一小部分版本,確切地說是相當於:

variant=release, threading=multi;link=shared|static;runtime-link=shared

variant=debug|release 決定編譯什麼版本(Debug or Release?)
link=static|shared 決定使用靜態庫還是動態庫
threading=single|multi 決定使用單線程還是多線程庫
runtime-link=static|shared 決定是靜態還是動態連結C/C++標準庫
--with-<library> 只編譯指定的庫,如輸入--with-regex就只編譯regex庫了
--show-libraries 顯示需要編譯的庫名稱

 編譯完成後,進行安裝,也就是將標頭檔和產生的庫,放到指定的路徑(--prefix)下

注意了:/usr/local/include/   /usr/local/lib/   的許可權一定要是 最大,不然庫的安裝不成功。
命令:
sudo chmod 777 -R /usr/local/include/
sudo chmod 777 -R /usr/local/lib/
./b2 install

 至此,如果一切順利,就完成安裝了。寫個小程式檢驗下,來自《Boost程式庫完全開發指南——深入C++“准”標準庫(修訂版)》(羅劍鋒著,電子工業出版社2012.5)


123456789101112131415

#include <stdio.h>
#include <iostream>
#include <boost/timer.hpp>
 
using namespace boost;
using namespace std ;
 
int main()
{
    timer t;
    cout <<"max timespan: "<< t.elapsed_max() / 3600 <<"h"<< endl;
    cout <<"min timespan: "<< t.elapsed_min() <<"s"<< endl;
 
    cout <<"now time elapsed: "<< t.elapsed() << "s"<< endl;
 
    return 1;
}

 程式輸出:


max timespan: 0.596523hmin timespan: 1e-06s

n

***************************************************************************************

1.下載 boost-1_52  

http://sourceforge.net/projects/boost/files/boost/1.54.0/boost_1_54_0.tar.gz/download

2. 將檔案解壓在/usr/local/目錄下

3. 進入/usr/local/boost-1_54_0/ 目錄, 在terminal中輸入

./bootstrap.sh

4.進入/usr/local/boost-1_54_0/ 目錄,在terminal中輸入

sudo ./b2

5.進入/usr/local/boost-1_54_0/ 目錄,在terminal中輸入

sudo ./bjam --layout=versioned--build-type=complete--toolset=gccinstall

6.添加環境變數(剛改完要重啟或者登出一下來更新剛修改過的環境變數)

兩種方法:

(1)修改/etc/profie檔案 末尾添加

export BOOST_INCLUDE=/usr/local/include/boost-1_54

export BOOST_LIB=/usr/local/lib

(2)在/etc/profile.d/ 中建立一個shell檔案boost.sh

#!/bin/sh

export BOOST_INCLUDE=/usr/local/include/boost-1_54

export BOOST_LIB=/usr/local/lib

測試:
test.cpp

[cpp]
view plaincopy
  1. #include <boost/lexical_cast.hpp>  
  2. #include <iostream>  
  3. int main()  
  4. {  
  5. using boost::lexical_cast;  
  6. int a = lexical_cast<int>("123");  
  7. double b = lexical_cast<double>("123.12");  
  8. std::cout<<a<<std::endl;  
  9. std::cout<<b<<std::endl;  
  10. return 0;  
  11. }  

編譯:
g++ test.cpp -I$BOOST_INCLUDE -L$BOOST_LIB -o test./test
輸出:
123
123.12

1.下載 boost-1_52  

http://sourceforge.net/projects/boost/files/boost/1.54.0/boost_1_54_0.tar.gz/download

2. 將檔案解壓在/usr/local/目錄下

3. 進入/usr/local/boost-1_54_0/ 目錄, 在terminal中輸入

./bootstrap.sh

4.進入/usr/local/boost-1_54_0/ 目錄,在terminal中輸入

sudo ./b2

5.進入/usr/local/boost-1_54_0/ 目錄,在terminal中輸入

sudo ./bjam --layout=versioned--build-type=complete--toolset=gccinstall

6.添加環境變數(剛改完要重啟或者登出一下來更新剛修改過的環境變數)

兩種方法:

(1)修改/etc/profie檔案 末尾添加

export BOOST_INCLUDE=/usr/local/include/boost-1_54

export BOOST_LIB=/usr/local/lib

(2)在/etc/profile.d/ 中建立一個shell檔案boost.sh

#!/bin/sh

export BOOST_INCLUDE=/usr/local/include/boost-1_54

export BOOST_LIB=/usr/local/lib

測試:
test.cpp

[cpp]
view plaincopy
  1. #include <boost/lexical_cast.hpp>  
  2. #include <iostream>  
  3. int main()  
  4. {  
  5. using boost::lexical_cast;  
  6. int a = lexical_cast<int>("123");  
  7. double b = lexical_cast<double>("123.12");  
  8. std::cout<<a<<std::endl;  
  9. std::cout<<b<<std::endl;  
  10. return 0;  
  11. }  

編譯:
g++ test.cpp -I$BOOST_INCLUDE -L$BOOST_LIB -o test./test
輸出:
123
123.12

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.