1.下载源文件
去官网下载:http://www.boost.org/
这里下载最新版本
wget https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.zip
2.生成工具
进入解压缩的文件中,运行脚本
./bootstrap.sh
会生成b2和bjam可执行程序
3.编译 运行
./b2 install
4.使用boost建立简单的程序
下面的程序(lambda.cpp)只用到header-only库。它是从标准输入中读入一串整数,使用Boost.Lambda每个数乘以3后输出。
|
编译:g++ lambda.cpp -o lambda
运行:echo 1 2 3 | ./lambda
Linux下boost库的编译、安装详解
下载boost源码
解压到一个目录
tar -zxvf boost_1_66_0.tar.gz
1、正常编译:
进入boost_1_66_0目录中
cd boost_1_66_0
./bootstrap.sh --with-libraries=all --with-toolset=gcc
--with-liraries:需要编译的库
--with-toolset:编译时使用的编译器
安装boost库
./b2 install --prefix=/usr
--prefix:boost库的安装目录,不加此参数,默认安装在/usr/local目录下
示例
g++ -o first first.cpp -lboost_timer -lboost_system
first.cpp:
#include <vector>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/assign.hpp>
#include <boost/timer.hpp>
using namespace std;
using namespace boost;
int main(){
timer t;
vector<int> v = (assign::list_of(1), 2, 3, 4, 5);
BOOST_FOREACH(int x, v){
cout<<x<<",";
}
cout<<endl;
cout<<t.elapsed()<<"s"<<endl;
cout<<"hello world!"<<endl;
return 0;
}
2、交叉编译:
创建脚本xcompile_boost.sh,内容如下:
#xcompile_boost.sh
mfile=project-config.jam
if [ -e ${mfile} ];then
rm ${mfile}
echo "rm ${mfile}"
fi
./bootstrap.sh \
--with-libraries=system,filesystem,regex \
--prefix=/home/moxo/msoft/boost_1_65_1/prefix
if [ -e ${mfile} ];then
mhost="/home/moxo/msoft/openwrt-guoxin/bin/arm-openwrt-linux-gnueabi-gcc -fPIC"
sed -i "/using gcc/c using gcc : arm : ${mhost} ; " ${mfile}
fi
echo "After 5s start compile"
sleep 5s
./b2
echo "Afer 5s start install"
sleep 5s
./b2 install