1、前提条件
linux平台/CentOS 7 下要编译安装Boost除gcc和gcc-c++之外,还需要两个开发库:bzip2-devel
和python-devel
,因此在安装前应该先保证这两个库已经安装。
安装指令:
yum install bzip2 bzip2-devel bzip2-libs python-devel
CentOS 7安装效果:
[root@localhost ~]# yum install bzip2 bzip2-devel bzip2-libs python-devel
已加载插件:fastestmirror
Determining fastest mirrors* base: mirrors.ustc.edu.cn* extras: mirrors.ustc.edu.cn* updates: mirrors.163.com
base | 3.6 kB 00:00:00
docker-ce-stable | 3.5 kB 00:00:00
extras | 2.9 kB 00:00:00
updates | 2.9 kB 00:00:00
(1/2): docker-ce-stable/7/x86_64/primary_db | 117 kB 00:00:26
(2/2): updates/7/x86_64/primary_db | 23 MB 00:02:58
软件包 bzip2-libs-1.0.6-13.el7.x86_64 已安装并且是最新版本
正在解决依赖关系
--> 正在检查事务
*********
2、安装
Boost 官网下载地址:https://www.boost.org/
将下载的boost 源码:boost_1_83_0_rc1.tar.gz, 上传CentOS 7 指定目录,我的目录地址为:/usr/local/source_code/.
CentOS 7 指令效果:
[root@localhost ~]# cd /usr/local/source_code/
[root@localhost source_code]# ll
总用量 146560
-rw-r--r--. 1 root root 144648962 10月 13 10:33 boost_1_83_0_rc1.tar.gz
drwxr-xr-x. 2 root root 42 9月 18 17:32 c_demo
drwxrwxr-x. 6 root root 4096 10月 10 09:44 hiredis-1.2.0
-rw-r--r--. 1 root root 126216 10月 10 09:42 hiredis-1.2.0.tar.gz
drwxr-xr-x. 2 root root 50 10月 10 10:37 hiredis_demo
drwxr-xr-x. 5 500 users 8192 9月 4 15:28 jpeg-9e
-rw-r--r--. 1 root root 1046935 9月 4 15:23 jpegsrc.v9e.tar.gz
-rw-r--r--. 1 root root 4234219 8月 31 16:22 release-3.4.13.tar.gz
drwxr-xr-x. 2 root root 52 10月 10 12:54 zookeeper_demo
drwxrwxr-x. 8 root root 259 8月 31 16:33 zookeeper-release-3.4.13
解压Boost 源码 并安装,执行如下命令:
# 解压boost 源码
tar -zxvf boost_1_83_0_rc1.tar.gz
# 进入boost 源码目录
cd boost_1_83_0
# 启动 boost 安装指南脚本./bootstrap.sh
# 安装boost 全部 模块
./b2 install --with=all
CentOS 7 指令效果:
# 进入Boost 源码目录
[root@localhost source_code]# cd boost_1_83_0
# 启动Boost 库安装指南sh
[root@localhost boost_1_83_0]# ./bootstrap.sh
Building B2 engine..###
###
### Using 'gcc' toolset.
###
###g++ (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44)
Copyright © 2015 Free Software Foundation, Inc.
本程序是自由软件;请参看源代码的版权声明。本软件没有任何担保;
包括没有适销性和某一专用目的下的适用性担保。###
###> g++ -x c++ -std=c++11 -O2 -s -DNDEBUG builtins.cpp class.cpp command.cpp compile.cpp constants.cpp cwd.cpp debug.cpp debugger.cpp execcmd.cpp execnt.cpp execunix.cpp filesys.cpp filent.cpp fileunix.cpp frames.cpp function.cpp glob.cpp hash.cpp hcache.cpp hdrmacro.cpp headers.cpp jam_strings.cpp jam.cpp jamgram.cpp lists.cpp make.cpp make1.cpp md5.cpp mem.cpp modules.cpp native.cpp object.cpp option.cpp output.cpp parse.cpp pathnt.cpp pathsys.cpp pathunix.cpp regexp.cpp rules.cpp scan.cpp search.cpp startup.cpp subst.cpp sysinfo.cpp timestamp.cpp variable.cpp w32_getreg.cpp modules/order.cpp modules/path.cpp modules/property-set.cpp modules/regex.cpp modules/sequence.cpp modules/set.cpp -o b2tools/build/src/engine/b2
Detecting Python version... 2.7
Detecting Python root... /usr
Unicode/ICU support for Boost.Regex?... /usr
Generating B2 configuration in project-config.jam for gcc...Bootstrapping is done. To build, run:./b2To generate header files, run:./b2 headers
******
# 安装Boost 模块
[root@localhost boost_1_83_0]# ./b2 install --with=all
Performing configuration checks- default address-model : 64-bit [1]- default architecture : x86 [1]- compiler supports SSE2 : yes [2]- compiler supports SSE4.1 : yes [2]- has std::atomic_ref : no [2]- has -Wl,--no-undefined : yes [2]- has statx : no [2]- has statx syscall : no [2]- has init_priority attribute : yes [2]- has stat::st_blksize : yes [2]- has stat::st_mtim : yes [2]- has stat::st_mtimensec : no [2]- has stat::st_mtimespec : no [2]- has stat::st_birthtim : no [2]- has stat::st_birthtimensec : no [2]- has stat::st_birthtimespec : no [2]- has fdopendir(O_NOFOLLOW) : yes [2]- has dirent::d_type : yes [2]- has POSIX *at APIs : yes [2]- cxx11_auto_declarations : no [2]- cxx11_auto_declarations : no [3]- has_icu builds : yes [2]
warning: Graph library does not contain MPI-based parallel components.
note: to enable them, add "using mpi ;" to your user-config.jam.
note: to suppress this message, pass "--without-graph_parallel" to bjam.- zlib : yes- bzip2 : yes- lzma : no- zstd : no- has_lzma_cputhreads builds : no [2]- cxx11_constexpr : no [2]- cxx11_constexpr : no [3]- iconv (libc) : yes [2]- icu : yes [2]- iconv (libc) : yes [3]
******
3、Boost 快速上手Demo
在指定目录(/usr/local/source_code) ,创建boost 测试目录(boost_demo)。指令如下:
[root@localhost source_code]# mkdir boost_demo
[root@localhost source_code]# cd boost_demo/
[root@localhost boost_demo]# ll
总用量 0
在boost_demo 目录下,创建boost 测试程序:accumulators_demo.cpp。指令如下:
[root@localhost boost_demo]# vi accumulators_demo.cpp
编辑 accumulators_demo.cpp 文件内容如下:
#include <iostream>
#include <boost/accumulators/accumulators.hpp>
#include <boost/accumulators/statistics/stats.hpp>
#include <boost/accumulators/statistics/mean.hpp>
#include <boost/accumulators/statistics/moment.hpp>
using namespace boost::accumulators;int main()
{// Define an accumulator set for calculating the mean and the// 2nd moment ...accumulator_set<double, stats<tag::mean, tag::moment<2> > > acc;// push in some data ...acc(1.2);acc(2.3);acc(3.4);acc(4.5);// Display the results ...std::cout << "Mean: " << mean(acc) << std::endl;std::cout << "Moment: " << moment<2>(acc) << std::endl;return 0;
}
编译accumulators_demo.cpp 文件并执行。
[root@localhost boost_demo]# g++ accumulators_demo.cpp -o accumulators_demo
In file included from /usr/local/include/boost/function/detail/prologue.hpp:12:0,from /usr/local/include/boost/function.hpp:30,from /usr/local/include/boost/parameter/aux_/tagged_argument.hpp:88,from /usr/local/include/boost/parameter/aux_/tag.hpp:11,from /usr/local/include/boost/parameter/keyword.hpp:10,from /usr/local/include/boost/parameter/nested_keyword.hpp:10,from /usr/local/include/boost/accumulators/accumulators_fwd.hpp:229,from /usr/local/include/boost/accumulators/framework/accumulator_set.hpp:29,from /usr/local/include/boost/accumulators/accumulators.hpp:12,from accumulators_demo.cpp:2:
/usr/local/include/boost/function/detail/requires_cxx11.hpp:18:192: 附注:#pragma message:C++03 support was deprecated in Boost.Function 1.82 and will be removed in Boost.Function 1.84. Please open an issue in https://github.com/boostorg/function if you want it retained.BOOST_PRAGMA_MESSAGE("C++03 support was deprecated in Boost.Function 1.82 and will be removed in Boost.Function 1.84. Please open an issue in https://github.com/boostorg/function if you want it retained.")^
In file included from /usr/local/include/boost/bind/mem_fn.hpp:24:0,from /usr/local/include/boost/mem_fn.hpp:22,from /usr/local/include/boost/function/detail/prologue.hpp:19,from /usr/local/include/boost/function.hpp:30,from /usr/local/include/boost/parameter/aux_/tagged_argument.hpp:88,from /usr/local/include/boost/parameter/aux_/tag.hpp:11,from /usr/local/include/boost/parameter/keyword.hpp:10,from /usr/local/include/boost/parameter/nested_keyword.hpp:10,from /usr/local/include/boost/accumulators/accumulators_fwd.hpp:229,from /usr/local/include/boost/accumulators/framework/accumulator_set.hpp:29,from /usr/local/include/boost/accumulators/accumulators.hpp:12,from accumulators_demo.cpp:2:
/usr/local/include/boost/bind/detail/requires_cxx11.hpp:18:180: 附注:#pragma message:C++03 support was deprecated in Boost.Bind 1.82 and will be removed in Boost.Bind 1.84. Please open an issue in https://github.com/boostorg/bind if you want it retained.BOOST_PRAGMA_MESSAGE("C++03 support was deprecated in Boost.Bind 1.82 and will be removed in Boost.Bind 1.84. Please open an issue in https://github.com/boostorg/bind if you want it retained.")^
In file included from /usr/local/include/boost/typeof/typeof.hpp:16:0,from /usr/local/include/boost/accumulators/numeric/functional.hpp:22,from /usr/local/include/boost/accumulators/statistics/mean.hpp:14,from accumulators_demo.cpp:4:
/usr/local/include/boost/typeof/detail/requires_cxx11.hpp:14:102: 附注:#pragma message:C++03 support was deprecated in Boost.Typeof 1.82 and will be removed in Boost.Typeof 1.84.BOOST_PRAGMA_MESSAGE("C++03 support was deprecated in Boost.Typeof 1.82 and will be removed in Boost.Typeof 1.84.")^
[root@localhost boost_demo]# ll
总用量 244
-rwxr-xr-x. 1 root root 245128 10月 13 10:57 accumulators_demo
-rw-r--r--. 1 root root 678 10月 13 10:53 accumulators_demo.cpp
[root@localhost boost_demo]# ./accumulators_demo
Mean: 2.85
Moment: 9.635
4、Boost 常用库
Boost 是一组扩展了 C++ 功能的库。常用的包括:
1. Boost.Asio:包含了一套网络和底层 I/O 编程的接口,支持同步、异步操作。
2. Boost.Unordered:提供了类似 C++11 中的 std::unordered_map, std::unordered_set 等的无序容器。
3. Boost.Thread:封装了线程相关的接口,如线程类、互斥量、条件变量等。
4. Boost.Python:扩展了 Python/C++ 的转换机制,以便于在 Python 和 C++之间进行进行更灵活的程序集成。
5. Boost.Smart_Ptr:包含智能指针,如 shared_ptr,weak_ptr,scoped_ptr 等用于资源管理的类。
6. Boost.Filesystem:提供一套跨平台的文件系统访问方法。
7. Boost.Graph:提供一套图数据结构及相关算法。
8. Boost.Regex:封装了正则表达式的类。
9. Boost.Log:提供一套跨平台的日志库。
10. Boost.Date_Time:时间和日期库,提供一种简单、易于操作的方式来处理时间和日期。