Linux下boost库的编译、安装详解

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后输出。

#include <boost/lambda/lambda.hpp>

#include <iostream>

#include <iterator>

#include <algorithm>

 

int main()

{

    using namespace boost::lambda;

    typedef std::istream_iterator<int> in;

 

    std::for_each(

        in(std::cin), in(), std::cout << (_1 * 3) << " " );

}


编译:g++ lambda.cpp -o lambda

运行:echo 1 2 3 | ./lambda

Linux下boost库的编译、安装详解

下载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
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
### 回答1: 安装 Boost 可以让你在 Linux 系统上使用 Boost 提供的 C++ 。以下是在 Linux安装 Boost 的步骤: 1. 打开终端,使用以下命令安装 Boost 的依赖项: ``` sudo apt-get update sudo apt-get install build-essential ``` 2. 下载 Boost ,可以从官网(https://www.boost.org/users/download/)下载最新的版本。解压缩下载的文件,进入解压后的目录。 3. 在终端中进入 Boost 的目录,并运行以下命令: ``` ./bootstrap.sh --prefix=/usr/local ``` 这将启动 Boost 的构建系统,并将安装到 /usr/local 目录中。 4. 运行以下命令编译安装 Boost : ``` ./b2 sudo ./b2 install ``` 这将编译 Boost 并将其安装到 /usr/local/lib 和 /usr/local/include 目录中。 5. 确认 Boost 已经安装成功。在终端中输入以下命令,查看 Boost 的版本: ``` ldconfig -v | grep boost ``` 如果一切正常,应该可以看到安装Boost 的版本信息。 安装成功后,你就可以在 Linux 系统上使用 Boost 了。 ### 回答2: Boost是一个流行的C++,提供了许多有用的功能,例如数据结构、算法和多线程支持。在Linux安装Boost需要以下步骤: 第一步:下载Boost 到官网(https://www.boost.org/)下载最新版的Boost源代码(zip或tar.gz格式)。解压文件到本地目录。 第二步:编译Boost 打开终端,进入Boost源代码目录,运行以下命令开始编译: ./bootstrap.sh --prefix=/usr/local ./b2 其中--prefix指定了安装目录。 该命令将在源代码目录下生成一个bin目录,其中包含许多文件。由于Boost非常庞大,编译时间可能会很长。 第三步:安装Boost 运行以下命令安装Boost: sudo ./b2 install 此命令将安装Boost到指定的目录(默认为/usr/local/lib/)。如果需要安装到其他目录,可以用--prefix指定。 安装完成后,就可以在代码中使用Boost了。将需要使用的头文件路径和文件路径添加到代码编译选项中,即可开始使用Boost的各种功能。 ### 回答3: Linux Boost是一款开源C++,由C++标准的部分扩展而来,提供一系列的高效的数据结构和算法,能够使开发者更加轻松高效地编写C++程序。 安装Boost也比较简单,以下是详细的步骤: 1. 下载Boost 在官网上下载需要的版本:https://www.boost.org/users/download/ 下载完成后解压缩文件 2. 编译Boost 打开终端,进入Boost文件夹,输入以下命令: ./bootstrap.sh 3. 安装Boost 输入以下命令: sudo ./b2 install 等待安装完成即可。 安装完成后,测试安装是否成功,可以输入以下命令: #include <iostream> #include <boost/version.hpp> using namespace std; int main(){ cout << "Boost version:" << BOOST_VERSION << endl; return 0; } 编译运行后如果输出Boost版本号,则表示Boost已经安装成功。 以上就是Linux Boost安装的详细步骤,希望对大家有所帮助。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wangchuang2017

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值