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

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

http://www.xdnf.cn/news/10914.html

相关文章:

  • 使用 Python + ExecJS 获取网易云音乐歌曲歌词
  • 01电气设计-380V强电部分设计
  • 前缀和基础训练
  • Docker 镜像(或 Docker 容器)中查找文件命令
  • 5月底 端午节
  • 2024-2025-2-《移动机器人设计与实践》-复习资料-1-7
  • C++语法系列之特殊类设计
  • ​​Agentic Voice Stack 热门项目
  • MySQL连接报SSL错误
  • 【QT】认识QT
  • v4l2常见操作-查看当前摄像头信息,帧率,控制参数,分辨率,支持格式,抓图实践等
  • LangChain核心之Runnable接口底层实现
  • Vue中安装插件的方式
  • [蓝桥杯]路径之谜
  • 快速排序(Quick Sort)算法详解(递归与非递归)
  • 第一章-计算机系统概述深化
  • AI数字人技术革新进行时:井云数字人如何重塑人机交互未来?
  • 瑞幸咖啡香港自营门店增至 12 间 未来或拓展至中环等核心区
  • 问题七、isaacsim中添加IMU传感器
  • one-hot编码VS对象嵌入表示
  • docker创建postgreSql带多个init的sql
  • 工厂模式与多态结合
  • 通信算法之281:大疆DJI无人机ID-DJI DroneID开源工程-相关问题-协议信息问题
  • 【高等数学】(2)函数
  • MongoDB数据库学习
  • 【JS服务器】JETBRAINS IDEs JS服务器使用什么编译JNI
  • Docker或Docker-Compose时间时区配置
  • 【亲测有效 | Cursor Pro每月500次快速请求扩5倍】(Windows版)Cursor中集成interactive-feedback-mcp
  • 工业智能网关保障冷冻仓储设备无人值守安全运行
  • 当 “欧洲版 Cursor” 遇上安全危机