c++ 庫 boost安裝


http://blog.chinaunix.net/uid-12226757-id-3427282.html

ubuntu 

apt-get install libboost-dev 

全部:

apt-get install libboost-all-dev

看有些資料寫的:
apt-get install libboost-dev libboost-dbg libboost-doc bcp libboost-*

  如果提示權限不夠,就加上sudo或者切換到擁有足夠權限的賬號進行安裝!

  linux自身就帶有STL了,是 SGI版本的,可以直接使用,不用安裝。

 

自己編譯安裝:

下載ziphttp://www.boost.org/

首先要編譯生成boost安裝工具bjam
進入boost目錄執行:
./bootstrap.sh

輸出:

root@iZ23onhpqvwZ:~/download/boost_1_57/boost_1_57_0# ./bootstrap.sh
Building Boost.Build engine with toolset gcc... tools/build/src/engine/bin.linuxx86/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

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

using  mpi ;  #如果需要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® Core™2 Duo CPU T5750 @ 2.00GHz × 2 ,2G DDR2內存的老機子上,使用上述選項,半個小時就差不多了。這個時間是可以承受的。全部編譯安裝,心理上感覺也舒服些。^_^

 

我的步驟:

./b2 --prefix=/usr

一個漫長的過程。

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

./b2 install

測試:

#include<iostream>
#include<boost/bind.hpp>
using namespace std;
using namespace boost;
int fun(int x,int y){return x+y;}
int main(){
    int m=1;int n=2;
    cout<<boost::bind(fun,_1,_2)(m,n)<<endl;
    return 0;
}

 

Linux下boost庫的安裝
 
 
  最近由於編譯一個程序用到了C++的boost庫,所以便安裝了這個庫。但是,其中遇到了一些小問題。所以記錄下來,以供別人參考。
         首先說一下我的環境:Ubuntu 12.04 32bit,  gcc 4.6.3
         其實在ubuntu下,可以用一下命令直接安裝
sudo apt-get install libboost-dev 
          但是,我這樣安裝以后,編譯程序時出現了很多錯誤,而且都是系統文件的錯誤。我開始以為是我的boost庫版本不對,后來換了好幾個版本,都出現了同樣的問題。后來,自己編譯了一個boost.1.42。然后程序就順利通過了。后來想了一下原因,可能是因為從上面的命令默認安裝的boost在編譯打包時沒有開啟mpi這個功能。而我的程序需要這個功能。以下說一下安裝的簡要步驟(我編譯了boost1.42和boost1.50,兩者安裝方法相同,以后者為例):  www.2cto.com  
 1. 下載boost1.53源文件 (93.56M)  http://sourceforge.net/projects/boost/files/latest/download?source=dlp
 2.解壓后,進入boost_1_53_0目錄,執行:./bootstrap.sh
 3.(重點)修改  tools/build/v2/user-config.jam文件,在最后面加上一行“using mpi ;”(注意mpi后面有個空格,       然后一個分號 )
  4. 執行: ./b2
  5. 執行:sudo ./b2  install
      以上便是全部安裝過程,因為網上提到的比較多,所以此處從略。重點想提的是第三步,務必記得。不過,如果你的程序不需要mpi這個功能,那也就不重要了。應用默認設置即可。而且,第四步,執行時可以在后面家prefix參數,如果不加,裝好后,默認的頭文件在/usr/local/include/boost目錄下。庫文件在/usr/local/lib/目錄下。默認不用修改。
       如果編譯好程序后,在運行時提示無法加載某個庫文件,則把/usr/local/lib下的所有boost的庫文件mv到/usr/lib目錄下就可以了。
 
 
windows安裝:
下載源碼,

boost_1_51_0.zip 下載並解壓到C盤根文件夾

二、編譯boost

1、生成生命行程序

  執行bootstrap.bat

2、編譯

  執行b2.exe,完成后顯示:(時間非常之久,半個多小時)

The Boost C++ Libraries were successfully built!
The following directory should be added to compiler include paths:
    C:/boost_1_51_0
The following directory should be added to linker library paths:
    C:\boost_1_51_0\stage\lib (這是生成的一些列lib文件)

三、使用boost

1、創建一個win32 console

2、引用bootst

  C/C++ -> Additional Include Directories: C:\boost_1_51_0
  Linker-> Additional Library Directories: C:\boost_1_51_0\stage\lib
  Linker->Input->Additional Dependencies :libboost_signals-vc110-mt-gd-1_51.lib;libboost_regex-vc110-mt-gd-1_51.lib;

測試code:

#include "stdafx.h"
#include <boost/regex.hpp>
#include <boost/signals.hpp>
#include <boost/lambda/lambda.hpp> 
 
#include <iostream> 
#include <cassert> 
  
struct print_sum { 
  void operator()(int x, int y) const { std::cout << x+y << std::endl; } 
}; 
  
struct print_product {
  
  void operator()(int x, int y) const { std::cout << x*y << std::endl; }
  
};
 
int _tmain(int argc, _TCHAR* argv[])
{
    boost::signal2<void, int, int, boost::last_value<void>, std::string> sig;
    sig.connect(print_sum());
    sig.connect(print_product());
    sig(3, 5);
    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;     
    }
    return 0;
}
View Code

一個簡單的測試:(沒有特別添加庫)

#include<iostream>
#include<boost/thread/thread.hpp>

void hello()
{
    std::cout << "Hell world,I am a thread!" << std::endl;
}
int main()
{
    boost::thread test_thread(&hello);
    test_thread.join();

}

參考:

http://www.cnblogs.com/chuncn/archive/2012/09/10/2679026.html


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM