1.linux下的安装方法
http://sourceforge.net/projects/boost寻找一个合适的版本。比如我下载的是boost_1_33_1.tar.gz,解压到/opt。
tar xzvf boost_1_33_1.tar.gz -C/opt
cd /opt/boost_1_33_1
cd tools/build/jam_src/ ./build.sh
cd ../../.. bjam -sTOOLS=gcc install
BOOST_ROOT=/opt/boost_1_33_1 BOOST_INCLUDE=/usr/local/include/boost-1_33_1 BOOST_LIB=/usr/local/lib
#!/bin/sh #boost settings BOOST_ROOT=/opt/boost_1_33_1 BOOST_INCLUDE=/usr/local/include/boost-1_33_1 BOOST_LIB=/usr/local/lib export BOOST_ROOT BOOST_INCLUDE BOOST_LIB
#include <boost/lexical_cast.hpp> #include <iostream> int main() { using boost::lexical_cast; int a = lexical_cast<int>("123"); double b = lexical_cast<double>("123.12"); std::cout<<a<<std::endl; std::cout<<b<<std::endl; return 0; }
g++ lex.cpp -I$BOOST_ROOT -o lex
./lex
123 123.12
#include <iostream> #include <string> #include <boost/regex.hpp> int main() { std::string s = "who,lives:in-a,pineapple under the sea?"; boost::regex re(",|:|-|\\s+"); boost::sregex_token_iterator p(s.begin( ), s.end( ), re, -1); boost::sregex_token_iterator end; while (p != end) std::cout << *p++ << '\n'; }
g++ re.cpp -I$BOOST_ROOT -lboost_regex-gcc -o re
./re
who lives in a pineapple under the sea?
ln -s /opt/boost_1_33_1/boost /usr/include/boost
ln -s /usr/local/include/boost-1_33_1/boost /usr/include/boost
cd /opt/boost_1_33_1 bjam -sTOOLS=gcc clean
#!/usr/bin/python import os import sys import re BOOST_ROOT = os.getenv('BOOST_ROOT') BOOST_LIB = os.getenv('BOOST_LIB') #BOOST_ROOT = '/opt/boost_1_33_1' #BOOST_LIB = '/usr/local/lib' def getlibs(): alls = os.listdir(BOOST_LIB) libpattern = re.compile(r'^libboost_([^-]+)-gcc') libs = {} for lib in alls: m = libpattern.match(lib) if m: libs[m.group(1).lower()] = 1 return libs pattern = re.compile(r'^\s*#include\s*<\s*boost/(.+)\.(h|hpp)\s*>') libs = getlibs() libskeys = libs.keys() includes = {} ENV = os.environ ARGV = sys.argv[1:] files = ARGV if len(files) == 0: sys.exit() for f in files: if f.lower().endswith('.cpp'): fp = open(f, 'r') lines = fp.readlines() for ln in lines: m = pattern.match(ln) if m: libname = m.group(1).lower() if libname in libskeys: includes[libname] = 1 libline = ' '.join(map(lambda lib: '-lboost_'+lib+'-gcc', includes.keys())) obj = ARGV[0] obj = obj[:len(obj)-4] #cmd = 'g++ %s -I%s %s -o %s' % (' '.join(files), BOOST_ROOT, libline, obj) cmd = 'g++ %s %s -o %s' % (' '.join(files), libline, obj) print cmd os.system(cmd)
gccboost lex.cpp gccboost re.cpp
cmd = 'g++ %s %s -o %s' % (' '.join(files), libline, obj)
cmd = 'g++ %s -I%s %s -o %s' % (' '.join(files), BOOST_ROOT, libline, obj)
BOOST_ROOT = os.getenv('BOOST_ROOT') BOOST_LIB = os.getenv('BOOST_LIB')
BOOST_ROOT = '/opt/boost_1_33_1' BOOST_LIB = '/usr/local/lib'
cmd = 'g++ %s %s -o %s' % (' '.join(files), libline, obj)
cmd = 'g++ %s %s' % (' '.join(files), libline)
2.windows下的安装
一、 下载boost
1、boostpro
http://www.boostpro.com/download/
2、boost.org(本文下载方式)
http://www.boost.org/users/download/
http://sourceforge.net/projects/boost/files/boost/1.51.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
三、使用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;
3、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;
}
|
示例程序在vs2012下通过,输出:
8
15