开发者

C++中Boost库安装使用指南(VS2022 + vcpkg)

目录
  • 一、安装Boost组件
  • 二、创建VS2022项目
  • 三、项目配置###
    • 1. C/C++ → 常规 → 附加包含目录:
    • 2. 链接器 → 常规 → 附加库目录:
    • 3. 链接器 → 输入 → 附加依赖项:
  • 四、完整示例代码
    • 输出结果示例
  • 五、高级配置说明
    • 静态链接配置

前置:跨平台C++包管理利器vcpkg完全指南

一、安装Boost组件

# 管理员权限打开PowerShell
cd C:\vcpkg
.\vcpkg install boost-system:x64-Windows boost-filesystem:x64-windows boost-date-time:x64-windows

二、创建VS2022项目

  • 新建项目 → Visual C++ → 控制台应用 → 项目名"BoostDemo"
  • 解决方案资源管理器右键项目 → 属性

三、项目配置###

1. C/C++ → 常规 → 附加包含目录:

  • C:\vcpkg\installed\x64-windows\include

2. 链接器 → 常规 → 附加库目录:

  • C:\vcpkg\installed\x64-windows\lib

3. 链接器 → 输入 → 附加依赖项:

  • boost_system-vc143-mt-x64-1_86.lib
  • boost_filesystem-vc143-mt-x64-1_86.lib

注意:具体名称以自己安装的版本与路径为主。

四、完整示例代码

#include <IOStream>
#include <fstream>
#include <boost/filesystem.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

namespace fs = boost::filesystem;
namespace pt = boost::posix_time;

void print_directory(const fs::path& dir) {
    try {
        if (fs::exists(dir)) {
            std::cout << "目录内容: " << dphpir << "\n";
            for (const auto& entry : fs::directory_iterator(dir)) {
                std::cout << "  " << entry.path().filename() << std::endl;
            }
        }
    }
    catch (const fs::filesystem_error& e) {
        std::cerr << "文件系统错误: " << e.what() << std::endl;
    }
}

int main() {
    // 1. 文件系统操作
    fs::path current_dir = fs::current_pawww.devze.comth();
    std::cout << "当前工作目录: " << current_dir << "\n\n";

    // 创建测试目录
    fs::create_directories("test_dir/data");
    std::ofstream("test_dir/sample.txt") << "Boost测试文件";

    // 列出目录内容
    print_directory("test_dir");

    // 2. 日期时间操作
    pt::ptime now = pt::second_clock::local_time();
    pt::time_duration td = now.time_of_day();

    std::cout << "\n当前时间: "
        << now.date().year() << "-"js
        << std::setw(2) << std::setfill('0') << now.date().month().as_number() << "-"
        << std::setw(2) << now.date().day() << " "
        << td.hours() << ":" << td.minutes() << ":" << td.seconds()
        << std::endl;

    // 3. 路径操作演示
    fs::path p("test_dir/data/file.dat");
    std::cout << "\n路径分解演示:\n"
        << "根目录: " << p.root_name() << "\n"
        << "相对路径: " << p.relative_path() << "\n"
        << "父目录: " << p.parent_path() << "\n"
        << "文件名: " << p.filename() << std::www.devze.comendl;

    // 清理测试目录
    fs::remove_all("test_dir");

    return 0;
}

注意:运行时选Release

输出结果示例

 输出结果示例

当前工作目录: "C:\BoostDemo\x64\Release"

目录内容: "test_dir"

  data

  sample.txt

当前时间: 2024-2-5 14:30:45

路径分解演示:

根目录: ""

相对路径: "test_dir/data/file.dat"

父目录: "test_dir/data"

文件名: "file.dat"

五、高级配置说明

静态链接配置

# 安装静态库版本
vcpkg install boost-system:x64-windows-static

项目属性调整:

  • C/C++ → 代码生成 → 运行库:/MT

到此这篇关于C++中Boost库安装使用指南(VS2022 + vcpkg)的文章就介绍到这了,更多相关C++ Boost库安装内容请搜索编程客栈(www.devze.com)以前的VGHIjFK文章或继续浏览下面的相关文章希望大家以后多多支持编程客栈(www.devze.com)! 

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新开发

开发排行榜