1. 程式人生 > >linux下安裝boost與測試

linux下安裝boost與測試

Boost庫是一個功能強大,構造精巧,跨平臺,開源並且完全免費的C++程式庫,
它作為標準庫的後備,是C++標準化程序的發動機之一,
在linux安裝過程如下:

去官方網站下載最新的:www.boost.org

一,下載,編譯安裝,
需要到官方網站下載最新的版本,最新已經到1.60.0。
$ tar -jxvf boost_1_60_0.tar.bz2 
$ cd boost_1_60_0/
$ sudo ./bootstrap.sh 

1. 只編譯不安裝使用下面的命令
$ ./b2
...
...updated 1134 targets...
The Boost C++ Libraries were successfully built!


The following directory should be added to compiler include paths:
    /opt/cpp_practice/boost/boost_1_60_0

The following directory should be added to linker library paths:
    /opt/cpp_practice/boost/boost_1_60_0/stage/lib

2. 編譯並安裝則用下面的命令:
$ ./b2 install
如果像上面這樣不指定額外選項,boost將編譯release版本的庫檔案,
把標頭檔案安裝到 /usr/local/include,庫檔案安裝到 /usr/local/lib。


3. 也可以完整編譯並安裝
$ ./b2 --buildtype=complete install
這樣會對boost進行完整編譯,安裝所有除錯版,發行版的靜態庫和動態庫。

4. 定製化的編譯和安裝
執行命令:
$ ./b2 --show-libraries
可檢視所有必須編譯才能使用的庫
在完全編譯命令的基礎上,使用--with或 --without選擇可開啟或關閉某個庫的編譯,
如:
$ ./b2 --with-date_time --buildtype=complete install
則將僅編譯和安裝date_time庫

二,驗證開發環境
1. 測試程式碼
$ cat test_boost.cpp
#include <boost/version.hpp>

#include <boost/config.hpp>
#include <boost/lexical_cast.hpp>
#include <iostream>

using namespace std;

int main()
{
  using boost::lexical_cast;
  int a= lexical_cast<int>("123456");
  double b = lexical_cast<double>("123.456");
  std::cout << a << std::endl;
  std::cout << b << std::endl;
  return 0;
}

2.編譯,執行
$ g++ -Wall -o test_boost test_boost.cpp
$ ls
test_boost  test_boost.cpp

$ ./test_boost 
123456
123.456