Linux 安装 Boost 1.64.0

需要用到的东西

Boost 1.64.0

https://dl.bintray.com/boostorg/release/1.64.0/source/boost_1_64_0.tar.bz2

依赖于zlib、bzip

 

Unicode支持类库ICU(可选,用以支持Regex)

下载地址:http://site.icu-project.org/download
ICU4c-59_1.tgz

配置

解压下载到的压缩包:tar -xvf boost_1_64_0.tar.bz2

解压过程可能需要几分钟,转到解压后的目录中。可以看到很多东西,最重要的就是boost的配置脚本./booststrap.sh

事实上boost的大部分功能都是直接#include就可以了,不过有少部分需要编译成.a或.lib后才能使用,这部分库如下:

The following Boost libraries have portions that require a separate build
and installation step. Any library not listed here can be used by including
the headers only.

The Boost libraries requiring separate building and installation are:
    - atomic				#原子操作(底层版互斥锁)
    - chrono				#关于时间的库
    - container				#标准库容器和扩展(类似std::vector)
    - context				#(C++11)线程上下文切换
    - coroutine				#协程库
    - coroutine2			#协程库V2
    - date_time				#(基于泛型编程)关于日期、时间的库
    - exception				#异常处理
    - fiber				#用户线程库
    - filesystem			#文件系统扩展库
    - graph				#BGL图形界面和图形组件库
    - graph_parallel			#PBGL图形界面和图形组件
    - iostreams				#流、缓冲区、i/o过滤器框架
    - locale				#本地化的Unicode处理工具
    - log				#日志库
    - math				#相对复杂的数学工具库
    - metaparse				#编译时解析嵌入DSL代码
    - mpi				#消息传递接口库
    - program_options			#关于程序命令行和配置文件的库
    - python				#python与C++接口框架
    - random				#完整的随机数生成系统
    - regex				#正则表达式库
    - serialization			#(可持久)序列化
    - signals				#信号和槽
    - system				#操作系统支持,包括诊断是否支持c++0x标准库等
    - test				#简单的程序测试,单元测试,程序执行监控等
    - thread				#可移植多线程库
    - timer				#事件计时器,状态计时器等
    - type_erasure			#多态运行时库
    - wave				#预处理库

以上基于本人翻译Boost Documentation后得出,部分翻译或用处描述可能不准确,仅供参考。

如果有不需要的库,可以通过加上--without-libraries=list除去,此处我就按照默认安装了(安装好后约178MB)。

sudo ./bootstrap.sh --prefix=/usr/local/boost

编译

编译的命令其实很简单,也只有一句

sudo ./b2 && sudo ./b2 install

不过在编译前,一定要记得进入到Boost解压后产生的路径下,不然可能会提示编译失败!(就像下面这样)

boost_1_64_0/bootstrap.sh: 1: boost_1_64_0/bootstrap.sh: ./tools/build/src/engine/build.sh: not found
Building Boost.Build engine with toolset ... 
Failed to build Boost.Build build engine
Consult 'bootstrap.log' for more details

安装过程同样需要一些时间,这次就比较长了,编译了近一个小时才编译完。

编译完后可以看到/usr/local/boost目录下有include和lib文件夹。

使用

在使用前先要注册动态链接库,方法是在/etc/ld.so.conf.d/目录下新建一个boost.conf文件,内容为/usr/local/boost/lib/,然后记得sudo ldconfig。用命令简化就是:

sudo echo /usr/local/boost/lib/ > /etc/ld.so.conf.d/boost.conf
sudo ldconfig

gcc编译时要记得加上-I /usr/local/boost/include-L /usr/local/boost/lib,对,大写的i,中间有没空格都可以!

测试

编辑test.cpp,内容为:

#include <boost/lexical_cast.hpp>
#include <iostream>
using namespace std;
int main()
{
        //此处仅测试boost中的类型转换功能,如果可以使用就能间接代表boost安装成功
        using boost::lexical_cast;
        int a = lexical_cast<int>("123");
        double b = lexical_cast<double>("123.0123456789");
        string s0 = lexical_cast<string>(a);
        string s1 = lexical_cast<string>(b);
        cout << "number: " << a << "  " << b << endl;
        cout << "string: " << s0 << "  " << s1 << endl;
        int c = 0;
        //故意生成一个转换错误
        try{
                c = lexical_cast<int>("abcd");
        }
        catch (boost::bad_lexical_cast& e){
                cout << e.what() << endl;
        }
        return 0;
}

编译使用命令:

g++ test.cpp -I /usr/local/boost/include -L /usr/local/boost/lib -o test

执行:

$ ./test
number: 123  123.012
string: 123  123.0123456789
bad lexical cast: source type value could not be interpreted as target

如果看到输出如上,则boost安装成功!

如果你需要使用到boost中需要安装的库(见配置部分),或者见到了类似如下的错误信息:

undefined reference to `boost::system::generic_category()'
undefined reference to `boost::thread::create()'

请检查是否忘记加上-l<需要用到的库>,如-lboost_system、-lboost_thread等

(完)

转载于:https://my.oschina.net/u/2933242/blog/1491809

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值