C++:Boost库

今日安装一个PSI库时,需要boost库,在此认识一下boost库,转载:macOS 中Boost的安装和使用

介绍

Boost是一个功能强大,构造精良,跨越平台,代码开源,完全免费的C++程序库。

共包含160余个库/组件,涵盖字符串与文本处理、容器、迭代器、算法、图像处理、模板元编程、并发编程等多个领域。

由c++标准委员会成员发起倡议并建立boost社区,C++11标准库中三分之二来自boost,并且将来还会有更多的库进入c++标准库,因此boost是一个c++ "准"标准库。

支持现有的所有操作系统。

Boost库的大多数组件不需要编译链接,源码里直接包含头文件。(注意:包含头文件的时候需要有boost目录,即#include "boost/logic/tribool.hpp",而不能是#include "logic/tribool.hpp")剩下的少量库(如chrono,date_time,program_options,test,threa等)必须编译成静态库或动态库,并在构建时指定链接选项才能使用

Boost的独特之处:它把C++类的声明和实现放在了一个文件中,而不是分成两个文件,即.h+.cpp,故文件的后缀是.hpp

功能

含有的功能类有:

  • 字符串和文本处理库
  • 容器库
  • 迭代器库
  • 算法库
  • 函数对象和高阶编程库
  • 泛型编程库
  • 模板元编程
  • 预处理元编程库
  • 并发编程库
  • 数学和数字库
  • 排错和测试库
  • 数据结构库
  • 图像处理库
  • 输入输出库
  • 跨语言混合编程库
  • 内存管理库
  • 解析库
  • 编程接口库
  • 综合类库
  • 编译器问题的变通方案库

目录结构

--boost:最重要的目录,90%以上的Boost程序库源码都在这里

--doc:HTMI格式的文档,也可以生成PDF格式的文档

--libs:所有组件的示例、测试、编译代码和说明文档

--more:库作者的相关文档

--status:可用于测试Boost库的各个组件

--tools:用于编译Boost的工具的源代码等

安装Boost

使用源码安装

(1)下载Boost源码
(2)解压放在任意目录,例如/usr/local/boost_1_63_0

./bootstrap.sh
./b2 headers
./b2
./b2 install

安装时间稍长,生成的静态(同态)文件都在/usr/local/lib中,需要cp到/usr/lib中,可以在代码中直接使用;头文件在/usr/local/include中。

使用Homebrew安装

(1)下载安装HomeBrew

brew install boost

(2)留意运行日志会显示头文件目录 /usr/local/Cellar/boost/1.60.0_2/include, lib目录/usr/local/Cellar/boost/1.60.0_2/lib

使用MacPort安装

下载安装MacPort

sudo port install boost

在CLion中使用Boost

(1)新建一个C++项目
(2)在cmakelists中 增加头文件目录

include_directories(/Users/pam/Desktop/pam/boost_1_78_0/)
或者
find_package(Boost 1.70.0 REQUIRED)
if(Boost_FOUND)
    set(Boost_LIBRARY_DIRS D:/ScanSource/download/Boost/vc141_64/lib)
    message(Boost_INCLUDE_DIRS " ${Boost_INCLUDE_DIRS}")
    message(Boost_LIBRARY_DIRS " ${Boost_LIBRARY_DIRS}")
endif()

include_directories(${Boost_INCLUDE_DIRS})
link_directories(${Boost_LIBRARY_DIRS})

(3)替换main.cpp中代码,运行!输入任意数字回车可看到结果。

在XCode项目中使用Boost

(1)新建一个Command Line Tool项目
(2)在Build Setings - Header Search Paths 增加头文件目录
(3)替换main.cpp中代码,运行!输入任意数字回车可看到结果。

#include <iostream>
#include <boost/lambda/lambda.hpp>

int main(int argc, const char * argv[]) {
    
    printf("Please input any number:");
    using namespace boost::lambda;
    typedef std::istream_iterator<int> in;
    
    std::for_each(in(std::cin), in(), std::cout << (_1 * 3) << " " );
    return 0;
}

在XCode项目中使用Boost Lib库

Boost的很多功能都是直接在hpp头文件里实现的,比如上面的lambda例子不用导入任何lib就可以运行了。但也有一部分需要依赖指定lib库才能使用。比如下面这个正则表达式的例子:

#include <iostream>
#include <boost/regex.hpp>

int main(int argc, const char * argv[]) {
    std::string   str = "2013-08-15";
    boost::regex  rex("(?<year>[0-9]{4}).*(?<month>[0-9]{2}).*(?<day>[0-9]{2})");
    boost::smatch res;
    
    std::string::const_iterator begin = str.begin();
    std::string::const_iterator end   = str.end();
    
    if (boost::regex_search(begin, end, res, rex))
    {
        std::cout << "Day:   " << res ["day"] << std::endl
        << "Month: " << res ["month"] << std::endl
        << "Year:  " << res ["year"] << std::endl;
    }
}

使用静态库

在Build Setings - Other linker flags

/usr/local/boost_1_63_0/stage/lib/libboost_regex.a

使用命令行编译相当于

c++ -I /usr/local/boost_1_63_0 main.cpp -o main /usr/local/boost_1_63_0/stage/lib/libboost_regex.a
./main

如果这里直接使用lboost_regex, Xcode默认加载动态库。实际运用中可以考虑将目录中的动态库删除,只保留静态库,并在Build Setings - Library Search Paths 增加lib文件目录。

使用动态库

(1)在Build Setings - Library Search Paths 增加lib文件目录
(2)将lib文件目录中的libboost_regex.dylib文件拖入项目
(3)确保在Build Phases - Link Bindary With Libraries中已经有该库
(4)在Build Phases - Copy Files, 复制libboost_regex.dylib到Products Directory
使用命令行编译相当于

c++ -I /usr/local/boost_1_63_0 main.cpp -o main -L/usr/local/boost_1_63_0/stage/lib/ -lboost_regex
cp /usr/local/boost_1_63_0/stage/lib/libboost_regex.dylib ./
./main

例子:

#include <iostream>
#include <boost/tuple/tuple.hpp>
#include "boost/version.hpp"
using namespace std;

void version()
{
    cout << BOOST_LIB_VERSION << endl;
    cout << BOOST_VERSION << endl;
}
int main()
{
    using namespace std;
    int i;
    char c;
    double d;
    boost::tuples::tie(i,c,d);
    boost::tuples::tie(i,c,d) = boost::tuples::tuple <int,char,double>(1,'A',0.68);
    cout << d <<endl;

    cout<<"BOOST的版本:";
    version();
    return 0;
}

参考

1、Boost(1):Boost库简介及安装
2、Boost库简介
3、https://zhuanlan.zhihu.com/p/85806857

4、https://blog.csdn.net/weixin_39766005/article/details/120305393

posted @ 2022-04-06 17:59  PamShao  阅读(2519)  评论(0编辑  收藏  举报