歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> Linux上安裝使用Boost入門指導

Linux上安裝使用Boost入門指導

日期:2017/2/28 16:09:52   编辑:Linux教程
  1. 獲得boost
  2. boost分布
  3. 只需要頭文件的庫
  4. 使用boost建立一個簡單的程序
  5. 准備使用boost二進制文件庫
  6. 把你的程序鏈接到boost庫

1.獲得boost

下載boost_1_46_1.tar.bz2

解壓

2.boost分布

boost_1_46_1.........................boost根目錄

  boost/.....................................所有boost頭文件

  libs/........................................Tests,.cpps,docs等的庫文件

注意:

  (1)boost根目錄(通常是/usr/local/boost_1_46_1)想到$BOOST_ROOT變量中

  (2)編譯程序時如果用到boost庫,需要指定頭文件路徑-I$BOOST_ROOT

  (3)因為所有頭文件都在boost文件夾下,並且頭文件都是hpp後綴,所#include形如:

    #include <boost/whaever.hpp>

3.只需要頭文件的庫

  絕大多數的boost庫都是header-noly的:它們完全由包含模板和inline函數的頭文件組成,不需要單獨編譯和二進制庫文件,也不需要鏈接時特別對待。

  只有下面的boost庫必需單獨built:

  • Boost.Filesystem
  • Boost.GraphParallel
  • Boost.IOStreams
  • Boost.MPI
  • Boost.ProgramOptions
  • Boost.Python (see the Boost.Python build documentation before building and installing it)
  • Boost.Regex
  • Boost.Serialization
  • Boost.Signals
  • Boost.System
  • Boost.Thread
  • Boost.Wave

  下面這些單獨built是可選(optional)的: 

  Boost.DateTime  Boost.Graph  Boost.Math  Boost.Random  Boost.Test

4.使用boost建立簡單的程序

  下面的程序(example.cc)只用到header-only庫。它是從標准輸入中讀入一串整數,使用Boost.Lambda每個數乘以3後輸出。

#include <boost/lambda/lambda.hpp> #include <iostream> #include <iterator> #include <algorithm> int main() { using namespace boost::lambda; typedef std::istream_iterator<int> in; std::for_each( in(std::cin), in(), std::cout << (_1 * 3) << " " ); }

編譯:g++ -I$BOOST_ROOT example.cc -o example

運行:echo 1 2 3 | ./example

5.准備使用boost二進制庫

  如果你的程序用到需要單獨編譯的boost庫,你需要首先獲得這些二進制庫文件。

5.1編譯安裝所有二進制庫文件

  cd $BOOST_ROOT

  ./bootstrap.sh --help

  ./bootstrap.sh --prefix=/usr/local    ##其實默認情況下prefix的值就是/usr/local

  此時生成了bjam可執行文件,這個東西就是用來編譯boost庫的。

  ./bjam install

5.2僅安裝指定的二進制庫文件

  下面均使用系統默認的編譯器,即Linux上的gcc。

  5.2.1安裝Boost.build

  Boost.Build是一個用於開發、測試、安裝軟件的基於文本的系統。Boost.Build的生成安裝步驟:

  (1)cd $BOOST_ROOT/tools/build/v2

  (2)./bootstrap.sh

  (3)./bjam install --prefix=/usr/local/      ##prefix是Boost.Build安裝位置

  (4)把prefix/bin放到PATH中          ##當然/usr/local/bin已經PATH中了

  5.2.2調用bjam時不指定toolset則使用系統默認的編譯器。如果你的Linux上裝了不同版本的gcc,則使用toolset選項時可以指定版本號:toolset=gcc-4.4

  5.2.3指定build路徑,通過--build-dir=/path選項,不指定時默認在當前路徑下創建bin.v2文件夾,把生成的文件放在其內。

  5.2.4調用bjam

  cd $BOOST_ROOT

  bjam --build-dir=./build-boost toolset=gcc stage

  上面的命令將創建static and shared non-debug multi-threaded variants of the libraries.如果要建立所有的variants,請使用"--build-type=complete"選項。

  所有的boost二進制庫文件將放在stage/lib/下,如果你要另外指定路徑,請使用“--stagedir=directory"選項。

  注意為節省build時間,你可能需要少build一些庫文件:

  • 查看庫文件名稱  --show-libraries
  • 限制build哪些庫  --with-libraryname或者--without-libraryname
  • 選擇特定的build variant  adding release or debug to the command line

  友情提示:Boost.Build會生成很多報告輸出,如果你能保證建立過程不出錯誤,你可以禁止這些輸出以節省時間。方法:在命令後追加”>build.log 2>&1"

6.把你的程序鏈接到boost庫

  下面的程序是從郵件中抽取“主題”內容,它用到了Boost.Regex庫,這個庫是需要單獨編譯的。

#include <boost/regex.hpp> #include <iostream> #include <string> int main() { std::string line; boost::regex pat( "^Subject: (Re: |Aw: )*(.*)" );      while (std::cin) { std::getline(std::cin, line); boost::smatch matches; if (boost::regex_match(line, matches, pat)) std::cout << matches[2] << std::endl; } }

編譯:

方法A:g++ -I$BOOST_ROOT example.cc -o example $BOOST_ROOT/stage/lib/ -lboost_regex.a

方法B:g++ -I$BOOST_ROOT example.cc -o example -L$BOOST_ROOT/stage/lib/ -lboost_regex

當你要使用多個庫源於一個路徑時使用方法B就省力了(paid off)。注意到方法B中並沒有指定.a(靜態庫)還中.so(動態庫),系統將自動地幫你選擇使用靜態庫還是動態庫,當然你可以通過選項“-static"顯示地指定。

6.1庫文件的命名方式

拿libboost_regex-gcc34-mt-d-1_36來說:

lib....................................通用前綴

boost_regex.....................庫名

gcc34...............................編譯時使用的toolset是gcc-3.4

mt...................................編譯時是支持多線程的

d/s/g/y/p..........................ABI tag

1_36................................Tag version

6.2運行我們的程序

  首先新建一個文本文件mail.txt

To: George Shmidlap From: Rita Marlowe Subject: Will Success Spoil Rock Hunter? --- See subject.

如果我們的程序鏈接到了一個共享動態庫,我們需要讓系統知道到哪兒去加載它。請看我的~/.bashrc文件:

#boost export BOOST_ROOT="/usr/local/boost_1_46_1" export LD_LIBRARY_PATH="/usr/local/boost_1_46_1/stage/lib:$LD_LIBRARY_PATH"

運行程序:./example < mail.txt

應該輸出

Will Success Spoil Rock Hunter?
Copyright © Linux教程網 All Rights Reserved