Boost笔记 1:下载、编译、安装、测试

1. 下载

当前版本是1.82,下载链接:
https://boostorg.jfrog.io/artifactory/main/release/1.82.0/source/

2. 安装编译依赖库

本地环境是Ubuntu 22.04,需要安装以下依赖库,部分影响boost相关功能的开启,部分影响编译过程。
本地环境编译AOSP源码,安装过相关依赖库,除了这里列出的可能还需要其他依赖库,可根据提示安装。
sudo apt install icu-devtools libicu-dev libbz2-dev liblzma-dev libzstd-dev python3-dev python2-dev -y

2.1 解压

解压后源码文件夹:

boost_1_82_0$ ls
boost            boost.css      bootstrap.sh  index.html  libs             README.md  tools
boost-build.jam  boost.png      doc           INSTALL     LICENSE_1_0.txt  rst.css
boostcpp.jam     bootstrap.bat  index.htm     Jamroot     more             status

2.2 执行bootstrap.sh脚本,生成b2编译工具

1)执行bootstrap.sh后,源码目录下增加b2和project-config.jam两个文件。
2)b2工具是用来编译和安装boost库。
(1)查看帮助:b2 --help
(2)编译:b2
(3)clean:b2 --clean
(4)安装:b2 install
(5)重新配置编译脚本:b2 --reconfigure
如果编译失败或者过程中发现缺少一些依赖库及boost相关模块,安装依赖库后,需要执行这个命令重新检测依赖并生成编译脚本。

$ ./bootstrap.sh 
Building B2 engine..

###
###
### Using 'gcc' toolset.
###
###

g++ (Ubuntu 12.1.0-2ubuntu1~22.04) 12.1.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.


###
###

> 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 b2
> tools/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:

    ./b2
    
To generate header files, run:

    ./b2 headers

The configuration generated uses gcc to build by default. If that is
unintended either use the --with-toolset option or adjust configuration, by
editing 'project-config.jam'.

Further information:

   - Command line help:
     ./b2 --help
     
   - Getting started guide: 
     http://www.boost.org/more/getting_started/unix-variants.html
     
   - B2 documentation:
     http://www.boost.org/build/

2.3 编译和安装

1)执行b2命令编译源码,先检测编译依赖,然后根据依赖配置进行编译。
2)成功编译后,安装:b2 install。
默认安装路径为:/usr/local/include/boost 和 /usr/local/lib/

$ ./b2
Performing configuration checks

    - default address-model    : 64-bit [1]
    - default architecture     : x86 [1]

Building the Boost C++ Libraries.


    - 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                : yes [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 POSIX *at APIs       : yes [2]
    - cxx11_auto_declarations  : yes [2]
    - cxx11_constexpr          : yes [2]
    - cxx11_defaulted_functions : yes [2]
    - cxx11_final              : yes [2]
    - cxx11_hdr_mutex          : yes [2]
    - cxx11_hdr_tuple          : yes [2]
    - cxx11_lambdas            : yes [2]
    - cxx11_noexcept           : yes [2]
    - cxx11_nullptr            : yes [2]
    - cxx11_rvalue_references  : yes [2]
    - cxx11_template_aliases   : yes [2]
    - cxx11_thread_local       : yes [2]

...(检测编译依赖)

Component configuration:

    - atomic                   : building
    - chrono                   : building
    - container                : building
    - context                  : building
    - contract                 : building
    - coroutine                : building
    - date_time                : building
    - exception                : building
    - fiber                    : building
    - filesystem               : building
    - graph                    : building
    - graph_parallel           : building
    - headers                  : building
    - iostreams                : building
    - json                     : building
    - locale                   : building
    - log                      : building
    - math                     : building
    - mpi                      : building
    - nowide                   : building
    - program_options          : building
    - python                   : building
    - random                   : building
    - regex                    : building
    - serialization            : building
    - stacktrace               : building
    - system                   : building
    - test                     : building
    - thread                   : building
    - timer                    : building
    - type_erasure             : building
    - url                      : building
    - wave                     : building

...patience...
...patience...
...found 18844 targets...
...updating 1767 targets...

... (编译源码)

common.copy /home/dev/github/boost/temp/boost_1_82_0/stage/lib/libboost_wave.a
...updated 1767 targets...


The Boost C++ Libraries were successfully built!

The following directory should be added to compiler include paths:

    /home/dev/github/boost/temp/boost_1_82_0

The following directory should be added to linker library paths:

    /home/dev/github/boost/temp/boost_1_82_0/stage/lib

2.4 测试程序

安装后,不用指定include和library path,编译时可自动搜索到。
以下测试程序可用clion或者命令行编译:

$ g++ -o main main.cpp
#include <string>
#include <iostream>
#include <boost/foreach.hpp>

int main()
{
    std::string hello( "Hello, world!" );

    BOOST_FOREACH( char ch, hello )
    {
        std::cout << ch;
    }

    return 0;
}

2.5 CMake依赖

在cmake脚本中使用find_package显示引用boost:

find_package(Boost 1.82 REQUIRED COMPONENTS filesystem regex PATHS C:/Boost)
find_package(Boost 1.82 REQUIRED COMPONENTS ALL)
find_package(Boost 1.82 REQUIRED ALL)
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
`boost::asio::io_service`是Boost中的一个核心类,用于提供异步I/O操作的事件循环机制。它是实现异步网络编程的基础,可用于处理各种网络通信任务。 `boost::asio::io_service`的主要作用是管理和调度异步操作。它负责处理事件循环,监视和分发I/O事件,以及执行注册的回调函数。通过使用`io_service`对象,可以实现非阻塞式的异步I/O编程,提高程序的并发性和响应性。 以下是`boost::asio::io_service`类的常见用法示例: ```cpp #include <boost/asio.hpp> void handleRead(const boost::system::error_code& error, std::size_t bytes_transferred) { if (!error) { // 处理读取数据的回调逻辑 } else { // 处理错误情况 } } int main() { boost::asio::io_service io_service; // 创建一个socket对象 boost::asio::ip::tcp::socket socket(io_service); // 连接到服务器 socket.connect(endpoint); // 异步读取数据 boost::asio::async_read(socket, boost::asio::buffer(buffer), handleRead); // 开始事件循环 io_service.run(); return 0; } ``` 在上述示例中,首先创建了一个`io_service`对象,然后创建了一个TCP socket对象,并使用`io_service`对象进行异步读取操作。在`async_read`函数中,指定了一个回调函数`handleRead`,用于处理读取数据完成后的回调逻辑。 最后,通过调用`io_service`的`run()`函数启动事件循环,使程序进入等待事件的状态。在事件循环中,`io_service`会不断地监视和分发I/O事件,并执行注册的回调函数。 使用`boost::asio::io_service`,可以方便地实现异步的网络编程,处理各种I/O操作,如读取、写入、连接、接收等。它提供了高度灵活和可扩展的方式来构建异步应用程序。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

抓饼先生

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

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

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

打赏作者

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

抵扣说明:

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

余额充值