版本选择
选择你想要的版本。这里我用的是boost_1_64_0,在/home目录下下载
$wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.gz
$ tar xvf boost_1_64_0.tar.gz
测试
保存以下代码另存为test.cpp
#include <iostream>
#include <boost/smart_ptr.hpp>
using namespace std;
using namespace boost;
int main(){
int* p = new int;
shared_ptr<int> sh(p);
*p = 123;*sh = 456;
cout<<*p<<endl;
return 0;
}
该程序使用了boost里面shared_ptr智能指针。该库无需编译只需要包含头文件即可以使用
$g++ -I /home/boost_1_64_0 test.cpp
查看需要编译才能使用的库
$ cd boost_1_64_0
$ ./bootstrap.sh --show-libraries //查看需要编译才能使用的库, 显示如下:
- atomic
- chrono
- container
- context
- coroutine
- coroutine2
- date_time
- exception
- fiber
- filesystem
- graph
- graph_parallel
- iostreams
- locale
- log
- math
- metaparse
- mpi
- program_options
- python
- random
- regex
- serialization
- signals
- system
- test
- thread
- timer
- type_erasure
- wave
部分编译
$./ bootstrap.sh //生成bjam
$./bjam --toolset=gcc --with-regex //编译regex库,toolset选项指定编译器,如msvc,gcc等
编译完成显示如下:
测试
编辑以下代码另存为regex.cpp
#include <boost/regex.hpp>
#include <iostream>
#include <string>
int main()
{
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;
}
}
$g++ --std=c++11 regex.cpp -I /home/boost_1_64_0 -L /home/boost_1_64_0/stage/lib/ -lboost_regex
编译通过,生成可执行程序a.out
完全编译
当项目中用到多个boost库的时候可以选择完全编译,完全编译需要的时间较长
$./bjam--toolset=gcc
生成的库与以上所述部分编译的路径一致,当然也可以编译的选择路径等其他参数,详情请看bjam帮助文档。这里不做介绍。
对于生成的include path 和library path要配置对应的环境变量