当前位置: 代码网 > 服务器>服务器>Linux > Linux如何编译和安装boost库

Linux如何编译和安装boost库

2024年05月19日 Linux 我要评论
1.下载boost安装包并解压缩到https://www.boost.org/下载boost的安装包,以boost_1_58_0.tar.gz为例下载完成后进行解压缩:tar zxvf boost_1

1.下载boost安装包并解压缩

https://www.boost.org/下载boost的安装包,以boost_1_58_0.tar.gz为例

下载完成后进行解压缩:

tar zxvf boost_1_58_0.tar.gz

2.设置编译器和所选库

先进入解压缩后的目录:

cd boost_1_58_0

然后运行bootstrap.sh脚本并设置相关参数:

./bootstrap.sh --with-libraries=all --with-toolset=gcc

--with-libraries指定编译哪些boost库,all的话就是全部编译,只想编译部分库的话就把库的名称写上,之间用 , 号分隔即可

可指定的库有以下几种:

库名说明
atomic
chrono
context
coroutine
date_time
exception
filesystem
graph图组件和算法
graph_parallel
iostreams
locale
log
math
mpi用模板实现的元编程框架
program_options
python把c++类和函数映射到python之中
random
regex正则表达式库
serialization
signals
system
test
thread可移植的c++多线程库
timer
wave

--with-toolset指定编译时使用哪种编译器,linux下使用gcc即可,如果系统中安装了多个版本的gcc,在这里可以指定gcc的版本,比如--with-toolset=gcc-4.4

命令执行完成后看到显示如下即为成功:

building boost.build engine with toolset gcc... tools/build/src/engine/bin.linuxx86_64/b2
detecting python version... 2.6
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/build/doc/html/index.html

3.编译boost

执行以下命令开始进行boost的编译:

./b2 toolset=gcc

编译的时间大概要10多分钟,耐心等待,结束后会有以下提示:

...failed updating 60 targets...
...skipped 21 targets...
...updated 663 targets...

4.安装boost

最后执行以下命令开始安装boost:

./b2 install --prefix=/usr

--prefix=/usr用来指定boost的安装目录,不加此参数的话默认的头文件在/usr/local/include/boost目录下,库文件在/usr/local/lib/目录下。

这里把安装目录指定为--prefix=/usr则boost会直接安装到系统头文件目录和库文件目录下,可以省略配置环境变量。

安装完毕后会有以下提示:

...failed updating 60 targets...
...skipped 21 targets...
...updated 11593 targets...

最后需要注意,如果安装后想马上使用boost库进行编译,还需要执行一下这个命令:

ldconfig

更新一下系统的动态链接库

5.boost使用测试

以boost_thread为例,测试刚安装完的boost库是否能正确使用,测试代码如下:

#include <boost/thread/thread.hpp> //包含boost头文件
#include <iostream>
#include <cstdlib>
using namespace std;

volatile bool isruning = true;

void func1()
{
    static int cnt1 = 0;
    while(isruning)
    {
        cout << "func1:" << cnt1++ << endl;
        sleep(1);
    }
}

void func2()
{
    static int cnt2 = 0;
    while(isruning)
    {
        cout << "\tfunc2:" << cnt2++ << endl;
        sleep(2);
    }
}

int main()
{
    boost::thread thread1(&func1);
    boost::thread thread2(&func2);

    system("read");
    isruning = false;

    thread2.join();
    thread1.join();
    cout << "exit" << endl;
    return 0;
}

在编译程序时,需要加入对boost_thread库的引用:

g++ main.cpp -g -o main -lboost_thread

如果boost库的安装位置不是在系统目录下,则还需要在编译时加上-i和-l指定boost头文件和库文件的位置

编译成功后运行程序,利用boost实现的多线程任务正确运行:

func1:  func2:00

func1:1
        func2:1
func1:2
func1:3
        func2:2
func1:4
func1:5
        func2:3
func1:6
func1:7
        func2:4
func1:8
func1:9
        func2:5
func1:10
func1:11
        func2:6
func1:12
func1:13
        func2:7
func1:14
func1:15
        func2:8
func1:16

exit

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com