c++ 库 boost安装

时间:2023-03-09 16:38:02
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=;int n=;
cout<<boost::bind(fun,_1,_2)(m,n)<<endl;
return ;
}
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(, );
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[] << std::endl;
}
return ;
}

一个简单的测试:(没有特别添加库)

#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