当前位置: 首页 > news >正文

VS2022下C++ Boost库安装与使用使用

一.Boost概述

1.简介

Boost 是一个广泛使用的 C++ 库集合,提供了许多高质量、可移植、高效的工具和组件,被视为 C++ 标准库的延伸。自 1998 年成立以来,Boost 已成为 C++ 社区的核心资源,许多 Boost 库通过实践验证后被纳入 C++ 标准(如智能指针、正则表达式、多线程),因此被称为 “C++ 的未来实验室”。

2.跨平台兼容:支持 Windows、Linux、macOS 等主流操作系统,以及 GCC、Clang、MSVC 等编译器。

3.官网:https://www.boost.org/

官方文档:https://www.boost.org/doc/

GitHub 仓库:https://github.com/boostorg/boost

二.win10下Boost安装

方法一:下载源码编译使用

1.下载Boost库

官网下载:访问 Boost官网 下载最新版本(如 boost_1_84_0.tar.gz 或 .zip)。

官网:https://www.boost.org/releases/latest/

2.解压文件

使用工具(如7-Zip)解压到目标目录。

3.生成构建工具

Windows(在解压目录打开命令提示符):

bootstrap.bat

4. 编译Boost库

Windows(VS2022编译64位):

执行b2.exe

5.验证安装

创建测试程序 test_boost.cpp:

#include <boost/filesystem.hpp>

#include <iostream>

int main() {

    std::cout << "Boost version: "

              << BOOST_VERSION / 100000 << "."

              << BOOST_VERSION / 100 % 1000 << "."

              << BOOST_VERSION % 100

              << std::endl;

    return 0;

}

方法二:直接下载二进制版本使用

1.也可以直接下载release版本,直接运行exe文件安装编译好的lib库到指定目录,无需上述编译过程。

Vs2022匹配版本:boost_1_88_0-msvc-14.3-64.exe

.VS2022环境配置

1.步骤 1:项目编译配置

确保编译时选择的 Boost 库版本(32/64位、Debug/Release)与 VS 项目配置一致,如下图:

注意:

如果配置不一致,可能会导致头文件找不到的情况。

2.步骤 2:配置 VS2022 项目

包含头文件目录:

右键项目 → 属性 → VC++ 目录 → 包含目录 → 添加:E:\boost_1_88_0

或者

右键项目---属性---配置属性---c/c++---常规---附加包含目录,添加:E:\boost_1_88_0

Lib库链接:

通用属性->链接器->常规:"附加库目录":同上面的"库目录",添加:

E:\boost_1_88_0\stage\lib

.高级配置(CMake 集成)

若项目使用 CMake,可在 CMakeLists.txt 中添加:

set(BOOST_ROOT "C:/Boost")  # 指定 Boost 根目录

find_package(Boost 1.84.0 REQUIRED COMPONENTS filesystem system regex)

add_executable(MyProject main.cpp)

target_link_libraries(MyProject PRIVATE

    Boost::filesystem

    Boost::system

    Boost::regex

)

.代码示例

1.输出 Boost 版本(验证安装)

#include <iostream>

#include <boost/version.hpp>

int main() {

    std::cout << "Boost 版本: "

              << BOOST_VERSION / 100000 << "."   // 主版本

              << BOOST_VERSION / 100 % 1000 << "."  // 次版本

              << BOOST_VERSION % 100             // 修订号

              << std::endl;

    return 0;

}

编译选项:

确保项目配置(Debug/Release、x64/x86)与 Boost 库的编译参数一致。

若使用动态库(DLL),需将 boost_*.dll 文件复制到项目输出目录(如 Debug/)。

2.文件系统操作(boost::filesystem)

#include <iostream>

#include <boost/filesystem.hpp>

namespace fs = boost::filesystem;

int main() {

    // 检查文件是否存在

    fs::path file_path = "C:\\Windows\\System32\\drivers\\etc\\hosts";

    if (fs::exists(file_path)) {

        std::cout << "文件大小: " << fs::file_size(file_path) << " 字节" << std::endl;

    } else {

        std::cout << "文件不存在!" << std::endl;

    }

    // 遍历当前目录

    std::cout << "\n当前目录内容:" << std::endl;

    for (const auto& entry : fs::directory_iterator(fs::current_path())) {

        std::cout << entry.path().filename() << std::endl;

    }

    return 0;

}

附加依赖项:

libboost_filesystem-vc143-mt-x64-1_84.lib

libboost_system-vc143-mt-x64-1_84.lib

3.正则表达式(boost::regex)

#include <iostream>

#include <boost/regex.hpp>

int main() {

    std::string text = "我的电话号码是 138-1234-5678";

    boost::regex pattern(R"((\d{3})-(\d{4})-(\d{4}))"); // 匹配电话号码

    boost::smatch matches;

    if (boost::regex_search(text, matches, pattern)) {

        std::cout << "完整匹配: " << matches[0] << std::endl;

        std::cout << "区号: " << matches[1] << std::endl;

    } else {

        std::cout << "未找到电话号码!" << std::endl;

    }

    return 0;

}

附加依赖项:

libboost_regex-vc143-mt-x64-1_84.lib

4.日期时间(boost::posix_time)

#include <iostream>

#include <boost/date_time/posix_time/posix_time.hpp>

int main() {

    // 获取当前时间

    boost::posix_time::ptime now = boost::posix_time::second_clock::local_time();

    std::cout << "当前时间: " << now << std::endl;

    // 计算时间差

    boost::posix_time::ptime start = boost::posix_time::microsec_clock::local_time();

    // ...(执行某些操作)

    boost::posix_time::ptime end = boost::posix_time::microsec_clock::local_time();

    boost::posix_time::time_duration duration = end - start;

    std::cout << "耗时: " << duration.total_milliseconds() << " 毫秒" << std::endl;

    return 0;

}

附加依赖项:

libboost_date_time-vc143-mt-x64-1_84.lib

相关文章:

  • 血管的三维重建
  • 【Java】mybatis-plus乐观锁与Spring重试机制
  • 【Typst】5.文档结构元素与函数
  • 【计算机网络 第8版】谢希仁编著 第六章应用层 题型总结1 编码
  • JavaScript 递归构建树形结构详解
  • 闲谈PMIC和SBC
  • Message=“HalconDotNet.HHandleBase”的类型初始值设定项引发异常
  • v4l2常见操作-查看当前摄像头信息,帧率,控制参数,分辨率,支持格式,抓图实践等
  • 【2025年B卷】OD-100分-斗地主之顺子
  • 【大模型:知识图谱】--3.py2neo连接图数据库neo4j
  • 6月2日day43打卡
  • 预警功能深度测评:系统如何降低设备突发故障率?
  • 网络攻防技术九:网络监听技术
  • 每天总结一个html标签——area与map标签
  • 机器人开发前景洞察:现状、机遇、挑战与未来走向
  • NX890NX894美光固态闪存NX906NX908
  • DSN(数字交换网络)由什么组成?
  • NebulaAI V2.6.0发布:工作流功能正式上线!
  • 嵌入式复习小练
  • Python库 Pympler 详解:内存分析与追踪工具
  • 沈阳手机网站制作/网站推广的主要方式
  • 日志文件WordPress/网站首页seo关键词布局
  • 网站模板编辑工具/徐州seo公司
  • 平凉哪家做企业网站/直销怎么做才最快成功
  • 电影网站模板html/西安百度公司开户
  • 深圳东莞网站开发/成都门户网站建设