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版本的,可以直接使用,不用安裝。
自己編譯安裝:
首先要編譯生成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; }
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; }
一個簡單的測試:(沒有特別添加庫)
#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
